题意:

给定n个正整数与a,b两个集合,求一种方案使得这n个数恰好被分在这两个集合中且集合中无多余的数且若x在a中则A-x在a中,若x在b中则B-x在b中。

题意理解了我好半天...

解法1:并查集。

把x, A - x,B - x(如果不存在B - x,x就不能放B集合)放入同一个并查集即可。

实现时注意一些乱七八糟的东西,大力讨论即可。

 #include <cstdio>
#include <map>
#include <cstring>
const int N = ; int a[N], ans[N], A, B, n, fa[N];
std::map<int, int> mp; inline int find(int x) {
if(x == fa[x]) {
return x;
}
return fa[x] = find(fa[x]);
}
inline bool check(int x, int y) {
return find(x) == find(y);
}
inline void merge(int x, int y) {
fa[find(x)] = find(y);
return;
} int main() {
memset(ans, -, sizeof(ans));
for(int i = ; i < N; i++) {
fa[i] = i;
}
scanf("%d%d%d", &n, &A, &B);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
mp[a[i]] = i;
}
for(int i = ; i <= n; i++) {
bool x = mp.find(A - a[i]) == mp.end();
bool y = mp.find(B - a[i]) == mp.end();
if(x && y) {
printf("NO");
return ;
}
else if(x) {
int &t = ans[mp[B - a[i]]];
if(t == ) {
printf("NO");
return ;
}
if(t == -) {
t = ;
}
ans[i] = ;
merge(i, mp[B - a[i]]);
}
else if(y) {
int &t = ans[mp[A - a[i]]];
if(t == ) {
printf("NO");
return ;
}
if(t == -) {
t = ;
}
ans[i] = ;
merge(i, mp[A - a[i]]);
}
else {
merge(i, mp[A - a[i]]);
merge(i, mp[B - a[i]]);
}
} for(int i = ; i <= n; i++) {
if(ans[i] == -) {
ans[i] = ans[find(i)];
}
else {
if(ans[find(i)] == -) {
ans[find(i)] = ans[i];
}
else if(ans[find(i)] != ans[i]) {
printf("NO");
return ;
}
}
}
printf("YES\n");
for(int i = ; i <= n; i++) {
if(ans[find(i)] == -) {
printf("0 ");
}
else {
printf("%d ", ans[find(i)]);
}
} return ;
}

AC代码

CF 468B Two Sets的更多相关文章

  1. CF 103E Buying Sets 最大权闭合子图,匹配 难度:4

    http://codeforces.com/problemset/problem/103/E 这道题首先一看就很像是最大权闭合子图,但是我们可以认为现在有两种点,数字和集合点,我们需要消除数字点的影响 ...

  2. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  3. 算法笔记--2-sat

    强连通分量的应用,详见<挑战程序设计>P324 例题1:HDU Peaceful Commission 思路:强连通分量分解,看有没有两个同一个国家的代表在一个强连通分量里,如果有,就是N ...

  4. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

  6. cf B. Eight Point Sets

    http://codeforces.com/contest/334/problem/B #include <cstdio> #include <cstring> #includ ...

  7. cf Two Sets (我用二分最大匹配做的)

    题意: n个数p1,p2....pn     两个数a,b 把它们分成A,B两个集合. 若x属于A,a-x一定属于A. 若x属于B,b-x一定属于B. 问是否可能将这n个数分成两个集合.若可以,输出每 ...

  8. CF下的BackgroudWorker组件优化.

    .net compact framwork(2.0/3.5)下没有Backgroundworder组件,在网上找了一个类 经过使用发现了一些问题,主要有一个问题:在一个Dowork事件中对Report ...

  9. spark MLlib 概念 4: 协同过滤(CF)

    1. 定义 协同过滤(Collaborative Filtering)有狭义和广义两种意义: 广义协同过滤:对来源不同的数据,根据他们的共同点做过滤处理. Collaborative filterin ...

随机推荐

  1. Quartz.net 定时任务之储存与持久化和集群(源码)

    一.界面 1.这篇博客不上教程.直接看结果(包括把quartz任务转换成Windows服务) (1).主界面 (2).添加任务(默认执行) (3).编辑(默认开启) (4).关闭和开启 2.代码说明 ...

  2. 利用卷积神经网络(VGG19)实现火灾分类(附tensorflow代码及训练集)

    源码地址 https://github.com/stephen-v/tensorflow_vgg_classify 1. VGG介绍 1.1. VGG模型结构 1.2. VGG19架构 2. 用Ten ...

  3. 浏览器数据库 IndexedDB 入门

    一.概述 随着浏览器的功能不断增强,越来越多的网站开始考虑,将大量数据储存在客户端,这样可以减少从服务器获取数据,直接从本地获取数据. 现有的浏览器数据储存方案,都不适合储存大量数据:Cookie 的 ...

  4. C_数据结构_递归实现累加

    # include <stdio.h> long sum(int n) { //用递归实现: ) ; else ) + n; /* 用for循环实现: long s = 0; int i; ...

  5. 回溯法解n皇后问题

    #include<bits/stdc++.h> using namespace std; int n,sum; int c[100]; void search(int cur){ if(c ...

  6. 『编程题全队』Beta 阶段冲刺博客二

    1.提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID) (1) 昨天已完成的工作 孙志威: 1.添加了SubTask类,完成基本UI 2.为SubTask类添加了展开/收缩 ...

  7. Vert.x简介

    https://vertx.io/ https://vertx.io/download/ https://baike.baidu.com/item/Vert.x 近年来,移动网络.社交网络和电商的兴起 ...

  8. zabbix 使用问题两个--中文乱码,以及监控ESXi下的虚拟机

    1. 中文乱码 最开始中文显现 长方形不显示文字.解决办法: c:\windows\fonts 下面复制 楷体的字体(字体随意看自己喜欢) 文件名一般为: simkai.ttf 2.将simkai.t ...

  9. 【Linux】工作管理

    在进行工作管理的行为中,其实每个工作都是目前bash的子进程,即彼此间是有相关性的.我们无法以job control的方式由tty1的环境去管理tty2的bash 当只有一个终端时,可以出现提示符让你 ...

  10. matplotlib之直接保存图片

    自动保存图表:pyplot.savefig('D:\\pic.png'),替代了 pyplot.show(). # 使用matplotlib.pyplot.scatter绘制散点 import mat ...