【题解】CF1180B:Nick and Array

原题传送门
若一个数 x > = 0 x>=0 ,则 x 1 > x |-x-1|>|x|
若一个数 x < 0 x<0 ,则 x 1 < x |-x-1|<|x|
所以把所有大于等于0的数变成负的,如果有奇数个数,乘积是负的
所以把绝对值最大的负数变成正的,易证最优

Code:

#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
int n, a[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
	n = read();
	for (int i = 1; i <= n; ++i){
		a[i] = read();
		if (a[i] >= 0) a[i] = -a[i] - 1;
	}
	if (n & 1){
		int p = 0;
		for (int i = 1; i <= n; ++i)
			if (a[p] > a[i]) p = i;
		a[p] = -a[p] - 1;
	}
	for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ModestCoder_/article/details/108251976