cf468B Two Sets
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.
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).
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).
4 5 9
2 3 4 5
YES
0 0 1 1
3 3 4
1 2 4
NO
It's OK if all the numbers are in the same set, and the other one is empty.
如果A中有了一个x,那么A中也要有a-x,说明(逆否命题)如果A中没有a-x,也就没有x。即如果B中有a-x,就有x。因为不在A就在B咯
所以这个A还是B无所谓的,重要的是x和a-x一定在同一集合,x和b-x一定在同一集合
因此裸并查集,只要被并起来的数字有一个不能在A,那么这一群都不能在A
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL n,a,b;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct po{LL x;int rnk;}p[];
bool operator <(po a,po b){return a.x<b.x;}
int fa[];
int mrk[];
inline int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
map<LL,LL>mp;
int main()
{
n=read();a=read();b=read();
for (int i=;i<=n;i++)
{
p[i].x=read();
p[i].rnk=i;
}
sort(p+,p+n+);
for(int i=;i<=n;i++)mp[p[i].x]=p[i].rnk,fa[i]=i,mrk[i]=;
for(int i=;i<=n;i++)
{
if (mp[a-p[i].x])
{
int pos=getfa(mp[a-p[i].x]),pos2=getfa(p[i].rnk);
if (pos!=pos2)fa[pos2]=pos;
}
if (mp[b-p[i].x])
{
int pos=getfa(mp[b-p[i].x]),pos2=getfa(p[i].rnk);
if (pos!=pos2)fa[pos2]=pos;
}
}
for (int i=;i<=n;i++)
{
int ff=getfa(p[i].rnk);
if (!mp[a-p[i].x])mrk[ff]&=;
if (!mp[b-p[i].x])mrk[ff]&=;
}
for (int i=;i<=n;i++)
if (mrk[getfa(i)]==){puts("NO");return ;}
else if (mrk[getfa(i)]==)mrk[getfa(i)]=;
puts("YES");
for (int i=;i<=n;i++)printf("%d ",mrk[getfa(i)]==?:);
puts("");
}
cf468B
cf468B Two Sets的更多相关文章
- TSQL 分组集(Grouping Sets)
分组集(Grouping Sets)是多个分组的并集,用于在一个查询中,按照不同的分组列对集合进行聚合运算,等价于对单个分组使用“union all”,计算多个结果集的并集.使用分组集的聚合查询,返回 ...
- grouping sets从属子句的运用
grouping sets主要是用来合并多个分组的结果. 对于员工目标业绩表'businessTarget': employeeId targetDate idealDistAmount 如果需要分别 ...
- Codeforces 722D. Generating Sets
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【转】rollup、cub、grouping sets、grouping、grouping_id在报表中的应用
摘自 http://blog.itpub.net/26977915/viewspace-734114/ 在报表语句中经常要使用各种分组汇总,rollup和cube就是常用的分组汇总方式. 第一:gro ...
- salesforce 零基础学习(十九)Permission sets 讲解及设置
Permission sets以及Profile是常见的设置访问权限的方式. Profile规则为'who see what'.通过Profile可以将一类的用户设置相同的访问权限.对于有着相同Pro ...
- Python数据类型之“集合(Sets)与映射(Mapping)”
一.集合类型(Sets) 集合对象是不同的(不可重复)hashable对象的无序集合.常见用法包括:成员关系测试.移除序列中的重复.以及科学计算,例如交集.并集.差分和对称差分.通俗点来说,集合是一个 ...
- 【Swift学习】Swift编程之旅---集合类型之Sets(七)
Sets是存储无序的相同类型的值,你可以在顺序不重要的情况下使用Sets来替代数组,或者当你需要同一个值在集合中只出现一次时. 一.Sets类型语法 写作Set<Element>,Ele ...
- Support for multiple result sets
https://blueprints.launchpad.net/myconnpy/+spec/sp-multi-resultsets Calling a stored procedure can p ...
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- VS远程调试虚拟机中的程序
1. 设置VS项目属性 => 调试页 例子如下 远程命令: C:\test.exe 工作目录 : C:\ 远程服务器名称: 192.168.xx.xx 查看网络共享 => 本地连 ...
- NBUT 1117 Kotiya's Incantation(字符输入处理)
题意: 比较两个串,有三种情况:完全相同,可见字符相同,不同.每个字符串以'-'结尾.难点在输入. 思路: 字符逐个读入,直到'-'为止,读出两串就可以直接进行判断.如果不足两串则结束.输入时需要注意 ...
- codevs 5462 HYY迎亲I
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 青铜 Bronze 题目描述 Description HYY要娶山那头的JCH,可毕竟是山路,十分崎岖,他又十分的单(bai)纯(ch ...
- Words Prefixed Trans-
transit v. Pass across or through (an area) The new large ships will be too big to transit the Panam ...
- dp 20190618
C. Party Lemonade 这个题目是贪心,开始我以为是背包,不过也不太好背包,因为这个L都已经是1e9了. 这个题目怎么贪心呢?它是因为这里有一个二倍的关系,所以说val[i]=val[i- ...
- shell脚本,编程题练习。
题目是:将 文件file为 b+b+b+b+b+b+b+b 变为 b+b=b+b=b+b=b+b 解答方法如下:
- oracle count 百万级 分页查询记要总数、总条数优化
oracle count 百万级 分页查询记录总数.总条数优化 oracle count 百万级 查询记录总数.总条数优化 最近做一个项目时,做分页时,发现分页查询速度很慢,分页我做的是两次查询,一次 ...
- 自写小函数处理 javascript 0.3*0.2 浮点类型相乘问题
const reg = /^([-+]?)([0-9]+)\.([0-9]*)$/; // 判断是不是浮点数 const isFloat = function(number){ return reg. ...
- CentOS7支持中文显示
1.查看系统是否安装有中文语言包 locale -a | grep "zh_CN" 命令含义:列出所有可用的公共语言环境的名称,包含有"zh_CN" 若 ...
- (56)zabbix Screens视图配置
screen翻译成中文为“屏幕”,在超市.单位等等地方都比较常见到监控视频,视频上有多块小视频,实际上zabbix screen和这个功能类似.你可以设置多个screen,每个screen可以显示特定 ...