哈理工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;}
0 0