题意:
给你一个X集合,让你构造Y集合,满足Y集合里的数不能在X集合里出现,并且对于X集合里的每一个数都有且X、Y集合里的数均小于10的6次方。
思路:
构造。枚举X里的元素,对于一个元素x,它能对应一个Y集合中的元素y,满足y=1e6-x+1,若这样的y没有出现在集合X里,那么取,如果出现在X集合里,那么先放在一边。
对于所有放在一边的元素,它们必定死偶数个,因为是成对的,那么接下来只需要枚举a满足a>=x,和计算b=1e6-a+1,然后判断是否已经加入了X、Y集合,若还没加入,那么取,否则继续枚举。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1e6+5;
int n,x[MAX],ans[MAX],other[MAX];
bool use[MAX];
int main(){
scanf("%d",&n);
memset(use,false,sizeof(use));
for(int i=0;i<n;i++){
scanf("%d",&x[i]);
use[x[i]]=true;
}
int cnt=0,num=0;
sort(x,x+n);
for(int i=0;i<n;i++){
int tmp=1e6-x[i]+1;
if(use[tmp]==false){
use[tmp]=true;
ans[cnt++]=tmp;
}
else{
other[num++]=x[i];
}
}
if(num){
sort(other,other+num);
int a=other[0]+1;
for(int i=0;i<num/2;i++){
while(a<=1e6){
int b=1e6+1-a;
if(use[a]==false&&use[b]==false){
ans[cnt++]=a;
ans[cnt++]=b;
use[a]=use[b]=true;
break;
}
a++;
}
}
}
sort(ans,ans+cnt);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
if(i!=0) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
return 0;
}