动态规划,背包问题变型
来源:互联网 发布:sql中like用法 编辑:程序博客网 时间:2024/05/18 17:03
题目:UVALive - 4870
题意:坐过山车,可以兴奋,可以头晕。对于每一段路,你可以闭眼睛,也可以睁开眼睛。
如果睁开眼睛,会得到一定的兴奋值,也会产生一定的眩晕值。如果闭上眼睛,会减少一定的眩晕值,
但不会得到兴奋值。求不超过一个给定眩晕值情况下,得到的最大兴奋值。
另外,眩晕值初始为0,也不会低于0。
Input
There will be several test cases in the input. Each test case will begin with a line with three integers:
N K L
Where N(1,1 000)(闭合) is the number of sections in this particular roller coaster, K(1,500) (闭合)is the amount that Bessie's dizziness level will go down if she keeps her eyes closed on any section of the ride, and L(1,300 000)(闭合) is the limit of dizziness that Bessie can tolerate - if her dizziness ever becomes larger than L, Bessie will get sick, and that's not fun!
Each of the next N lines will describe a section of the roller coaster, and will have two integers:
F D
Where F(1,20) is the increase to Bessie's total fun that she'll get if she keeps her eyes open on that section, and D(1,500) is the increase to her dizziness level if she keeps her eyes open on that section. The sections will be listed in order. The input will end with a line with three 0s.
Output
For each test case, output a single integer, representing the maximum amount of fun Bessie can have on that roller coaster without exceeding her dizziness limit. Print each integer on its own line with no spaces. Do not print any blank lines between answers.
Sample Input
3 1 2 2 1 3 1 5 2 10 5 1 20 2 12 4 3 3 10 6 20 3 19 9 19 7 1 500 15 5 4 2 0 0 0
Sample Output
7 0
定义:dp[i+1][j]表示前i段路中挑选出兴奋值总和为j时,总眩晕值的最小量(不存在时就是一个充分大的数值inf)。
dp[0][0] = 0
dp[0][j] = inf
#include<iostream>#include<bits/stdc++.h>using namespace std;const int maxn = 1005;const int maxv = 20;const int inf = 999999;int v[maxn];//得到的价值int w[maxn];//重量int N,K,L;int dp[maxn][maxn*maxv];void solve() {fill(dp[0],dp[0]+maxn*maxv,inf);dp[0][0] = 0;for(int i=0; i<N; i++) {for(int j=0; j<=maxn*maxv; j++) {if(j<v[i]) {if(dp[i][j]==inf) dp[i+1][j] = dp[i][j];//上一个值不存在,下一个值直接不存在 else dp[i+1][j] = dp[i][j]-K;//第i段不睁眼,眩晕减掉k if(dp[i+1][j]<0) dp[i+1][j] = 0;//眩晕不能低于0 } else {int x;if(dp[i][j]==inf) {x = dp[i][j];} else {x = dp[i][j]-K;if(x<0) x = 0;}int y;if(dp[i][j-v[i]]==inf) {y = dp[i][j-v[i]];} else {y = dp[i][j-v[i]]+w[i];}if(y<=L||x<=L)//至少有一个不能超过最大眩晕值 dp[i+1][j] = min(x,y);else dp[i+1][j] = inf;}}}int res = 0;//for(int i=0;i<=N;i++){//for(int j=0;j<=2;j++){//cout<<dp[i][j]<<' ';//}//cout<<endl;//}for(int i=0; i<=N*maxv; i++) {//选出满足条件的最大的j if(dp[N][i]<=L) {res = i;}}cout<<res<<endl;}int main() {while(cin>>N>>K>>L) {if(!N&&!K&&!L) return 0;for(int i=0; i<N; i++) {cin>>v[i]>>w[i];}solve();}}
可以优化为一维dp。。。
- 动态规划,背包问题变型
- 动态规划 背包问题
- 【动态规划】背包问题
- 动态规划-背包问题
- 动态规划+背包问题
- 动态规划-背包问题
- 背包问题 -- 动态规划
- 《背包问题》 动态规划
- 动态规划-背包问题
- 动态规划 背包问题
- 动态规划 背包问题
- 动态规划 背包问题
- 动态规划 背包问题
- 动态规划-背包问题
- 动态规划--背包问题
- 动态规划背包问题
- 背包问题 动态规划
- 动态规划-背包问题
- openCV利用航拍相机从底部向上扫描物体拼接全景图
- 目前的感悟
- 平衡二叉树【AVL树】- 通过实例理解概念
- 剑指offer-44.翻转单词顺序列
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- 动态规划,背包问题变型
- 可逆素数
- (不ROOT/不安装软件/安全)--Android/安卓手机手动冻结/禁用系统应用方法
- PHP 中使用explode()函数切割字符串为数组
- MySQL Order By实现原理分析和Filesort优化
- 各种面试题-虐虐虐
- [cv] filters as templates-normxcorr2(template,img)
- 383. Ransom Note
- java的本地方法