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; ...
随机推荐
- c# 格式化百分比
代码示例: <%#Eval("total").ToString() == "0" ? "00.00%" : (Convert.ToDe ...
- site与subsite
1.List template只能加载在主站点上,加载在主站点上之后,其subsite也能引用: 2.发布webpart的时候,路径也只能写网站集的路径,发布到网站集之后其主站点和subsite都能引 ...
- Jenkins corbertura问题
最近在Jenkins上部署项目时遇到无法展示覆盖率测试报告的问题. build success后,出现配置的覆盖率报告存储位置not exists的失败问题,评估是Jenkins每次按照publish ...
- windows server 2003 系统重装蓝屏
错误码:0X0000007B 这个代码和硬盘有关系,不过不用害怕,不是有坏道了,是设置问题或者病毒造成的硬盘引导分区错误.如果您在用原版系统盘安装系统的时候出这个问题,那说明您的机器配置还是比较新的, ...
- sql Server 触发器 调用java.
在这里,通过 xp_cmdshell 调用java程序. 步骤: 1)开启 xp_cmdshell(sql Server 2008): 通过如下命令开启: -- To allow advanced o ...
- QueryString传值的加密与解密方法 .
//加密 Response.Redirect("DetailInfo.aspx?id=" + Convert.ToBase64String(System.Text.Encoding ...
- 使用java的Calendar对象获得当前日期的上几个度开始、结束时间
思路: 先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期 /** * @param flag true:开始日期:false:结束日期 * @return */ ...
- 关于iOS元旦http,https的规定,官方论坛回应
先贴原文地址:https://forums.developer.apple.com/thread/48979#146140 原文: eskimoAug 2, 2016 4:17 AM(in respo ...
- 171. Excel Sheet Column Number(C++)
171. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as ...
- php文件缓存
1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...