http://blog.csdn.net/libin56842/article/details/9492351
这次比较理解那个!dp[j]是为了什么,因为是滚动数组,没有这个的话used那边会出问题
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <cctype> #include <vector> #include <iterator> #include <set> #include <map> #include <sstream> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) #define pf printf #define sf scanf #define spf sprintf #define pb push_back #define debug printf("! ") #define MAXN 1010 #define MAX(a,b) a>b?a:b #define blank pf(" ") #define LL long long #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define pqueue priority_queue #define INF 0x3f3f3f3f struct node { int h,c,a; }p[405]; int K; bool dp[40005]; int used[40005]; int cmp(const node& x,const node& y) { return x.a<y.a; } int main() { int i,j,k; while(~sf("%d",&K)) { for(i=1;i<=K;i++) { sf("%d%d%d",&p[i].h,&p[i].a,&p[i].c); } sort(p+1,p+K+1,cmp); /* for(i=1;i<=K;i++) { pf("%d %d %d ",p[i].h,p[i].a,p[i].c); }*/ mem(dp,false); dp[0] = true; int ans = 0; for(i=1;i<=K;i++) { mem(used,0); for(j=p[i].h;j<=p[i].a;j++) { if(!dp[j] && dp[j-p[i].h] && used[j-p[i].h]<p[i].c) { dp[j] = true; used[j] = used[j-p[i].h] + 1; //pf("%d %d %d ",i,j,used[j]); if(ans<j) ans = j; } } } pf("%d ",ans); } return 0; }