codeforces #268 div2 D
对于这道题第一感觉是图论相关
然后我们先分析,假设a[i]在A集合需要的元素是a[x],在B集合是a[y]
那么假设a[i]在A集合,那必然a[x]也必须在A集合,由于a[y]如果在B集合就没有对应元素,则a[y]也一定在A集合
同理,当a[i]在B集合,a[x],a[y]一定也要在B集合
对此我们可以分析出,要想有解,那么a[i],a[x],a[y]必定在同一集合
这样我们可以构造出一堆堆团,每个团都只可能在唯一的集合
那这个团属于那个集合呢?假如这个团有一个元素a[j]它有一个不存在对应在A集合的元素,
那么这个团一定是在B集合的,否则反之;
这样我们也很容易想到什么时候无解,那一定是某个团中既存在一个元素不存在对应在A集合的元素,又存在一个元素不存在对应在B集合的元素
所以就很好解决了,我们用n+1,n+2分别表示不存在对应在A集合的元素,不存在对应在B集合的元素
不难想到用并查集维护团,然后搞定(其实不难,可惜现场nc没想出来)
var ans,a,fa,c:array[..] of longint;
p,q,x,i,n:longint; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function getf(x:longint):longint;
begin
if fa[x]<>x then fa[x]:=getf(fa[x]);
exit(fa[x]);
end; procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(c[i],c[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; function find(x:longint):longint;
var l,r,m:longint;
begin
l:=;
r:=n;
while l<=r do
begin
m:=(l+r) shr ;
if a[m]=x then exit(m);
if a[m]>x then r:=m-
else l:=m+;
end;
exit(-);
end; procedure union(x,y:longint);
var k1,k2:longint;
begin
k1:=getf(x);
k2:=getf(y);
if k1<>k2 then fa[k2]:=k1;
end; begin
readln(n,p,q);
for i:= to n do
begin
read(a[i]);
c[i]:=i;
fa[i]:=i;
end;
fa[n+]:=n+;
fa[n+]:=n+;
sort(,n); for i:= to n do
begin
x:=find(p-a[i]);
if x<>- then union(i,x)
else union(i,n+);
x:=find(q-a[i]);
if x<>- then union(i,x)
else union(i,n+);
end;
if getf(n+)=getf(n+) then
begin
writeln('NO');
halt;
end;
writeln('YES');
for i:= to n do
if getf(i)=getf(n+) then ans[c[i]]:= else ans[c[i]]:=;
for i:= to n do
begin
write(ans[i]);
if i<>n then write(' ');
end;
writeln;
end.
codeforces #268 div2 D的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- 新建oracle数据库表空间
1.新建表空间,注意是数据表空间,不是临时表空间 create tablespace CARD logging datafile 'C:\app\tablespace\CARD.DBF' //注 ...
- Java递归流程
递归二字顾名思义就是:递过去,归回来.所以我索性叫它做有借有还吧. 下面的例子由c而来: public class Main { publ ...
- PhotoShop常用快捷键(1)
一.工具栏工具 移动工具 [V] 矩形.椭圆选框工具 [M] 套索.多边形套索.磁性套索 [L] 快速选择.魔棒工具[W] 裁剪工具 [C] 吸管.颜色取样器 [I] 修补.污点修复[J] 画笔工具 ...
- UML图基本类型
use case model用例模型 analysiss model分析模型 design model设计模型 implementation model实现模型 deployment model部署模 ...
- SQL统计不重复字段的个数.
注意:下面的举例适用于ORCLE和MSSQL,不能在Access中使用. 语法 SELECT COUNT(DISTINCT column(s)) FROM table 举例 With this &qu ...
- struts通过Ajax返回数据时,例如对象类型,没有执行Ajax的回调函数
<result type="json" name="success"> <param name=" ...
- PL/SQL 中文转换为\uxxxx格式(unicode)
用plsql生成json的时候,经常要处理中文的情况. SELECT replace(asciistr('你好,,hello'),'\','\u') FROM dual 输出:\u4F60\u597D ...
- window远程连接linux
一.字符界面连接Linux 1.直接使用window自带的telnet. 2.但现在Linux一般都不启用telnet,而是启用ssh.这样的话,window就要安装客户端来访问Linux了.这 ...
- JavaScript 标准对象
在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...