您的位置:首页 > 其它

51nod 1557 两个集合(hash)

2017-08-29 23:40 253 查看
1557 两个集合


题目来源: CodeForces

基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题


 收藏


 关注

小X有n个互不相同的整数: p1,p2,...,pn 。他想把这些整数分到两个集合A和B里边。但是要符合下面两个条件。

·        如果x属于A,那么a-x也肯定属于A。

·        如果x属于B,那么b-x也肯定属于B。

判断一下是否存在一种方案来分配这些数字到集合A,B中。

注意:如果一个集合为空也是可以的。

Input
单组测试数据。
第一行有三个整数n,a,b (1≤n≤10^5; 1≤a,b≤10^9)。
第二行有n个不一样的整数 p1,p2,...,pn (1≤pi≤10^9).


Output
如果可行,那么输出YES,否则输出NO。


Input示例
样例输入1
4 5 9
2 3 4 5


Output示例
样例输出1
YES



题目给的条件往往都是非常有用的,这题有一个很有用的条件就是每一个数都不同,如果有一个数既能属于A集合又能属于B集合,就看他在这个集合A中对应的那个数

如果那个数只能和这个数匹配存在于集合A,那么匹配,否则这个数应该属于集合B;

坑点太多。。。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
map<LL,int>q;
LL s
;
LL a, b;
int judge(LL x)
{
LL y=b-x;
if(q[y]==1&&q[a-y]!=1) return 0;
return 1;
}

int main()
{
int n;

scanf("%d %lld %lld", &n, &a, &b);
q.clear();
for(int i=1; i<=n; i++)
{
LL x;
scanf("%lld", &s[i]);
q[s[i]]=1;
}
int cnt=0;
int flag=0;
for(int i=1; i<=n; i++)
{
if(q[s[i]]!=1) continue;
if(a>=s[i]&&q[a-s[i]]==1)
{
if(judge(s[i]))
{
if(a==(s[i])*2) cnt+=1;
else cnt+=2;
q[s[i]]--;
q[a-s[i]]--;
continue;
}
}
if(b>=s[i]&&q[b-s[i]]==1)
{
q[s[i]]--;
q[b-s[i]]--;
if(b==(s[i])*2) cnt+=1;
else cnt+=2;
continue;
}
flag=1;
break;
}
if(cnt!=n) flag=1;
if(flag) puts("NO");
else puts("YES");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: