您的位置:首页 > 编程语言 > Go语言

codeforces Round #238(div2) d解题报告

2014-03-27 21:20 531 查看
D. Toy Sum

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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!

Input

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++.

Output

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.

Sample test(s)

input
3
1 4 5


output
2
999993 1000000


input
1
1


output
1
1000000


题目大意:

        有一个集合,包含了1~10^6,给出一个非空子集,求出另一个满足条件的非空子集并且两子集不能重叠.

        条件就是:


        求出任意一个满足上述条件的子集

解法:

       因为是构造任意一个,我们只需要构造得出来就够了,并且本题保证有解

       每个x都对应了一个y,是的 x-1=s-y,即x+y=s+1,那么我们只需要求出y并且y不在X集中就可以加入Y集.

       如若y也在X集中, (x-1)+(y-1) = x+y-2 = s-1, Y集这边就需要 (s-1) = (s-a1)+(s-a2)...(s-an)... = n*s-(a1+a2+...+an),然而最简单的形式就是 s-1 = 2*s-(a+b) 即 a+b = s+1

       那么,我们只需要寻找一对a+b=10^6+1且均不在X集即可.

注意:

        在寻找数的时候,只要符合不在X集的a+b就可以加入Y集,所以我们只需要按顺序枚举a即可

#include <cstdio>
#include <vector>
#define INF 1000000

using namespace std;

int n;
int a[INF+10];
bool vis[INF+10], pass[INF+10];
vector<int> ans;

void init() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vis[a[i]] = true;
}
}

void solve() {
int j = 1;

for (int i = 1; i <= n; i++) {
int tmp = INF+1-a[i];

if (pass[a[i]]) continue;

pass[a[i]] = true;
pass[tmp] = true;
if (!vis[tmp]) {
vis[tmp] = true;
ans.push_back(tmp);
}
else {
while (vis[j] || vis[INF+1-j]) j++;

ans.push_back(j);
ans.push_back(INF+1-j);
vis[j] = true;
vis[INF+1-j] = true;
}
}

printf("%d\n",ans.size());
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
}

int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);

init();
solve();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces algorithm math