程序员人生 网站导航

hihocoder 1044 状态压缩dp

栏目:php教程时间:2015-05-14 09:24:34
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> #define INF 100000000 using namespace std; int n,m,q; int dp[1050][1100]; int num[1100]; int a[1050]; int f(int i){ int ans = 0; while(i){ ans += i&1; // cout <<"a " << i << endl; // cout << ( i^0 )<< endl; i >>= 1; } return ans; } void fun(){ for(int i = 0;i <= (1<<10);i++){ num[i] = f(i); } } int main(){ fun(); while(cin >> n >> m >> q){ for(int i = 0;i < n;i++){ scanf("%d",&a[i]); } memset(dp[0],0,sizeof(dp[0])); for(int i = 0;i < n;i++){ for(int s = 0; s < (1 << m);s++){ if(num[s] <= q){ if(s&1){ dp[i+1][s] = a[i]; dp[i+1][s] += max(dp[i][(s>>1)^(1<<(m-1))],dp[i][(s>>1)]); } else{ if(num[s] == q){ dp[i+1][s] = dp[i][s>>1]; } else{ dp[i+1][s] = max(dp[i][(s>>1)^(1<<(m-1))],dp[i][(s>>1)]); } } } else{ dp[i][s] = 0; } } } int max = 0; for(int i = 0;i < (1<<m);i++){ if(num[i] <= q && dp[n][i] > max){ max = dp[n][i]; } } printf("%d ",max); } return 0; }
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