http://codeforces.com/contest/469/problem/D

题目大意:

给你一个长度为n数组,给你两个集合A、B,再给你两个数字a和b。A集合中的每一个数字x都也能在a集合中找到x-a的数字。同理,b集合也一样。问,这个数组能否分成这两个集合?(然后坑点就是集合里面的元素可以为空)

思路一:

首先定义type函数,-1表示不在任何集合内,0表示A集合内,1表示B集合,2表示该元素是a的一半。(之所以要表示成2的原因是因为这个数值很特殊,a-x=x,所以他可以满足A集合,但是同时也可能存在ty=-1的数值y,他的b-y=x,所以这个时候集合是可以发生改变的)

我们先暴力枚举并且二分找一下满足条件数组A里面的元素。再找一下数组B里面的元素,且在找这个数组B的时候不能修改A中集合中的元素(即ty=0)。

然后我们暴力发现还存在ty=-1的数值,那么他肯定不满足属于A集合,因此可能是属于B集合的,所以我们对该元素进行bfs一下就好了。

复杂度O(n)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int p[maxn], ans[maxn], ty[maxn];
map<int, int> id;
int a, b, n; ///a type is 0,b is 1, 2 is suit for a
void bfs(int w){
queue<int> que;
que.push(w);
while (!que.empty()){
int pos = que.front(); que.pop();
int bval = b - p[pos];
int mypos = lower_bound(p, p + n, bval) - p;
if (p[mypos] == bval) {
if (ty[mypos] != ) {
int aval = a - p[mypos];
int sonpos = lower_bound(p, p + n, aval) - p;
if (p[sonpos] != aval) continue;
if (ty[sonpos] != ){
que.push(sonpos);
ty[sonpos] = -;
}
}
else continue;
ty[mypos] = ty[pos] = ;
}
}
} bool solve(){
if (p[n] >= a && p[n] >= b) return false;
///集合在a里面
for (int i = ; i < n; i++){
if (p[i] >= a) break;
if (ty[i] == ) continue;
if (p[i] * == a) {
ty[i] = ; continue;
}
int val = a - p[i];
int pos = lower_bound(p, p + n, val) - p;
if (pos >= n || p[pos] != val) continue;
if (ty[pos] == -){
ty[pos] = , ty[i] = ;
}
}
///集合在b里面
for (int i = ; i < n; i++){
if (p[i] >= b) break;
if (ty[i] == || ty[i] == ) continue;
if (p[i] * == b && ty[i] == -){
ty[i] = ; continue;
}
int val = b - p[i];
int pos = lower_bound(p, p + n, val) - p;
if (pos >= n || p[pos] != val || ty[pos] == ) continue;
if (ty[pos] == - || ty[pos] == ){
ty[pos] = , ty[i] = ;
}
}
for (int i = ; i < n; i++)
if (ty[i] == -) bfs(i);
for (int i = ; i < n; i++){
if (ty[i] == -) return false;
if (ty[i] == ) ty[i] = ;
}
return true;
} int main(){
int cnt = ;
cin >> n >> a >> b;
for (int i = ; i < n; i++){
scanf("%d", p + i);
id[p[i]] = cnt++;
}
sort(p, p + n);
memset(ty, -, sizeof(ty));
bool flag = solve();
if (flag){
puts("YES");
for (int i = ; i < n; i++){
int myid = id[p[i]];
ans[myid] = ty[i];
}
for (int i = ; i < n; i++){
printf("%d ", ans[i]);
}
cout << endl;
}
else puts("NO");
return ;
}

思路二:

当然,这题还可以用并查集来分配

假定,如果存在x,在数列中不存在a-x的数字,那么他一定属于集合B,同理,存在x,在数列中不存在b-x的数字,那么他一定属于集合A。然后我们只要利用这个条件,然后用并查集维护一下就好了。

这个的话可以看这个人的代码:链接在此

贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

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

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

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

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

  5. Codeforces Round #268 (Div. 2) D. Two Sets [stl - set + 暴力]

    8161957                 2014-10-10 06:12:37     njczy2010     D - Two Sets             GNU C++     A ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #268 (Div. 1) 468D Tree(杜教题+树的重心+线段树+set)

    题目大意 给出一棵树,边上有权值,要求给出一个1到n的排列p,使得sigma d(i, pi)最大,且p的字典序尽量小. d(u, v)为树上两点u和v的距离 题解:一开始没看出来p需要每个数都不同, ...

随机推荐

  1. kernel Makefile Kconfig说明

    实际文档位置:Documentation/kbuild/makefiles.txt,此为翻译稿. *************************************************** ...

  2. Impala:新一代开源大数据分析引擎

    Impala架构分析 Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统虽然也提供了SQL语 ...

  3. 第七十三节,css盒模型

    css盒模型 学习要点: 1.元素尺寸 2.元素内边距 3.元素外边距 4.处理溢出 本章主要探讨HTML5中CSS盒模型,学习怎样了解元素的外观配置以及文档的整体布局. 一.元素尺寸 CSS盒模型中 ...

  4. 3、File类之创建、删除、重命名、判断方法

    一般我们调用内置类的方法,都是指调用其成员方法,故而以下几种方法都是File类的成员方法,常用的有以下3种, 分别是 //创建 public boolean createNewFile() publi ...

  5. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  6. php基本(四)表单验证

    本文内容来自http://www.w3school.com.cn/php/php_form_url_email.asp PHP 表单验证 - 验证 E-mail 和 URL 本节展示如何验证名字.电邮 ...

  7. [转]Ubuntu系统下常用的新建、删除、拷贝文件命令

    我们在Ubuntu系统中安装程序时,经常要在usr目录下新建.拷贝文件,此文件夹在Linux类系统中需要root权限才能访问,因此用常规的鼠标右键菜单操作是无效的,今天分享一下在终端中使用命令新建.拷 ...

  8. ElasticSearch(1)-入门

    下一篇 Elastic Search基础(2) 相关文档: Gitbook[中文未完整]: http://learnes.net/ Gitbook[英文完整]:https://allen8807.gi ...

  9. Kettle jdbc连接hive出现问题

    jdbc连接时报如下错误: Error connecting to database [k] : org.pentaho.di.core.exception.KettleDatabaseExcepti ...

  10. iOS开发的一些奇巧淫技(转载)

    iOS开发的一些奇巧淫技 http://www.cocoachina.com/ios/20141229/10783.html iOS开发的一些奇巧淫技2 http://www.cocoachina.c ...