CF 182DIV2 C

Link: http://codeforces.com/contest/302/problem/C

C - Yaroslav and Sequence

大意:对一串数列 n + n - 1个数操作, 可对其中n个数字的取反, 操作可重复, 求若干操作后该数列最大和。

分析: 简单数学

 

f为数列中负数的个数

gao(f)

if  (f % 2 == 0)

  可对数列进行取反使数列全部为正数

else

     if  n % 2 == 0

          f 为奇数, n - f 为奇数, 必定会存在一个负数

    else

         n - f 为偶数  , gao(n-f)

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <cstring>
#include <cstdlib>
#include<queue>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    int mini = (1<<29);
    int sum = 0;
    int f = 0;
    for(int i = 0, x; i < n + n - 1; i++){
        cin >>x;
        if(x < 0)   x = - x, f++;
        mini = min(mini, x);
        sum += x;
    }
    if(f&1 && !(n&1)) sum -= 2*mini;
    cout << sum << endl;
}

  

 

posted @ 2013-05-06 20:37  Shamaoer  阅读(243)  评论(0)    收藏  举报