Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

CodeForecs 301A Yaroslav and Sequence

$
0
0

题目地址: http://codeforces.com/problemset/problem/301/A


A. Yaroslav and Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

In a single line print the answer to the problem — the maximum sum that Yaroslav can get.

Sample test(s)
input
2
50 50 50
output
150
input
2
-1 -100 -1
output
100
Note

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.



思路:

1、当n为奇数的时候,变换n个可以一次增加或减少1个负数,这样可以直到有n个负数,然后全变为整数ok

2、当n为偶数的时候,变换n个可以一次增加或减少2个负数,所以当有偶数个负数时都可以变为正数,当有奇数个负数时,最大和为(绝对值最小的那个为负数,其余的都为正数)。



代码如下:

#include<iostream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

typedef long long LL;
const int N=201000;

int main()
{
    int i,j,T,t,n,m,a,b,sum;
    while(cin>>n)
    {
        sum=0;
        int xiao=0,min=0x7f7f7f7f;
        //max表示的是绝对值最小的数,xiao表示小于0的个数
        for(i=0;i<2*n-1;i++)
        {
            scanf("%d",&a);
            if(a<0)
            {
                xiao++;
                a=-a;
            }
            sum+=a;
            if(a<min)
                min=a;
        }
        if(n&1)
            printf("%d\n",sum);
        else
        {
            if(xiao&1)
                printf("%d\n",sum-2*min);
            else
                printf("%d\n",sum);
        }
    }
    return 0;
}



作者:ilovexiaohao 发表于2013-5-6 21:43:54 原文链接
阅读:30 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>