Description
Given a sequence of integers, each operation may be one element of a value of +1 or -1, the minimum number of seek operations may cause the product sequence requires each element is equal to 1
Solution
dp
Some of the product of the number equal to 1, a certain sequence described containing only 1 or -1, and -1 must be an even number of occurrences
Then we define $ f [i] [1/0] $ $ I $ denotes the number of front, a total of odd / even smallest operand is the number -1
So we consider the transfer
-1 if the number of times we let the current number becomes 1, then the transition from stage i-1 to i parity unchanged
Similarly, becomes -1 means that the parity change
Then the state transition equation is very simple
Code
#include <bits/stdc++.h> namespace shl { typedef long long ll; int n, a[100010]; ll f[100010][3]; int c[100010][3]; inline int read() { int ret = 0, op = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') op = -1; c = getchar(); } while (isdigit(c)) { ret = (ret << 3) + (ret << 1) + c - '0'; c = getchar(); } return ret * op; } inline int aabs(int x) { return x >= 0 ? x : -x; } using std :: min; int main() { n = read(); for (register int i = 1; i <= n; ++i) a[i] = read(); for (register int i = 1; i <= n; ++i) { c[i][0] = aabs(a[i] + 1); // change into -1 c[i][1] = aabs(a[i] - 1); // change into 1 // std :: cerr << c[i][0] << " " << c[i][1] << std :: endl; } f[0][1] = 1ll << 60; for (register int i = 1; i <= n; ++i) { f[i][0] = min(f[i - 1][0] + c[i][1], f[i - 1][1] + c[i][0]); f[i][1] = min(f[i - 1][1] + c[i][1], f[i - 1][0] + c[i][0]); } printf("%I64d\n", f[n][0]); return 0; } }; int main() { shl :: main(); return 0; }