AtCoder——ABC125D.Flipping Signs(思维)

原题链接
题意:
给一个长度为n的序列,每次操作可以将相邻的两个数同时乘-1,可以做无限次操作,问序列之和的最大值。
思路:
操作次数是无限的,如果有偶数个负数,最后可以都变成正的;如果有奇数个负数,最后可以只剩下一个负数。
分类讨论记录一下绝对值的和以及绝对值最小的数。
代码:

#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_45675097/article/details/114239224