哈理工OJ 2274 Heroic Action(01坑背包)
来源:互联网 发布:深入浅出node.js系列 编辑:程序博客网 时间:2024/06/06 07:46
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2274
Heroic Action
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 35(11 users) Total Accepted: 12(11 users) Rating: Special Judge: No
Description
The hero has infiltrated the villain’s island and beat the villain again! But the villain still
refused to lose and activated the island’s self-destruction system. The island will soon
explode. Luckily, the hero has managed to find a parachute and jumped from the tall tower
on the island with a height of y meters. Now he hopes to get away from the island as far
as possible. While in the air, the hero can do n kinds of actions, while doing the i-th
action, his height will decrease a i meters and he will move forward b i meters. If the
current height of the hero is smaller than the height the action required, then the hero
cannot perform this action. If the hero does nothing, he will fall straight down slowly.
Remember, the same kind of action can be only performed once. And the hero can choose
to perform whatever actions in any order he wish.
Now give you the tower’s height y, and all the actions the hero can perform, please
calculate the maximum horizontal distance from the island the hero can get.
Input
The first line is the number of test cases T.
For each test cases, the first line contains two integers, y(1 ≤ y ≤ 1000) and n(1 ≤
n ≤ 1000). Then n lines follows, each line has two integers ai (1 ≤ ai ≤ y) and bi (1 ≤ bi ≤
105 ), representing the height the hero will lose and the distance he will gain after
performing this action.
Output
For each test cases, output a single line containing the maximum distance from the island the hero can get.
Sample Input
2
10 2
10 10
10 5
20 5
5 1
5 1
5 1
5 1
5 1
Sample Output
10
4
【思路分析】其实就是简单的背包,坑点在于用max函数会超时,无论是自己写还是调用函数库里的吗,用if来判断就不会超时,是不是有点坑。
【AC代码】
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int maxn(int a,int b){ if(a>b) { return a; } else { return b; }}struct node{ int ai,bi;}a[100005];int dp[1005];int main(){ int t,n,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&a[i].ai,&a[i].bi); } for(int i=0;i<=n;i++) { dp[i]=0; } for(int i=1;i<=m;i++) { for(int j=n;j>=a[i].ai;j--) { if(dp[j-a[i].ai]+a[i].bi>dp[j])//坑死人 { dp[j]=dp[j-a[i].ai]+a[i].bi; } } } printf("%d\n",dp[n]); } return 0;}
- 哈理工OJ 2274 Heroic Action(01坑背包)
- 哈理工OJ 1333GG的关心(01背包)
- 哈理工OJ 1627 猪猪罐(完全背包)
- Hrbust 哈理工 oj 2317 Game (完全背包)
- 哈理工OJ 2090 背包【思维】
- hrbust/哈理工oj 1740 A Story on a Sunshine Beach【二维01背包+输出路径】
- hrbust 哈理工oj 1993数硬币【dp】【背包】
- 南阳理工oj 题目289 苹果 01背包
- (坑点背包)0-1哈理工OJ1558
- 哈理工OJ 295 方方正正(判断01矩阵是否合法)
- 南阳理工1221 找数达人(01背包)
- 哈理工OJ 2304 Julyed(水题)
- hrbust 哈理工oj 1541 集合划分【dp、0-1背包】
- 哈理工OJ 2179(深搜)
- 哈理工 oj 1161 Leyni
- 哈理工OJ 1692【水题】
- 哈理工oj 2317 Game
- 哈理工oj 2002 幂集
- 顺序表 C++
- 告别软考——希望再也不“见”
- 正则表达式 学习
- HDU1238 字符暴搜
- 1202: [HNOI2005]狡猾的商人
- 哈理工OJ 2274 Heroic Action(01坑背包)
- ⑧Mysql导入导出相关操作
- 1112 练习 系统调用
- 爬虫资源
- 如何测试电脑的大小端存储
- GStreamer基础教程14——常用的element
- nginx windows应用配置
- Spring 全注解配置 bean 和 调用 (9) 5种通知
- python中tushare的安装--win7