zoukankan      html  css  js  c++  java
  • 集合问题

    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
  • 相关阅读:
    字符串哈希
    codeforces#766 D. Mahmoud and a Dictionary (并查集)
    莫比乌斯反演模板
    马拉车模板
    codeforces#580 D. Kefa and Dishes(状压dp)
    1076E
    448C
    543A
    295B
    poj3974 Palindrome
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12463402.html
Copyright © 2011-2022 走看看