HDU 5188 背包

有N道题,要求得到最少W分

给出N道题的:每道题用时T,分数V,应在且必须在L时刻提交才能得分

问得到W分所用的最少的时间

以L-T排序,然后做01背包即可

#include "stdio.h"
#include "algorithm"
#include "string.h"
using namespace std;

struct Mark
{
    int t,v,l,x;
}mark[40];

int dp[300010];

bool cmp(Mark a,Mark b)
{
   if (a.x!=b.x) return a.x<b.x;
     return a.l<b.l;
}
int Max(int a,int b)
{
    if (a<b) return b;
    else return a;
}
int main()
{
    int n,m,i,j,ans,up,sum,sum1;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        sum=sum1=up=0;
        for (i=1;i<=n;i++)
        {

            scanf("%d%d%d",&mark[i].t,&mark[i].v,&mark[i].l);
            sum+=mark[i].v;
            sum1+=mark[i].t;
            up=Max(up,mark[i].l);
            mark[i].x=mark[i].l-mark[i].t;
        }
        if (sum<m) {printf("zhx is naive!\n"); continue;}
        up=Max(up,sum1);
        sort(mark+1,mark+n+1,cmp);
        memset(dp,0,sizeof(dp));
        for (i=1;i<=n;i++)
            for (j=up;j>=mark[i].l ;j--)
            dp[j]=Max(dp[j],dp[j-mark[i].t]+mark[i].v);

        ans=up+1;
        for (i=0;i<=up;i++)
        if (dp[i]>=m) {ans=i;break;}
        if(ans==up+1) printf("zhx is naive!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-30 06:17:34

HDU 5188 背包的相关文章

hdu 4381 背包

http://acm.hdu.edu.cn/showproblem.php?pid=4381 Problem Description There are n boxes in one line numbered 1 to n, at the beginning, all boxes are black. Two kinds of operations are provided to you: 1 ai xi :You can choose any xi black boxes in interv

hdu 5188 zhx and contest [ 排序 + 背包 ]

传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 49 Problem Description As one of the most powerful brushes in the world, zhx usually takes part

HDU-1171 Big Event in HDU (多重背包)

Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.The splitting is absolutely a big

poj 3624 &amp;&amp; hdu 2955(背包入门)

http://poj.org/problem?id=3624 背包中最基础的01背包,大意是有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使价值总和最大 主要是要有动态规划的思想,列出每个容量下的最大值,每次只考虑取或不取两种情况<一个状态一个状态的转移 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int dp[12900]; 5 in

HDU 1171 Big Event in HDU(01背包)

题目地址:HDU 1171 还是水题..普通的01背包.注意数组要开大点啊.... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &

HDU 2639 背包第k优解

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4824    Accepted Submission(s): 2514 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

hdu 01背包(1171+2546+。。

1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : 1171 ( Big Event in HDU ) Judge Status : Accepted #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> us

(hdu step 3.3.1)Big Event in HDU(01背包:N件物品放在容量为V的背包中,第i件物品的费用是c[i],价值是w[i]。问所能获取的最大价值)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 854 Accepted Submission(s): 345 Problem Description Nowadays, we all know that Computer College is the biggest department in HDU.

HUD 1171 Big Event in HDU(01背包)

Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.The splitting is