1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <cstdio> #include <algorithm> #include <iostream>
using namespace std;
int d[1000][32][2]; int T, W; int apple[1000];
int mov(int a) { if ((a & 0x1) == 0) return 0; return 1; }
int main() { scanf("%d %d", &T, &W); int i, j, k; for(i=0;i<T;++i) { scanf("%d", &apple[i]); apple[i]--; } if (apple[0] == 0) { d[0][0][0] = 1; d[0][1][1] = 0; } else { d[0][0][0] = 0; d[0][1][1] = 1; }
for(i=0;i<T-1;++i) { for(j=0;j<=W;++j) { if (mov(j) == apple[i+1]) { d[i+1][j][mov(j)] = max(d[i+1][j][mov(j)], d[i][j][mov(j)] + 1); d[i+1][j+1][mov(j+1)] = max(d[i+1][j+1][mov(j+1)], d[i][j][mov(j)]); } else { d[i+1][j+1][mov(j+1)] = max(d[i+1][j+1][mov(j+1)], d[i][j][mov(j)] + 1); d[i+1][j][mov(j)] = max(d[i+1][j][mov(j)], d[i][j][mov(j)]); } } } cout << *max_element(d[T-1][0], d[T-1][0] + 2*(W+1)) << endl; return 0; }
|