您的位置:首页 > 产品设计 > UI/UE

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奇数偶数讨论,但是始终不知从哪里下手,没有进行深入分析,所以没有得到答案。最后还是问了别人,这样的思维还是不习惯。

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: