CF1180 B. Nick and Array

题目传送门:https://codeforces.com/problemset/problem/1180/B

题目大意:
给定一串长度为\(n\)的序列\(A\),每次操作可以任选一个数\(i(1\leqslant i\leqslant n)\),使得\(A_i=-A_i-1\),求在进行若干次操作后,使\(\prod\limits_{i=1}^nA_i\)最大的序列\(A\)


如果\(A_i\geqslant0\),则有\(|-A_i-1|>|A_i|\),故我们可以将所有的非负整数\(A_i\)都改为\(-A_i-1\)

\(n\)为奇数,则我们需要再将一个负整数\(A_i\)改为\(-A_i-1\)

\(S=\prod\limits_{i=1}^n|A_i|\),而对负整数的操作,会使得\(|A_i|\)变为\(|A_i|-1\),若我们对\(A_k\)进行操作,则会使\(S=S-\frac{S}{|A_k|}=S(\frac{|A_k|-1}{|A_k|})\),故我们找到最大的\(|A_k|\)进行操作即可

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
int A[N+10];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int n=read(0),Max=0,ID=0;
	for (int i=1;i<=n;i++){
		A[i]=read(0);
		if (A[i]>=0)	A[i]=-A[i]-1;
		if (Max<abs(A[i]))	Max=abs(A[i]),ID=i;
	}
	if (n&1)	A[ID]=-A[ID]-1;
	for (int i=1;i<=n;i++)	printf("%d%c",A[i],i==n?'\n':' ');
	return 0;
}
posted @ 2021-07-08 10:58  Wolfycz  阅读(13)  评论(0编辑  收藏  举报