Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9856 | Accepted: 4786 |
Description
Input
Output
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 int dp[1005][40]; 9 int T, W; 10 int a[1005]; 11 12 int main() { 13 14 scanf("%d%d", &T, &W); 15 memset(dp, -1, sizeof(dp)); 16 dp[0][0] = 0; 17 for (int i = 0; i < T; ++i) { 18 scanf("%d", &a[i]); 19 } 20 21 for (int i = 1; i <= T; ++i) { 22 for (int j = 0; j <= W; ++j) { 23 int c = (0 + j) % 2 == a[i - 1] - 1 ? 1 : 0; 24 dp[i][j] = max(dp[i][j], dp[i - 1][j] + c); 25 if (j != 0) { 26 dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + c); 27 } 28 29 } 30 } 31 32 int ans = 0; 33 for (int i = 0; i <= W; ++i) { 34 ans = max(ans, dp[T][i]); 35 } 36 37 printf("%d\n", ans); 38 39 40 41 return 0; 42 }
原文:http://www.cnblogs.com/hyxsolitude/p/5103244.html