HDU2546-饭卡(DP+贪心)

来源:互联网 发布:ssh 端口转发 反向代理 编辑:程序博客网 时间:2024/06/12 02:23

DP动态优化+贪心的算法。


#include <iostream>#include <algorithm>#include <cstring>using namespace std;int a[1010],V,rest,f[1200],maxm;void DP(){    for(int i=0;i<V;i++){        for(int v=rest-5;v>=0;v--){            if(f[v]==1) {                if(v+a[i]<=rest) f[v+a[i]]=1;                if(v+a[i]>maxm) maxm=v+a[i];            }        }    }}int main(){    while(cin>>V){        if(V==0) return 0;        maxm=0;        memset(f,0,sizeof(f));        f[0]=1;        for(int i=0;i<V;i++){            cin>>a[i];        }        sort(a,a+V);        cin>>rest;        DP();        cout<<rest-maxm<<endl;    }    return 0;}


0 0
原创粉丝点击