Yahoo Programming Contest 2019 D - Ears
D - Ears
分析:
转化一下题意,将一段序列分成5段,空 偶 奇 偶 空,然后dp一下,f[i][j]表示到第i个数,当前在第j段内,的最小花费。
空段中的花费是数字大小,偶数段中奇数花费1,奇数段中偶数花费1。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; } const int N = 200005; int a[N]; LL f[N][5]; int main() { int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1; i <= n; ++i) { f[i][0] = f[i - 1][0]; for (int j = 1; j <= 4; ++j) f[i][j] = min(f[i - 1][j], f[i][j - 1]); f[i][0] += a[i]; f[i][1] += (a[i] == 0) ? 2 : (a[i] & 1); f[i][2] += !(a[i] & 1); f[i][3] += (a[i] == 0) ? 2 : (a[i] & 1); f[i][4] += a[i]; } LL ans = f[n][0]; for (int i = 1; i <= 4; ++i) ans = min(ans, f[n][i]); cout << ans; return 0; }