码迷,mamicode.com
首页 > 其他好文 > 详细

集合问题

时间:2020-03-11 16:58:40      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:就是   ace   turn   pre   def   ios   ret   i++   splay   

https://ac.nowcoder.com/acm/problem/15167

 

二分

题意:先给b,再分给a,不符合就是no

二分就行了

技术图片
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5 + 5;
int n,ai,bi,p[maxn];
int a[maxn];
int vis[maxn];
int lower(int x){
    int l = 0, r = n - 1;
    while(l <= r){
        int mid = (l + r) / 2;
        if(p[mid] > x)
            r = mid - 1;
        else l = mid + 1;
        if(p[mid] == x && !vis[mid])
            return mid;
    }
    return n;
}
signed main(){
   // freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin >> n >> ai >> bi;
    for(int i = 0; i < n; i++)
        cin >> p[i];
    sort(p,p+n);
    for(int i = 0; i < n; i++){
        if(vis[i]) continue;
        int u = lower(bi - p[i]);
        if(u != n && p[u] + p[i] == bi){
            a[u] = a[i] = 1;
            vis[u] = vis[i] = 1;
        }else{
            u = lower(ai - p[i]);
            if(u != n && p[u] + p[i] == ai){
                a[u] = a[i] = 0;
                vis[u] = vis[i] = 1;
            }else{
                cout << "NO";
                return 0;
            }
        }
    }
    cout << "YES" << endl;
    for(int i = 0; i < n; i++){
        if(!i) cout << a[0];
        else cout << " " << a[i];
    }
    return 0;
}
View Code

 

集合问题

标签:就是   ace   turn   pre   def   ios   ret   i++   splay   

原文地址:https://www.cnblogs.com/xcfxcf/p/12463402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!