思路:
简单dp。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int t,w,x[1005]; 6 int dp[1005][2][35]; 7 int dfs(int now,int cur,int rem) 8 { 9 if(dp[now][cur][rem] != -1) 10 return dp[now][cur][rem]; 11 if(now == t) 12 return 0; 13 int p = dfs(now + 1,cur,rem) + (cur == x[now] - 1); 14 if(rem >= 1) 15 p = max(p, dfs(now,1 - cur,rem - 1) + (cur == x[now] - 1)); 16 return dp[now][cur][rem] = p; 17 } 18 int main() 19 { 20 memset(dp,-1,sizeof(dp)); 21 cin >> t >> w; 22 for(int i = 0;i < t;i ++) 23 { 24 scanf("%d",&x[i]); 25 } 26 cout << dfs(0,0,w) << endl; 27 return 0; 28 }