CF405D Toy Sum

刚看见的时候貌似是一道很难做的题…题面十分的玄学,不能重选的要求也十分难搞…

但是,可以发现N<=5*105从中可以不难发现一旦不能直接取时便会一定有一对正好和为1e6-1

#include<bits/stdc++.h>
using namespace std;
const int maxS=1000005;
int a[maxS],b[maxS],answer[maxS],S,N;
bool boo[maxS];
int main()
{
    S=1000000;
	scanf("%d",&N);
	int i;
	for(i=1;i<=N;i++)
	{
		scanf("%d",&a[i]);
		boo[a[i]]=1;//不能选了
	}
	int cnt=0;
	int full=0;//成对不能选的个数
	for(i=1;i<=N;i++)
	if(!boo[S-a[i]+1])//直接取
	{
		answer[++cnt]=S-a[i]+1;
		boo[S-a[i]+1]=1;
	}
	else
	full++;
	full/=2;//计算了两次..
	for(i=1;i<=(S+1)/2;i++)
	if(!boo[i]&&!boo[S-i+1])//成对可以取的
	{
		if(!full)break;//不需要了
		full--;
		answer[++cnt]=i;
		answer[++cnt]=S-i+1;
	}
	printf("%d\n",cnt);//输出个数
	for(i=1;i<=cnt;i++)
	printf("%d ",answer[i]);//输出序列
	return 0;
}

算是一道小清新的构造题…吧…

发布了72 篇原创文章 · 获赞 22 · 访问量 5533

猜你喜欢

转载自blog.csdn.net/sxy__orz/article/details/96490149
今日推荐