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; }