codeforces D. Toy Sum 解题报告

题目链接:http://codeforces.com/problemset/problem/405/D

题目意思:从 1 ~ 1000000 中选择 n 个数:x1,x2,...,xn,对 x1-1,x2-1,...xn-1 求和得s1。然后在 1 ~ 1000000 中除已经选择过的n个数中选择一些数,假设为y1, y2,...ym,设s = 1000000,对s-y1,s-y2,...,s-ym求和,如果这个和与s1相等,则输出y1,y2,...,ym

     可以这样想,由于集合X中:x1,x2,...,xn 是各不相同的,那么在S - X,设为Y(假定S是全集:1,2,...,n)对每个数xi(i : 1 ~ n)一定有相应的s-i+1与之对应(前提是,如果S-xi不在集合X中);如果有相应的s-xi+1在X中,那么可以找没有选择过的yj,s-yj+1来替换xi, s-xi+1。例如X中有 100, 999901而没有99, 999902,那么可以选择99, 999902来替代。效果是相同的。这样Y中的数量跟n是相同的。

     

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1e6;
 8 int x[maxn];
 9 
10 int main()
11 {
12     int n, i, j, t;
13     while (scanf("%d", &n) != EOF)
14     {
15         memset(x, 0, sizeof(x));
16         for (i = 0; i < n; i++)
17         {
18             scanf("%d", &t);
19             x[t] = 1;
20         }
21         printf("%d\n", n);
22         int cnt = 0;
23         for (i = 1; i <= maxn; i++)
24         {
25             if (x[i] && !x[maxn-i+1])
26             {
27                  printf("%d ", maxn-i+1);
28                  cnt++;
29             }
30         }
31         for (i = 1; i <= maxn && cnt != n; i++)
32         {
33             if (!x[i] && !x[maxn-i+1])
34             {
35                 printf("%d %d ", i, maxn-i+1);
36                 cnt += 2;
37             }
38         }
39         printf("\n");
40     }
41     return 0;
42 }

 

     

 

posted @ 2014-03-23 16:43  windysai  阅读(434)  评论(0编辑  收藏  举报