51 nod 1557 两个集合
只需要将满足条件的数尽可能的放一个集合里面,然后看有没有不满足的条件发生,注意可能先放A也可能先放B,所以要跑两遍,一边先放A,一边先放B(没注意到这点wa了好多发。)
#include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; typedef double dl; const int N = 1e5+7; const int M = 1e9+7; const int INF = 0x7fffffff; int p[N]; set<int> st; set<int> sta; set<int> stb; set<int> st1; set<int> sta1; set<int> stb1; void solve() { int n,A,B; scanf("%d%d%d",&n,&A,&B); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); st.insert(p[i]); st1.insert(p[i]); } sort(p+1,p+n+1); int flag1=1; for(int i=n;i>=1;i--) { if(st.count(p[i])==0) continue; else if(st.count(A-p[i])||sta.count(p[i])||sta.count(A-p[i])) { if(st.count(p[i])) sta.insert(p[i]); if(st.count(A-p[i])) sta.insert(A-p[i]); if(st.count(p[i])) st.erase(p[i]); if(st.count(A-p[i])) st.erase(A-p[i]); } else if(st.count(B-p[i])||stb.count(p[i])||stb.count(B-p[i])) { if(st.count(p[i])) stb.insert(p[i]); if(st.count(B-p[i])) stb.insert(B-p[i]); if(st.count(p[i])) st.erase(p[i]); if(st.count(B-p[i])) st.erase(B-p[i]); } else flag1=0; } int flag2=1; for(int i=n;i>=1;i--) { if(st1.count(p[i])==0) continue; else if(st1.count(B-p[i])||sta1.count(p[i])||sta1.count(B-p[i])) { if(st1.count(p[i])) sta1.insert(p[i]); if(st1.count(B-p[i])) sta1.insert(B-p[i]); if(st1.count(p[i])) st1.erase(p[i]); if(st1.count(B-p[i])) st1.erase(B-p[i]); } else if(st1.count(A-p[i])||stb1.count(p[i])||stb1.count(A-p[i])) { if(st1.count(p[i])) stb1.insert(p[i]); if(st1.count(A-p[i])) stb1.insert(A-p[i]); if(st1.count(p[i])) st1.erase(p[i]); if(st1.count(A-p[i])) st1.erase(A-p[i]); } else flag2=0; } if(flag1||flag2) { printf("YES\n"); } else printf("NO\n"); } int main() { solve(); }