hdu5188zhx and contest [01背包至少li才能。。。]
2016-05-20 22:04
417 查看
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 $i^{th}$ problem in $t_i$ units of time and he can get $v_i$ points.
As he is too powerful, the administrator is watching him. If he finishes the $i^{th}$ problem before time $l_i$, 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 \leq n \leq 30$, $0 \leq w \leq {10}^{9}$)
Then come n lines which contain three integers $t_i, v_i, l_i$. ($1 \leq t_i,l_i \leq {10}^{5}, 1 \leq v_i \leq {10}^{9}$)
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!
本题(bc)和这个题
hdu3466Proud Merchants【至少需要Qi才能买Pi】
(这是2010多校)
一样。多的就是优化上限TLE了两发==
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=4000009;
int dp[MAXN];
struct Node
{
int p,q,v;
}node[35];
bool cmp(Node a,Node b)
{
return (a.q-a.p)<(b.q-b.p);
}
int main()
{
// freopen("cin.txt","r",stdin);
int n,w;
int i,j;
int p,q,v;
while(scanf("%d%d",&n,&w)!=EOF)
{
memset(dp,0,sizeof(dp));
int sum=0,up=0;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&node[i].p,&node[i].v,&node[i].q);
sum+=node[i].p;
up=max(up,node[i].q);
}
up=max(up,sum);
sort(node,node+n,cmp);
for(i=0;i<n;i++)
{
for(j=up;j>=node[i].p;j--)
{
if(j>=node[i].q)
dp[j]=max(dp[j],dp[j-node[i].p]+node[i].v);
}
}
int ans=0;
for(i=1;i<=up;i++)
if(w<=dp[i]) {ans=i;break;}
if(ans) printf("%d\n",ans);
else puts("zhx is naive!");
}
return 0;
}
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 $i^{th}$ problem in $t_i$ units of time and he can get $v_i$ points.
As he is too powerful, the administrator is watching him. If he finishes the $i^{th}$ problem before time $l_i$, 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 \leq n \leq 30$, $0 \leq w \leq {10}^{9}$)
Then come n lines which contain three integers $t_i, v_i, l_i$. ($1 \leq t_i,l_i \leq {10}^{5}, 1 \leq v_i \leq {10}^{9}$)
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!
本题(bc)和这个题
hdu3466Proud Merchants【至少需要Qi才能买Pi】
(这是2010多校)一样。多的就是优化上限TLE了两发==
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=4000009;
int dp[MAXN];
struct Node
{
int p,q,v;
}node[35];
bool cmp(Node a,Node b)
{
return (a.q-a.p)<(b.q-b.p);
}
int main()
{
// freopen("cin.txt","r",stdin);
int n,w;
int i,j;
int p,q,v;
while(scanf("%d%d",&n,&w)!=EOF)
{
memset(dp,0,sizeof(dp));
int sum=0,up=0;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&node[i].p,&node[i].v,&node[i].q);
sum+=node[i].p;
up=max(up,node[i].q);
}
up=max(up,sum);
sort(node,node+n,cmp);
for(i=0;i<n;i++)
{
for(j=up;j>=node[i].p;j--)
{
if(j>=node[i].q)
dp[j]=max(dp[j],dp[j-node[i].p]+node[i].v);
}
}
int ans=0;
for(i=1;i<=up;i++)
if(w<=dp[i]) {ans=i;break;}
if(ans) printf("%d\n",ans);
else puts("zhx is naive!");
}
return 0;
}
相关文章推荐
- 训练3 习题15
- 喜剧者联盟观后感
- 饿了么开源项目Hermes:新颖巧妙易用的Android进程间通信IPC框架
- 转发与重定向区别
- TCP/IP协议原理与应用笔记21:路由选择的方法
- eclipse的应用技巧(一)
- 滴滴算法大赛算法解决过程(实时更新)
- Swift专题讲解十六——ARC在Swift中的应用
- 【bzoj 1042】 [HAOI2008] 硬币购物(dp+容斥原理)
- POJ 2385 Apple Catching
- 在Django中使用markdown
- 【模板】莫队算法
- Solr之搭建Solr6.0服务并从Mysql上导入数据
- 爱的方程
- 高精度模板2.0
- sql server 分组,取每组的前几行数据
- 发几张18650锂电池的放电曲线
- 枚举类
- Linux下进程的通信方式: 有名管道(命名管道)
- ACM--多边形重心–-HDOJ 1115-- Lifting the Stone