Yaroslav and Sequence - CodeForces 301A 水题
来源:互联网 发布:angularjs js cdn 编辑:程序博客网 时间:2024/04/28 18:20
Yaroslav has an array, consisting of (2·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.
Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?
Help Yaroslav.
The first line contains an integer n (2 ≤ n ≤ 100). The second line contains (2·n - 1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.
In a single line print the answer to the problem — the maximum sum that Yaroslav can get.
250 50 50
150
2-1 -100 -1
100
In the first sample you do not need to change anything. The sum of elements equals 150.
In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.
思路:其实每次可以把任意两个的符号改变,那么就要考虑n是奇数还是偶数了,如果n是奇数(或n是偶数且其中有偶数个负数),那么可以让所有数变成非负数,如果n是偶数且有奇数个负数,那么必须剩一个负数。另外考虑有0的情况。
AC代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<vector>using namespace std;vector <int> vc1,vc2;void solve(){ int T,t,n,m,i,j,k,len1,len2,sum=0,ret,num0=0; scanf("%d",&n); for(i=1;i<=n*2-1;i++) { scanf("%d",&k); if(k>0) vc1.push_back(k); else if(k<0) vc2.push_back(k); else num0++; } len1=vc1.size(); len2=vc2.size(); for(i=0;i<len1;i++) sum+=vc1[i]; for(i=0;i<len2;i++) sum-=vc2[i]; if(num0>0 || len1==n*2-1 || n&1) { printf("%d\n",sum); return; } if(n%2==0 && len2%2==0) { printf("%d\n",sum); return; } ret=100000; for(i=0;i<len1;i++) ret=min(ret,vc1[i]); for(i=0;i<len2;i++) ret=min(ret,-vc2[i]); printf("%d\n",sum-2*ret);}int main(){ solve();}
- Yaroslav and Sequence - CodeForces 301A 水题
- codeforces 301 A Yaroslav and Sequence
- 【CF】CodeForces 301A Yaroslav and Sequence
- codeforces 301a Yaroslav and Sequence
- Codeforces 301A Yaroslav and Sequence 【思维】
- A. Yaroslav and Sequence
- CodeForecs 301A Yaroslav and Sequence
- CodeForces 301A Yaroslav and Sequence(数字变号的最大和)
- 【构造策略】-CF-301A-Yaroslav and sequence
- CodeForces 296A Yaroslav and Permutations
- codeforces301A. Yaroslav and Sequence
- codeforces 301D Yaroslav and Divisors
- codeforces 301B. Yaroslav and Time
- Yaroslav and Time - CodeForces 301B Dijkstra
- codeforces 301D Yaroslav and Divisors
- CodeForces 301B Yaroslav and Time
- A. Yaroslav and Permutations
- Codeforces Round #182 (Div. 2)---C. Yaroslav and Sequence(不错的题,分析找规律)
- hdoj 4906 状态dp
- Android实现无线调试自己的应用
- 基于单片机的语音终端设计
- windows已在 XXX.exe 中触发断点
- Little Pony and Sort by Shift
- Yaroslav and Sequence - CodeForces 301A 水题
- 软件测试与质量控制
- 简介→_→
- burpsuite学习
- 探究计算机C语言编辑程序技巧
- Gdiplus的坑:通过DrawImage进行放大不是很靠谱
- Little Pony and Expected Maximum
- FFmpeg MP3解码器的研究与优化
- 数字心电管理系统的探讨与设计