0
点赞
收藏
分享

微信扫一扫

codeforces - 1455A 思维题

原题链接:https://codeforces.com/problemset/problem/1455/Aicon-default.png?t=LBL2https://codeforces.com/problemset/problem/1455/A

翻译:

 思路:我们可以发现规律:有奇数个负数翻转后一定会留下一个负数,偶数个可全部翻转为正数。而0可正可负,所以有0即可全部为正。

代码实现:

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll st[100005];

int main()
{
    int N,j = 0,flag = 0;
    ll m = 0,num = 0;
    cin >> N;
    if(N == 2){  //特殊情况
        ll s,b;
        cin >> s >> b;
        num = max(s + b, -s - b);
        cout << num << endl;
    }
    else{
    for(int i = 0; i < N; i++)
    {
        scanf("%lld",&st[i]);
        if(st[i] < 0){
                st[i] = -st[i];
        }
        num += st[i];
        if(st[i] == 0){
            flag = 1;
        }
    }
    sort(st,st + N);
    if(flag == 1 || j % 2 == 0){
        cout << num << endl;
    }
    else{
        cout << num - 2*st[0] << endl;
    }
}

    return 0;
}
举报

相关推荐

0 条评论