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 in all kinds of contests.
One day, zhx takes part in an contest. He found the contest very easy for him.
There are n

problems in the contest. He knows that he can solve the ith

problem in ti

units of time and he can get vi

points.
As he is too powerful, the administrator is watching him. If he finishes the ith

problem before time li

, he will be considered to cheat.
zhx doesn‘t really want to solve all these boring problems. He only wants to get no less than w

points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points.
Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.

Input

Multiply test cases(less than 50

). Seek EOF

as the end of the file.
For each test, there are two integers n

and w

separated by a space. (1≤n≤30

, 0≤w≤109

)
Then come n lines which contain three integers ti,vi,li

. (1≤ti,li≤105,1≤vi≤109

)

Output

For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying "zhx is naive!" (Do not output quotation marks).

Sample Input

1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3

Sample Output

7
8
zhx is naive!

Source

BestCoder Round #33

Recommend

hujie   |   We have carefully selected several similar problems for you:  5189 5184 5181 5180 5177

照例,先转一发官方题解:http://bestcoder.hdu.edu.cn/

1003 zhx and contest
状态压缩动态规划。i 

表示当前已经做了的题的集合。f i  

表示做完集合i中的所有题的最短用时。那么转移是相当简单的。但是也要注意判断状态是否合法时总分数可能会超过int范围。
另外有一种按l 

排序后折半枚举的思路。但是这种思路是错的。因为很有可能做一道结束时间靠前的题会导致时间被卡,但是它又可以放到后面再做。就像背包不能贪心一样。

如官方发题解所说,不能按照l排序,这种贪心是错误的。但是,可以按照 l-t 排序,即如果要选该题,那么浪费少的先选(如果不选,后面也不会选了),下面就是01背包了。

还是看不懂,为何n<=30也可以状压,不是应该妥妥 TLE+MLE的节奏吗? 难道是数据弱了? 等待大神博客ing
13130322 2015-03-15 10:11:17 Accepted 5188 202MS 14184K 1739 B G++ czy
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <map>
 8 #include <string>
 9
10 #define ll long long
11 int const N = 32;
12 int const M = 100002;
13 int const inf = 1000000000;
14 ll const mod = 1000000007;
15
16 using namespace std;
17
18 int n,w;
19 int dp[N*M];
20 int sum;
21 int sumt,mal;
22 int ma;
23
24 typedef struct
25 {
26     int t;
27     int v;
28     int l;
29 }PP;
30
31 PP p[N];
32
33 bool cmp(PP a,PP b)
34 {
35     return a.l-a.t<b.l-b.t;
36 }
37 void ini()
38 {
39     int i;
40     sum=0;sumt=0;mal=0;
41     for(i=1;i<=n;i++){
42         scanf("%d%d%d",&p[i].t,&p[i].v,&p[i].l);
43         sum+=p[i].v;
44         sumt+=p[i].t;
45         mal=max(mal,p[i].l);
46     }
47     ma=mal+sumt;
48     sort(p+1,p+1+n,cmp);
49    // for(i=1;i<=n;i++) printf(" i=%d t=%d v=%d l=%d\n",i,p[i].t,p[i].v,p[i].l);
50 }
51
52 void solve()
53 {
54     if(sum<w) return;
55     memset(dp,0,sizeof(dp));
56     int i,j;
57     for(i=1;i<=n;i++){
58         //printf("    i=%d l=%d\n",i,p[i].l);
59         for(j=ma;j>=0;j--){
60             if(j>=p[i].l){
61                 if(j>=p[i].t)
62                     dp[j]=max(dp[j],p[i].v+dp[ j-p[i].t ]);
63             }
64             else{
65                 //dp[j]=dp[i-1][j];
66             }
67             //printf(" i=%d j=%d dp=%d\n",i,j,dp[j]);
68         }
69     }
70 }
71
72 void out()
73 {
74     if(sum<w){
75         printf("zhx is naive!\n");
76     }
77     else{
78         int i;
79         for(i=0;;i++){
80             if(dp[i]>=w){
81                 printf("%d\n",i);break;
82             }
83         }
84     }
85 }
86
87 int main()
88 {
89     //freopen("data.in","r",stdin);
90     //scanf("%d",&T);
91     //for(cnt=1;cnt<=T;cnt++)
92     while(scanf("%d%d",&n,&w)!=EOF)
93     {
94         ini();
95         solve();
96         out();
97     }
98 }
时间: 2024-08-08 11:44:26

hdu 5188 zhx and contest [ 排序 + 背包 ]的相关文章

HDU - 5187 zhx&#39;s contest(快速幂+快速乘法)

作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1..ai是单调递减或者单调递增的. 2:ai..an是单调递减或者单调递增的. 他想你告诉他有多少种排列是漂亮的.因为答案很大,所以只需要输出答案模p之后的值. Input Multiply test cases(less than 10001000). Seek EOF as the end of

HDU 2639 Bone Collector II(01背包变型)

此题就是在01背包问题的基础上求所能获得的第K大的价值. 具体做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,其实就是2个数组合并之后排序,但是实际做法最好不要怎么做,因为你不知道总共有多少种,而我们最多只需要前K个大的就行了(因为可能2个数组加起来的组合数达不到K个),如果全部加起来数组开多大不清楚,所以可以选用归并排序中把左右2个有序数组合并成一个有序数组的方法来做,就是用2个变量去标记2个有序数组的头,然后比

hdu 3339 In Action (最短路径+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1237 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

HDU 5186 zhx&#39;s submissions 模拟,细节 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=5186 题意是分别对每一位做b进制加法,但是不要进位 模拟,注意:1 去掉前置0 2 当结果为0时输出0,而不是全部去掉 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=101; const int maxm=201; int n,b; char

hdu 4857 逃生 (拓扑排序+保证最小在前面)

逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 74    Accepted Submission(s): 13 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. 同时,社会是不平

HDU 3033 I love sneakers! 分组背包

我是个逗比...真心不是搞算法的料 不太中规中矩的分组背包,分组至少选一件商品.dp[i][j] 可以由当前dp[i-1][j-c] 和 dp[ i ][j-c]更新得到. #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <cmath

HDU 5234 Happy birthday --- 三维01背包

HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置(i,j)有一个容量为k的背包所能获得的最大价值 决策:a[i][j]处的数是否选取 不选取: dp[i][j][k]= max(dp[i-1][j][k], dp[i][j-1][k]) 选取:首先要求k >=a[i][j],那么dp[i][j][k] = max(dp[i-1][j][k-w[i

hdu 3033 I love sneakers!(分组背包,每组至少取一件)

http://acm.hdu.edu.cn/showproblem.php?pid=3033 大致题意:某人要买鞋子,有k种鞋,要求每种鞋至少买一双,给出每双鞋子的花费和价值,问m元钱可以买到的鞋子的最大价值是多少. 思路:分组背包问题.与传统的分组背包不同:每组物品至少取一件:且每组中物品任意取. 想一想传统的分组背包,每组至多选一件: for 所有的组k     for v=V..0         for 所有的i属于组k             f[v]=max{f[v],f[v-c[i

HDU 5186 zhx&#39;s submissions (进制转换)

Problem Description As one of the most powerful brushes, zhx submits a lot of code on many oj and most of them got AC. One day, zhx wants to count how many submissions he made on n ojs. He knows that on the ith oj, he made ai submissions. And what yo