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.
n
Odd
wish to setn=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 negativen/2
. Series becomes1,1,1,1,1,-1,-1,-1,-1
. Then choosen/2+1
a positive number,n/2
a negative number, you can add a negative number. The example is to select3
a1
and2
a-1
. Series becomes1,1,1,1,-1,-1,-1,-1,-1
. Then step into a totally positive.n
It is even
ideas and odd, as is differentn
division, n is only dividedn/2-1
andn/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