hdu5188 加限制的01背包问题
http://acm.hdu.edu.cn/showproblem.php?
pid=5188
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.
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)
1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
7
8
zhx is naive!
/**
hdu5188 有限制条件的01背包问题
题目大意:有n道题i题用时ti秒,得分vi,在li时间点之前不能做出来,并且一道题不能分开几次做(一旦開始做,必须在ti时间内把它做完)
问得到w分的最小用时是多少
解题思路:非常像01背包的基本题,可是有一个li分钟前不能AC的限制,因此第i道题必须在最早第(li-ti)时刻做,我们依照l-t递增排序,然后依照经典解法来做
即可了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std; struct note
{
int t,v,l;
bool operator <(const note &other)const
{
return l-t<other.l-other.t;
} }node[35]; int n,m;
int dp[3000005]; int main()
{
while(~scanf("%d%d",&n,&m))
{
int sum=0,ans=0,up=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&node[i].t,&node[i].v,&node[i].l);
sum+=node[i].v;
ans+=node[i].t;
up=max(up,node[i].l);
}
if(m>sum)
{
printf("zhx is naive!\n");
continue;
}
sort(node,node+n);
up=max(up,ans);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
for(int j=up;j>=node[i].l;j--)
{
if(j>=node[i].t)
{
dp[j]=max(dp[j],dp[j-node[i].t]+node[i].v);
}
}
}
int flag=0;
for(int i=0;i<=up;i++)
{
if(dp[i]>=m)
{
printf("%d\n",i);
flag=1;
break;
}
}
if(flag==0)
{
printf("zhx is naive!\n");
}
}
return 0;
}
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.
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)
1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
7
8
zhx is naive!
hdu5188 加限制的01背包问题的更多相关文章
- 01背包问题(动态规划)python实现
01背包问题(动态规划)python实现 在01背包问题中,在选择是否要把一个物品加到背包中.必须把该物品加进去的子问题的解与不取该物品的子问题的解进行比較,这样的方式形成的问题导致了很多重叠子问题, ...
- 算法笔记(c++)--01背包问题
算法笔记(c++)--经典01背包问题 算法解释起来太抽象了.也不是很好理解,最好的办法就是一步步写出来. 背包问题的核心在于m[i][j]=max(m[i-1][j],m[i-1][j-w[i]]+ ...
- PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***
1068 Find More Coins (30 分) Eva loves to collect coins from all over the universe, including some ...
- 0-1背包问题——回溯法求解【Python】
回溯法求解0-1背包问题: 问题:背包大小 w,物品个数 n,每个物品的重量与价值分别对应 w[i] 与 v[i],求放入背包中物品的总价值最大. 回溯法核心:能进则进,进不了则换,换不了则退.(按照 ...
- 201871030138-杨蕊媛 实验二 个人项目—《D{0-1}背包问题》项目报告
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...
- 01背包问题:POJ3624
背包问题是动态规划中的经典问题,而01背包问题是最基本的背包问题,也是最需要深刻理解的,否则何谈复杂的背包问题. POJ3624是一道纯粹的01背包问题,在此,加入新的要求:输出放入物品的方案. 我们 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- HDU 1864最大报销额 01背包问题
B - 最大报销额 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDOJ 2546饭卡(01背包问题)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
随机推荐
- Nginx +iis反向代理
一:简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所 ...
- win7 ie10输入网址显示无法显示此页问题的解决
忽然又一天,非常奇怪,所有的浏览器都无法访问网页,直接输入IP也是不行. 本人试过各种方法,包括用360进行修复:清除%temp%下文件:看是否设错了dns, 升级和修复IE,重新注册ie相关的dll ...
- ActiveMQ的入门demo
步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...
- 一次U盘安装Ubuntu双系统实录
准备:Win7系统(原来就在我电脑的系统) UltraISO(把系统写进U盘的工具) EasyBCD(双系统引导修复工具) 笔记本电脑(我的是联想Y470N) U盘一个 步骤: U盘准备工作: 插入U ...
- hdu 1823 Luck and Love 二维线段树
题目链接 很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发... #include<bits/stdc++.h> using nam ...
- Ubuntu Server修改IP、DNS、hosts
本文记录下Ubuntu Server 16.04修改IP.DNS.hosts的方法 -------- 1. Ubuntu Server 修改IP sudo vi /etc/network/interf ...
- 产品在焊接时出现异常,尤其是尺寸较大的QFP芯片,焊接后出现虚焊、冷焊、假焊等问题?
1 不良描述 客户采用我们提供的SMT设备后,部分产品在焊接时出现异常,尤其是尺寸较大的QFP芯片,焊接后出现虚焊.冷焊.假焊等不良.应客户要求对这一批不良产品以及生产条件进行分析,以便找到改善的依据 ...
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...
- start stack
Start OpenStack Services After launching your stack by Devstack, you maybe stop some services or reb ...
- Fastboot的使用简单教程
大家都知道HTC手机重新启动进入所谓的project模式,就是HBOOT,然后能够进入FASTBOOT界面,在这个界面.我们能够在电脑端能够做非常多事,特别是HBOOT被改动过,假设是ENG S-OF ...