8161957                 2014-10-10 06:12:37     njczy2010     D - Two Sets             GNU C++     Accepted                 171 ms                 7900 KB    
8156137                 2014-10-09 17:26:01     njczy2010     D - Two Sets             GNU C++     Wrong answer on test 9                 30 ms                 2700 KB    
8156046                 2014-10-09 17:20:58     njczy2010     D - Two Sets             GNU C++     Time limit exceeded on test 1                 1000 ms                 2700 KB    
8155943                 2014-10-09 17:16:09     njczy2010     D - Two Sets             GNU C++     Wrong answer on test 9                 31 ms                 2700 KB    
8154660                 2014-10-09 16:09:13     njczy2010     D - Two Sets             GNU C++     Wrong answer on test 6                 15 ms                 2700 KB

set真是太好用了,55555,要好好学stl

D. Two Sets
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

 
 

Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied:

  • If number x belongs to set A, then number a - x must also belong to set A.
  • If number x belongs to set B, then number b - x must also belong to set B.

Help Little X divide the numbers into two sets or determine that it's impossible.

Input

The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109).

Output

If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B.

If it's impossible, print "NO" (without the quotes).

Sample test(s)
Input
  1. 4 5 9 2 3 4 5
Output
  1. YES 0 0 1 1
Input
  1. 3 3 4 1 2 4
Output
  1. NO
Note

It's OK if all the numbers are in the same set, and the other one is empty.

题解转自:http://blog.csdn.net/u011353822/article/details/39449071

看到2Set我还真以为用2Set解,后来想想应该是2分匹配图,结果图左右两边分不出来,构不出图,弱爆了。。。唉。。早上起来又掉rating了

看了别人的解题报告,用的是直接暴力,能在a里处理的都放a集合,否则放入b集合,在b里开始遍历,如果b-x不在就去a里拿,如果a中也没有就输出NO,艾玛。。。。。

事实证明有时候想太多也不好

还能用并查集做,膜拜~~~  http://blog.csdn.net/budlele/article/details/39548063

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #include<set>
  10. #include<string>
  11. //#include<pair>
  12.  
  13. #define N 100005
  14. #define M 1000005
  15. #define mod 1000000007
  16. //#define p 10000007
  17. #define mod2 100000000
  18. #define ll long long
  19. #define LL long long
  20. #define maxi(a,b) (a)>(b)? (a) : (b)
  21. #define mini(a,b) (a)<(b)? (a) : (b)
  22.  
  23. using namespace std;
  24.  
  25. int n,a,b;
  26. map<int,int>mp;
  27. set<int>f,g;
  28. vector<int>c;
  29. int x;
  30. int res[N];
  31. int flag;
  32.  
  33. void ini()
  34. {
  35. memset(res,,sizeof(res));
  36. flag=;
  37. mp.clear();
  38. f.clear();
  39. g.clear();
  40. c.clear();
  41. int i;
  42. int y;
  43. for(i=;i<=n;i++){
  44. scanf("%d",&x);
  45. mp[x]=i;
  46. f.insert(x);
  47. }
  48. for(set<int>::iterator it=f.begin();it!=f.end();it++){
  49. y=*it;
  50. if(f.find(a-y)==f.end()){
  51. c.push_back(y);
  52. //c.push_back(a-y);
  53. }
  54. }
  55.  
  56. for(vector<int>::iterator it=c.begin();it!=c.end();it++){
  57. f.erase(*it);
  58. g.insert(*it);
  59. }
  60. }
  61.  
  62. void solve()
  63. {
  64. while(g.empty()!=){
  65. set<int>::iterator it=g.begin();
  66. int y=*it;
  67. if(g.find(b-y)!=g.end()){
  68. res[ mp[y] ]=;
  69. res[ mp[b-y] ]=;
  70. g.erase(y);g.erase(b-y);
  71. }
  72. else{
  73. if(f.find(b-y)!=f.end()){
  74. res[ mp[y] ]=;
  75. res[ mp[b-y] ]=;
  76. g.erase(y);
  77. f.erase(b-y);
  78. if(f.find( a-(b-y) )!=f.end()){
  79. g.insert(a-(b-y));
  80. f.erase(a-(b-y));
  81. }
  82. }
  83. else{
  84. flag=;return;
  85. }
  86. }
  87.  
  88. }
  89. }
  90.  
  91. void out()
  92. {
  93. if(flag==){
  94. printf("NO\n");
  95. }
  96. else{
  97. printf("YES\n");
  98. printf("%d",res[]);
  99. for(int i=;i<=n;i++){
  100. printf(" %d",res[i]);
  101. }
  102. printf("\n");
  103. }
  104. }
  105.  
  106. int main()
  107. {
  108. // freopen("data.in","r",stdin);
  109. //freopen("data.out","w",stdout);
  110. // scanf("%d",&T);
  111. // for(int ccnt=1;ccnt<=T;ccnt++)
  112. // while(T--)
  113. while(scanf("%d%d%d",&n,&a,&b)!=EOF)
  114. {
  115. //if(n==0 && k==0 ) break;
  116. //printf("Case %d: ",ccnt);
  117. ini();
  118. solve();
  119. out();
  120. }
  121.  
  122. return ;
  123. }

