Codeforces 301A. Yaroslav

Codeforces Tutorial

A. Yaroslav and Sequence

Problem Analysis

1, when n is an odd number, the n conversion can be increased or decreased once a negative number, which can have up to n-th negative, then all become OK integer of
2, when n is an even number, one can increase the conversion of n negative or reduced by 2, so when there is an even number of negative can become positive, negative when there is an odd number, and is the maximum (minimum absolute value that is negative, and the rest is a positive number).

With examples to explain the contents of the above.

  • nOdd
    wish to set n=5, sequence -1,-1,-1,-1,-1,-1,-1,-1,-1. Can be found, it can be done such that always less than the number of negative n/2. Series becomes 1,1,1,1,1,-1,-1,-1,-1. Then choose n/2+1a positive number, n/2a negative number, you can add a negative number. The example is to select 3a 1and 2a -1. Series becomes 1,1,1,1,-1,-1,-1,-1,-1. Then step into a totally positive.
  • nIt is even
    ideas and odd, as is different ndivision, n is only divided n/2-1andn/2+1

Acepted Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<istream>
#include<cassert>
#include<set>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<" = "<<x<<" , "\
<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
const int MAXN=300;
int n;
int a[MAXN];
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int ii=0;ii<2*n-1 ;ii++ ){
        scanf("%d",&a[ii]);
    }
    int ans=0;
    if(n%2){
        for(int ii=0;ii<2*n-1 ;ii++ ){
            ans+=abs(a[ii]);
        }
    }
    else {
        int cnt=0;
        int absmin=1e9;
        for(int ii=0;ii<2*n-1 ;ii++ ){
            if(a[ii]<0)cnt++;
            absmin=min(absmin,abs(a[ii]));
            ans+=abs(a[ii]);
        }
        if(cnt%2)ans-=2*absmin;
    }
    printf("%d\n",ans);
}

Wrong Answer Cases

What I Learn

  • More parity encountered

Reference

https://blog.csdn.net/xh_reventon/article/details/8892546

Guess you like

Origin www.cnblogs.com/MalcolmMeng/p/10945531.html
301