博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
阅读量:5310 次
发布时间:2019-06-14

本文共 3774 字,大约阅读时间需要 12 分钟。

题目链接:

题目

time limit per test:1 second
memory limit per test:256 megabytes

问题描述

Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.

There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds:

"Are you kidding me?", asks Chris.

For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7.

However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y!

输入

The first line of input contains a single integer n (1 ≤ n ≤ 5·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 106), the numbers of the blocks in X.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

输出

In the first line of output print a single integer m (1 ≤ m ≤ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≤ yi ≤ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi ≠ yj for all i, j (1 ≤ i ≤ n; 1 ≤ j ≤ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.

样例

input

3
1 4 5

output

2
999993 1000000

input

1
1
output
1
1000000

题意

给你一个大小为n的X集合x1,x2,...,xn,问你能不能选定一个集合大小S(S<=10^6),使得从S包含X,并且从属于S但不属于X的元素组成的集合里,选出一个大小为k的非空子集Y,使得等式sigma(xi-1)==sigma(s-yj),其中1<=i<=n,1<=j<=k。

题解

1、暴搜,在属于S但不属于X的元素组成的集合里暴搜出一个满足答案的解。跑了717ms。(暴搜方案,每个数选或不选)

#include
#include
#include
#include
#include
using namespace std;typedef __int64 LL;const int maxn=5e5+10;const int INF=1e6;int arr[maxn];int n;map
mp; vector
ans;vector
re;bool dfs(int cur,LL sum){ if(sum==0) return true; int pos=lower_bound(re.begin(),re.end(),sum)-re.begin(); if(pos>cur) pos=cur; if(pos<=0) return false; ans.push_back(INF-re[pos]); if(dfs(pos-1,sum-re[pos])) return true; ans.pop_back(); if(dfs(pos-1,sum)) return true; return false;}int main(){ scanf("%d",&n); LL sum=0; for(int i=0;i

2、找对称的数,对于x,有y=S+1-i,使得x-1=S-y;所以对于xi,如果与它对称的数yi不在X里面,则吧yi放到答案里面,如果在,则这两个数相加为S,我们只要另找一个数不在X里面,并且它对称的数也不在X里面,然后吧这两个数都加到答案里面就可以了。

#include
#include
#include
#include
#include
using namespace std;const int maxn = 1e6 + 10;const int S = 1e6;int used[maxn];int main() { memset(used, 0, sizeof(used)); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); used[x] = 1; } vector
ans; //cnt统计一个数和与它对称的数都在X里面的数对 int cnt = 0; for (int i = 1; i <= S; i++) { if (used[i]) { int j = S + 1 - i; if (!used[j]) { ans.push_back(j); used[j] = 1; } else { if(i<=j) cnt++; } } } for (int i = 1; i <= S&&cnt>0; i++) { if (!used[i]) { int j = S + 1 - i; ans.push_back(i); ans.push_back(j); used[i] = used[j] = 1; cnt--; } } sort(ans.begin(), ans.end()); printf("%d\n", ans.size()); for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]); printf("%d\n",ans[ans.size()-1]); return 0;}

转载于:https://www.cnblogs.com/fenice/p/5678398.html

你可能感兴趣的文章
排序算法小结
查看>>
Android Core
查看>>
给C#学习者的建议 - CLR Via C# 读后感
查看>>
Recover Binary Search Tree
查看>>
【遇见时光】笔试-偶数最大距离-java
查看>>
【AC自动机】bzoj4327: JSOI2012 玄武密码
查看>>
jquery 判定checkbox是否选中
查看>>
FOJ Problem 2257 Saya的小熊饼干
查看>>
js分享2
查看>>
Java 实践:生产者与消费者
查看>>
笛卡尔积和卡氏积
查看>>
看图说话,jquery中的width innerwidth outerwidth
查看>>
[INS-30060]:Check for group existence failed
查看>>
javaIO详解、包含文件以及流操作
查看>>
如何使用Leangoo自动生成燃尽图
查看>>
[转]IOCP--Socket IO模型终结篇
查看>>
为什么需要ObjectDataProvider
查看>>
Eclipse(非J2EE版本)配置Extjs环境以及安装部署Tomcat
查看>>
网页中搜索框效果原理。
查看>>
SpringBoot实战 之 异常处理篇
查看>>