codeforces Toy Sum

题意:给你x个集合的数,然后根据求y集合的数。

思路:根据对称性,先找出对称出现的个数cnt,然后对称位置的中如果出现一个的把另一个加入到y集合中,再找出cnt个对应位置都不出现的加入到y集合中。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <set>
 4 #include <algorithm>
 5 #define ll long long
 6 #define maxn 5000100
 7 using namespace std;
 8 
 9 int n;
10 int x[maxn];
11 bool vis[maxn];
12 
13 int main()
14 {
15     scanf("%d",&n);
16     ll s=1000000;
17     set<int>q;
18     set<int>::iterator it;
19     int cnt=0;
20     for(int i=1; i<=n; i++)
21     {
22         scanf("%d",&x[i]);
23         vis[x[i]]=true;
24     }
25     for(int i=1; i<=s/2; i++)
26     {
27         int xx=s-(i-1);
28         if(vis[i]&&vis[xx])
29         {
30             cnt++;
31         }
32     }
33     int j=1;
34     ll cc=s-(j-1);
35     while(j<=s/2)
36     {
37         if((!vis[j])&&vis[cc])
38         {
39             q.insert(j);
40         }
41         else if(vis[j]&&(!vis[cc]))
42         {
43             q.insert(cc);
44         }
45         j++;
46         cc=s-(j-1);
47     }
48     j=1;
49     cc=s-(j-1);
50     int t1=0;
51     while(j<=s/2&&t1<cnt)
52     {
53         if((!vis[j])&&(!vis[cc]))
54         {
55             q.insert(j);
56             q.insert(cc);
57             t1++;
58         }
59         if(t1==cnt) break;
60         j++;
61         cc=s-(j-1);
62     }
63     printf("%d
",(int)q.size());
64     for(it=q.begin(); it!=q.end(); it++)
65     {
66         printf("%d ",(*it));
67     }
68     printf("
");
69     return 0;
70 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4274167.html