codeforces 405 d toy sum
2014-04-22 22:41
295 查看
题意:1-10^6中,挑出n个数,求出这n个数-1相加的和。现在要找另外的数,使得每个数与10^6相减的和与之前的和相等。
这个题真的很无语,开始分析得出结果是,将一个数分解为k个数,这k个数中不能出现之前的n个数。然后深搜,dp什么的那方向想。
然后发现,每个数只能出现一次,那么在数组中看起来就是对称的。也就是说,一个数和另一个数相加的和为10^6+1。
如果两个数中有一个出现在n中,那么另一个数就是答案。两个数都出现在n中,那么寻找另一对和为10^6+1的即可。
这个题真的很无语,开始分析得出结果是,将一个数分解为k个数,这k个数中不能出现之前的n个数。然后深搜,dp什么的那方向想。
然后发现,每个数只能出现一次,那么在数组中看起来就是对称的。也就是说,一个数和另一个数相加的和为10^6+1。
如果两个数中有一个出现在n中,那么另一个数就是答案。两个数都出现在n中,那么寻找另一对和为10^6+1的即可。
#include <cstdio> #include <cstring> const int N = 1000001; struct Node { int x; int flag; }; Node a ; int ok ; int ans ; int main() { int n; while(~scanf("%d",&n)) { int k=0; memset(ok,0,sizeof(ok)); memset(ans,0,sizeof(ans)); for(int i=0; i<n; i++) { scanf("%d",&a[i].x); ok[a[i].x]=1; if(ok[N-a[i].x]==1) a[i].flag=1; else a[i].flag=0; } int cc=1; for(int i=0; i<n; i++) { int tmp=N-a[i].x; if(a[i].flag==1) continue; if(ok[tmp]==0) { ans[k++]=tmp; ok[tmp]=1; } else { for(int j=cc; j<=N/2; j++) { if(ok[j]==0 && ok[N-j]==0) { ans[k++]=j; ans[k++]=N-j; ok[j]=1; ok[N-j]=1; cc=j; break; } } } } printf("%d\n",k); for(int i=0; i<k-1; i++) { printf("%d ",ans[i]); } printf("%d\n",ans[k-1]); } return 0; }
相关文章推荐
- Toy Sum(codeforces)
- CodeForces 703D Mishka and Interesting sum
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- Codeforces 276C Little Girl and Maximum Sum 线段树区间累加
- CodeForces - 276C Little Girl and Maximum Sum (扫描线)
- 【Codeforces 608B】Hamming Distance Sum(前缀和)
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
- codeforces CF703D Mishka and Interesting sum 树状数组
- 【codeforces 257D】Sum
- CodeForces 85 D.Sum of Medians(线段树)
- Codeforces 85 D. Sum of Medians
- H - The Child and Toy-CodeForces 437C-水题
- CodeForces 703D Mishka and Interesting sum(树状数组 区间异或)
- [题解]codeforces 280d k-Maximum Subsequence Sum
- 【codeforces 703 D】【离线询问 树状数组 前驱思想 前缀异或和】D. Mishka and Interesting sum【 区间内出现次数偶数的数的异或和】
- CodeForces - 276C Little Girl and Maximum Sum
- Codeforces 365Div2 D Mishka and Interesting sum
- codeforces#277.5 C. Given Length and Sum of Digits
- Codeforces 489C - Given Length and Sum of Digits...(贪心)