题意:给出一串序列,可以进行两种操作:将序列数减1或加1,每种操作消耗1个费用。为了使序列所有数乘积为1,求出消耗最少费用
思路:对于正数,直接将其减到1即可。对于负数,当数量为偶数时,将其减到-1即可;数量为奇数时,分两种情况,序列中有0和没0,有0则可以将其中一个0减为-1,没0则需要将其中一个负数加到1,这个负数取负数中最大值使得加上的数最小即可。昨天没做出来主要两个原因:整数溢出,还有需要保证奇数负数没0时只减了一次。总的来说还是简单题目,还是太粗心了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f;
ll n, a[maxn];
int main()
{
while (cin >> n) {
ll ans = 0, cnt = 0, cnt2 = 0, maxx = -1e9;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] < 0) {
cnt++;
maxx = max(maxx, a[i]);
}
if (a[i] == 0)
cnt2++;
}
int flag =