Yaroslav and Sequence
2014-07-31 19:07
232 查看
Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A
题意:给你2*n-1个数,你每次可以选择n个连续的数,把这个n个数*-1,问你进行若干次操作,这2*n-1个数之和最大是多少。
题解:结论很简单:当n是奇数的时候,那么你一次操作可以增加或者减少一个负数,所以这时候所有的负数都能变成整数。当n是偶数的时候,那么你一次操作可以增加或者减少2个负数,如果负数的个数是偶数,那么所有的负数都可以变成整数,否则,就会还剩下一个负数,显然这个负数必须是绝对值最小的那个数。所以,结果出来啦。
思维过程;一开始也分n奇数偶数讨论,但是始终不知从哪里下手,没有进行深入分析,所以没有得到答案。最后还是问了别人,这样的思维还是不习惯。
View Code
题意:给你2*n-1个数,你每次可以选择n个连续的数,把这个n个数*-1,问你进行若干次操作,这2*n-1个数之和最大是多少。
题解:结论很简单:当n是奇数的时候,那么你一次操作可以增加或者减少一个负数,所以这时候所有的负数都能变成整数。当n是偶数的时候,那么你一次操作可以增加或者减少2个负数,如果负数的个数是偶数,那么所有的负数都可以变成整数,否则,就会还剩下一个负数,显然这个负数必须是绝对值最小的那个数。所以,结果出来啦。
思维过程;一开始也分n奇数偶数讨论,但是始终不知从哪里下手,没有进行深入分析,所以没有得到答案。最后还是问了别人,这样的思维还是不习惯。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> using namespace std; int ans1,ans2,sum,temp; int a1,n; int main(){ while(~scanf("%d",&n)){ int ans1=ans2=100000000,sum=a1=0; for(int i=1;i<=2*n-1;i++){ scanf("%d",&temp); if(temp>0){ sum+=temp; ans1=min(ans1,temp); } else { sum+=abs(temp); ans2=min(ans2,-temp); a1++; } } if(n&1)printf("%d\n",sum); else if(a1&1){ printf("%d\n",sum-2*min(ans1,ans2)); } else { printf("%d\n",sum); } } }
View Code
相关文章推荐
- 【构造策略】-CF-301A-Yaroslav and sequence
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Yaroslav and Permutations
- CodeForces - 284C - Cows and Sequence
- CUIT ACM Personal Training 11.27(FM)D - Yaroslav and Permutations
- A. Yaroslav and Permutations
- codeforces 743 B Chloe and the sequence(递归)
- CF438D(The Child and Sequence-线段树mod x)
- Codeforces Round #384 B. Chloe and the sequence
- CodeForces 222A Shooshuns and Sequence
- hdoj 5504 GT and sequence 【脑子 抽了】
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence (构造 贪心)
- HDU 6065 RXD, tree and sequence 分析+01背包
- C.Interface.And.Implementations—sequence的实现
- Calling sequence for creating and configuring a table view
- HDU6065 RXD, tree and sequence (DP+LCA)
- [均摊复杂度线段树]Codeforces 438D. The Child and Sequence
- 【Codeforces 743 BChloe and the sequence 】
- Codeforces Round #384 B. Chloe and the sequence