[Codeforces Round #160 (Div. 2)]B. Roma and Changing Signs

地址:http://codeforces.com/contest/262/problem/B

改正负k次,使得所有数字之和最大,数列是以非减形式给出

全为负数时,此时若n<k则只能更改最后一个数,原来是最大负数,现在变成了最小正数

并非全为负数,但n<k时,则要比较之前正负分界点两侧数字的大小,谁小就改谁

当然n<k时,也可以当数列全为正后来一次排序

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int a[100000],n,k;
 6 
 7 int main()
 8 {
 9     int i,sum=0,count;
10     cin>>n>>k;
11     for(i=0;i<n;i++)
12     {
13         cin>>a[i];
14         sum+=a[i];
15     }
16     count=k;
17     i=0;
18     while(count>0)
19     {
20         if(i>=n) i--;
21         if(a[i]>0)
22         {
23             if(count%2!=0 && i!=0) sum-=(2*(a[i-1]<a[i]?a[i-1]:a[i]));
24             else if(count%2!=0 && i==0) sum-=(2*a[i]);
25             break;
26         }
27         else
28         {
29             a[i]=0-a[i];
30             sum+=(2*a[i]);
31             i++;
32             count--;
33         }
34     }
35     cout<<sum<<endl;
36     return 0;
37 }

 

posted @ 2013-01-24 13:18  tjsuhst  阅读(189)  评论(0)    收藏  举报