标签:dp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8062 | Accepted: 3951 |
Description
Input
Output
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
Source
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cstdlib> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int dp[1010][33][2]; int app[1010]; int main() { int t, w; while (~scanf("%d%d", &t, &w)) { for (int i = 1; i <= t; ++i) { scanf("%d", &app[i]); } memset (dp, 0, sizeof(dp)); for (int i = 1; i <= t; ++i) { dp[i][0][0] = dp[i - 1][0][0] + (app[i] == 1 ? 1 : 0); dp[i][0][1] = dp[i - 1][0][1] + (app[i] == 2 ? 1 : 0); for (int j = 1; j <= (i - 1 > w ? w : i - 1); ++j) { dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j - 1][1]) + (app[i] == 1 ? 1 : 0); dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0]) + (app[i] == 2 ? 1 : 0); } } int ans = 0; for (int i = 0; i <= w; ++i) { ans = max(ans, max(dp[t][i][0], dp[t][i][1])); } printf("%d\n", ans); } return 0; }
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cstdlib> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int dp[2][33][2]; int app[1010]; int main() { int t, w; while (~scanf("%d%d", &t, &w)) { for (int i = 1; i <= t; ++i) { scanf("%d", &app[i]); } memset (dp, 0, sizeof(dp)); for (int i = 1; i <= t; ++i) { dp[i % 2][0][0] = dp[1 - i % 2][0][0] + (app[i] == 1 ? 1 : 0); dp[i % 2][0][1] = dp[1 - i % 2][0][1] + (app[i] == 2 ? 1 : 0); for (int j = 1; j <= (i - 1 > w ? w : i - 1); ++j) { dp[i % 2][j][0] = max(dp[1 - i % 2][j][0], dp[1 - i % 2][j - 1][1]) + (app[i] == 1 ? 1 : 0); dp[i % 2][j][1] = max(dp[1 - i % 2][j][1], dp[1 - i % 2][j - 1][0]) + (app[i] == 2 ? 1 : 0); } } int ans = 0; for (int i = 0; i <= w; ++i) { ans = max(ans, max(dp[t % 2][i][0], dp[t % 2][i][1])); } printf("%d\n", ans); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41484689