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 ...
随机推荐
- vertx从入门到精通
1.Vert.x安装指南 http://blog.csdn.net/sdyy321/article/details/38926005 http://blog.csdn.net/chszs/articl ...
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...
- 【Linux】Ubuntu18.04镜像下载,新功能介绍
一.Ubuntu18.04镜像下载 官方下载地址:http://releases.ubuntu.com/18.04/ 官方64位iso下载地址:http://releases.ubuntu.com/1 ...
- bootstrap历练实例:标签式的导航菜单
本章将讲解bootstrap提供的用于定义导航元素的一些选项,它使用相同的标签和基类.nav.Bootsrtap也提供了一个用于共享标记和状态的帮助器类.改变修饰的class,可以在不同的样式间进行切 ...
- 洛谷 P2921 在农场万圣节
https://www.luogu.org/problemnew/show/P2921 开始感觉这题30行代码就可以搞定,还是太菜啦,还是乖乖地写了tarjan. 对图进行缩点,那么这个强联通分量中的 ...
- 【DB_MySQL】MySQL重要知识点
MySQL中的select语句 各子句的执行顺序 SELECT语句的处理过程 1. FROM 组装数据来源 2. WHERE筛选元组 3. GROUP BY 将满足条件的元组进行分组 4. HAVIN ...
- (1) zabbix进程构成
进程介绍 zabbix_agentd客户端守护进程,此进程收集客户端数据,例如cpu负载.内存.硬盘使用情况等 zabbix_getzabbix工具,单独使用的命令,通常在server或者proxy端 ...
- 文件操作-cd
cd命令是linux实际使用当中另一个非常重要的命令,本文就为大家介绍下Linux中cd命令的用法. 转载自 https://www.cnblogs.com/waitig/p/5880719.html ...
- input标签内容改变触发的事件
原生方法 onchange事件 <input type="text" onchange="onc(this)"> function onc(data ...
- clock gate
clock gate 这个专题,比较复杂设计DC PT PR.下面仅仅从RTL行为级说明一下.