传送门

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
 
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
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n,w;
int dp[N*M];
int sum;
int sumt,mal;
int ma; typedef struct
{
int t;
int v;
int l;
}PP; PP p[N]; bool cmp(PP a,PP b)
{
return a.l-a.t<b.l-b.t;
}
void ini()
{
int i;
sum=;sumt=;mal=;
for(i=;i<=n;i++){
scanf("%d%d%d",&p[i].t,&p[i].v,&p[i].l);
sum+=p[i].v;
sumt+=p[i].t;
mal=max(mal,p[i].l);
}
ma=mal+sumt;
sort(p+,p++n,cmp);
// 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);
} void solve()
{
if(sum<w) return;
memset(dp,,sizeof(dp));
int i,j;
for(i=;i<=n;i++){
//printf(" i=%d l=%d\n",i,p[i].l);
for(j=ma;j>=;j--){
if(j>=p[i].l){
if(j>=p[i].t)
dp[j]=max(dp[j],p[i].v+dp[ j-p[i].t ]);
}
else{
//dp[j]=dp[i-1][j];
}
//printf(" i=%d j=%d dp=%d\n",i,j,dp[j]);
}
}
} void out()
{
if(sum<w){
printf("zhx is naive!\n");
}
else{
int i;
for(i=;;i++){
if(dp[i]>=w){
printf("%d\n",i);break;
}
}
}
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(cnt=1;cnt<=T;cnt++)
while(scanf("%d%d",&n,&w)!=EOF)
{
ini();
solve();
out();
}
}

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

  1. HDU 5188 zhx and contest(带限制条件的 01背包)

    Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...

  2. HDOJ 5188 zhx and contest 贪婪+01背包

    zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. hdu 5188(带限制的01背包)

    zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]

    传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. hdu 5187 zhx's contest (快速幂+快速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  6. HDU 5187 zhx's contest 快速幂,快速加

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  7. HDU - 5187 zhx's contest(快速幂+快速乘法)

    作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1. ...

  8. hdu 5187 zhx's contest

    题目分析如果n=1,答案是1,否则答案是2n−2. 证明:ai肯定是最小的或者最大的.考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的. 那么ai是最小或者最大分别有2n−1种情况,而整个序 ...

  9. HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

随机推荐

  1. iOS 播放本地,网络视频

    /** *  创建媒体播放控制器MPMoviePlayerControlle 可以控制尺寸 * *  @return 媒体播放控制器 */ -(MPMoviePlayerController *)mo ...

  2. 生产线上的Nginx如何添加未编译安装模块

    正在生产线上跑着web前端是nginx+tomcat,现在有这样一个需求,需要对网站的单品页面和列表页设置缓存,不同的页面设置不同的缓存,但是由于开始没有安装ngx_cache_purge这个模块,现 ...

  3. 如何用sql server数据库恢复.bak数据库备份

    @hcy(黄灿奕) 之前有两次都恢复不了,折腾了很长时间,这一次碰到这样的问题,居然又忘了,又捣鼓了很长时间,现在记下来 1.右击SQL Server 2008实例下的“数据库”文件夹.就是与安全性. ...

  4. 回顾Spring MVC_01_概述_入门案例

    SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...

  5. Android(java)学习笔记162:开发一个多界面的应用程序之两种意图

    1.两种意图: (1)显式意图: 在代码里面用intent设置要开启Activity的字节码.class文件: (2)隐式意图: Android(java)学习笔记218:开发一个多界面的应用程序之人 ...

  6. DELETE - 删除一个表中的行

    SYNOPSIS DELETE FROM [ ONLY ] table [ WHERE condition ] DESCRIPTION 描述 DELETE 从指明的表里删除满足 WHERE 子句的行. ...

  7. _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用

    _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用

  8. Java数据结构和算法(一)--栈

    栈: 英文名stack,特点是只允许访问最后插入的那个元素,也就是LIFO(后进先出) jdk中的stack源码: public class Stack<E> extends Vector ...

  9. Visual Studio 2017部署 webStrom开发的nodejs项目

    vs点击文件--新建--项目--JavaScript--Node.js--通过现有Node.js代码 wxxcx为nodejs项目根目录,然后右击整个项目--属性:1.启动目录2.默认打开的链接3.设 ...

  10. Android典型界面设计-访网易新闻实现双导航tab切换

    一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...