poj 2392 Space Elevator

题目链接http://poj.org/problem?id=2392

题目分类:动态规划

代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>

using namespace std;

#define INF 0x3f3f3f3f
int n;
bool f[40010];
int user[40010];

struct P
{
    int h,a,c;

}goods[500];

int cmp(P X,P Y)
{
    return X.a<Y.a;
}

int main()
{
   // freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)!=EOF)
    {
        memset(goods,0,sizeof(goods));

        for(int i=1;i<=n;i++)
        {
            scanf("%d %d %d",&goods[i].h,&goods[i].a,&goods[i].c);
        }
        sort(goods+1,goods+n+1,cmp);
        int maxx=0;
        memset(f,0,sizeof(f));

        f[0]=1;
        for(int i=1;i<=n;i++)
        {
            memset(user,0,sizeof(user));
            for(int j=goods[i].h;j<=goods[i].a;j++)
            {
                if(!f[j] && f[j-goods[i].h] && user[j-goods[i].h]+1<=goods[i].c)
                {
                    f[j]=1;
                    user[j]=user[j-goods[i].h]+1;
                   // printf("%d   %d   %d\n",i,j,user[j]);
                    maxx=max(maxx,j);
                }
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}

/**
printf("%d   %d   %d\n",i,j,user[j]);
1   5   1
1   10   2
1   15   3
1   20   4
2   7   1
2   12   1
2   14   2
2   17   1
2   19   2
2   21   3
2   22   1
2   24   2
2   26   3
2   27   1
2   29   2
2   31   3
2   34   2
2   36   3
3   2   1
3   4   2
3   6   3
3   8   4
3   9   1
3   11   2
3   13   3
3   16   1
3   18   2
3   23   1
3   25   2
3   28   1
3   30   2
3   32   3
3   33   1
3   35   2
3   37   3
3   38   1
3   39   4
3   40   2
3   41   5
3   42   3
3   43   6
3   44   4
3   46   5
3   48   6
48
*/

 

posted @ 2015-11-08 15:21  Gssol  阅读(189)  评论(0编辑  收藏  举报