zoj 2297

来源:互联网 发布:免费人事工资软件 编辑:程序博客网 时间:2024/05/20 14:25

[DP+位运算状态压缩]

#include <iostream>#include <cstring>using namespace std;int dp[(1<<20)+10];int main(){    int people[20][2];    int k;    int n,boss;        while (cin>>n) {// the number of opponents        for (int i=0; i<n-1; i++) {            cin>>people[i][0]>>people[i][1];// consume and recover        }        cin>>boss;                int m=(1<<(n-1))-1;//2^(n-1)                memset(dp, 0, sizeof(dp));                dp[0]=100;//The initial HP of your character is 100.                for (int i=0; i<=m; i++) {//every status            for (int j=0; j<n-1; j++) {//n-1 roles                k=1<<j;                if (!(k&i)&&dp[i]>=people[j][0]) {//由状态i可以转化为i&k状态                    int s=dp[i]+people[j][1]-people[j][0];                    if (s>dp[i+k]) {                        dp[i+k]=s;//原来可能已经有值,则选择较大的                    }                    if (dp[i+k]>100) {                        dp[i+k]=100;                    }                }            }        }                if (dp[m]>=boss) {            cout<<"clear!!!"<<endl;        }else{            cout<<"try again"<<endl;        }    }        return 0;}



0 0
原创粉丝点击