AtCoder-ABC125D.Flipping Signs (thinking)

The original question is linked to the
meaning of the question:
For a sequence of length n, two adjacent numbers can be multiplied by -1 at the same time for each operation, and unlimited operations can be performed to ask the maximum value of the sum of the sequence.
Idea: The
number of operations is unlimited. If there are an even number of negative numbers, they can all become positive in the end; if there are an odd number of negative numbers, there can only be one negative number left.
Discuss the categories and record the sum of absolute values ​​and the number with the smallest absolute value.
Code:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){
    
    ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a,ll b,ll p){
    
    ll res=1;while(b){
    
    if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
#define PI acos(-1)
#define x first
#define y second
const int maxn=1e6+7;
ll a[maxn],n;
int main(){
    
    
    n=read;
    ll cnt=0,maxx=1e9,sum=0;
    rep(i,1,n){
    
    
        a[i]=read;
        if(a[i]<0) cnt++;
        maxx=min(maxx,abs(a[i]));
        sum=sum+abs(a[i]);
    }
    if(cnt%2==0) cout<<sum<<endl;
    else cout<<sum-2*maxx<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/114239224