日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

哈理工OJ 2274 Heroic Action(01坑背包)

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈理工OJ 2274 Heroic Action(01坑背包) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接: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函數(shù)會超時,無論是自己寫還是調(diào)用函數(shù)庫里的嗎,用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; }

總結(jié)

以上是生活随笔為你收集整理的哈理工OJ 2274 Heroic Action(01坑背包)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。