Codeforces Round #268 (Div. 2) D. Two Sets [stl - set + 暴力]的更多相关文章

  1. Codeforces Round #268 (Div. 1) B. Two Sets 暴力

    B. Two Sets Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/468/problem/B ...

  2. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  3. Codeforces Round #277 (Div. 2) D. Valid Sets 暴力

    D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...

  4. Codeforces Round #268 (Div. 1) A. 24 Game 构造

    A. 24 Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/468/problem/A D ...

  5. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  6. Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #268 (Div. 2) (被屠记)

    c被fst了................ 然后掉到600+.... 然后...估计得绿名了.. sad A.I Wanna Be the Guy 题意:让你判断1-n个数哪个数没有出现.. sb题 ...

  8. Codeforces Round #277 (Div. 2) D. Valid Sets DP

    D. Valid Sets   As you know, an undirected connected graph with n nodes and n - 1 edges is called a  ...

  9. Codeforces Round #268 (Div. 2)

    补题解: E:只会第四种解法:也只看懂了这一种. PS:F[X+10^18]=F[X]+1;F[X]表示X的数字之和; 假设X,F[10^18+X]+F[10^18+X-1]+......F[10^1 ...

随机推荐

  1. 机器学习(1)- 概述&线性回归&逻辑回归&正则化

    根据Andrew Ng在斯坦福的<机器学习>视频做笔记,已经通过李航<统计学习方法>获得的知识不赘述,仅列出提纲. 1 初识机器学习 1.1 监督学习(x,y) 分类(输出y是 ...

  2. C-基础:atoi

    C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  3. Js自学学习-笔记6-8

    <!-- 第6-7课笔记 --> <!-- for循环 for(条件1:判断:变化)其实就是if嵌套 while do for循环简化版 可以用do while swith case ...

  4. 分布式mysql 和 zk ( zookeeper )的分布式的区别 含冷热数据讨论

    zk ( zookeeper )的分布式仅仅指的是备份模式. 分布式 mysql 不仅仅要关注备份(从以往的半主,主主,到 paxos). (mysql 比 hbase 的region成熟, hdfs ...

  5. ios调试小结

    Xcode底部的小黑盒是我们调试时的好朋友,它可以输出日志信息.错误信息以及其他有用的东西来帮你跟踪错误,除了可以看到日志直接输出的信息外,我们编程过程中也可以在某些断点停留,来检查app的多个方面. ...

  6. javascipt的forEach

    1.Array let arr = [1, 2, 3]; arr.forEach(function (element, index, array) { console.log('数组中每个元素:', ...

  7. 【树形dp】7.14城市

    很典型的按照边考虑贡献的题. 题目描述 小A居住的城市可以认为由n个街区组成.街区从1到n依次标号街区与街区之间由街道相连,每个街区都可以通过若干条街道到达任意一个街区,共有n-1条街道.其中标号为i ...

  8. mysql:explain分析sql

    对于执行较慢的sql,可以使用explain命令查看这些sql的执行计划.查看该SQL语句有没有使用上了索引,有没有做全表扫描,这都可以通过explain命令来查看 mysql> explain ...

  9. dom4j 常用操作

    package com.wanbang.wbyyb.common.util; import com.alibaba.fastjson.JSONObject; import com.wanbang.wb ...

  10. (转)UILabel常用属性

    Java代码 收藏代码 #import "ViewController.h" #import <CoreText/CoreText.h> @interface View ...