poj2184 背包
//Accepted 1492 KB 110 ms
//背包
//把si看成weight,Fi看成value,这可以表示成当dp[j]=max(dp[j-weight[i]]+value[i])
//考虑到si可能为负,需要整段区间的平移
//背包过程中,根据weight的正负,我们需要考虑dp的顺序,如果weight为正,这j从大到小
//如果weight为负,则要从小到大,这是根据dp[i][j]=max(dp[i][j],dp[i-1][j-weight[i]]+value[i])
//如果我们省略一维,这需要先更新j,再更新j-weight[i],当weight[i]为正时,我们要先更新大的。。。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
/**
* This is a documentation comment block
* 如果有一天你坚持不下去了,就想想你为什么走到这儿!
* @authr songt
*/
;
;
int value[imax_n];
int weight[imax_n];
];
int n;
int max(int a,int b)
{
return a>b?a:b;
}
void Dp()
{
;i<=n*;i++) dp[i]=-inf;
dp[n*]=;
;i<=n;i++)
{
&& weight[i]<=) continue;
)
{
;j>=weight[i];j--)
if (dp[j-weight[i]]>-inf)
dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
}
else
{
;j<=n*+weight[i];j++)
if (dp[j-weight[i]]>-inf)
dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
}
}
;
;i<=n*;i++)
)
ans=max(ans,dp[i]+i-n*);
printf("%d\n",ans);
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
;i<=n;i++)
scanf("%d%d",&weight[i],&value[i]);
Dp();
}
;
}
poj2184 背包的更多相关文章
- POJ-2184 Cow Exhibition---01背包变形(负数偏移)
题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...
- [poj2184]我是来水一下背包的
http://poj.org/problem?id=2184 题意:01背包的变种,就是说有2组值(有负的),你要取一些物品是2阻值的和非负且最大 分析: 1.对于负的很好处理,可以把他们都加上一个数 ...
- Cow Exhibition [POJ2184] [DP] [背包的负数处理]
题意: 有很多羊,每只羊有一个幽默度和智商,要选出一些羊,智商加幽默度总和最大,其中智商总和和幽默度总和都不能是负数. 样例输入: 5 -5 7 8 -6 6 -3 2 1 -8 -5 样例输出: 8 ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- poj2184 Cow Exhibition(p-01背包的灵活运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...
- POJ2184(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11092 Accepted: 4404 D ...
- POJ2184 Cow Exhibition 背包
题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...
- poj2184 01背包变形,价值为可为负数
题目链接:http://poj.org/problem?id=2184 题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,满足S与F都不能为负数的条件下,使S与F的和最大. tips:动 ...
随机推荐
- xUtils更新到3.0后的基本使用规则
说实话,对于xUtils,是我最近才用到的开发框架(也是刚接触),对于其功能不得不说,简化了很多的开发步骤,可以说是非常好的开发工具,但是其最近更新到3.0也没有解决加载自定义ImageView报错的 ...
- AOP 之 6.1 AOP基础(拾陆)
6.1.1 AOP是什么 考虑这样一个问题:需要对系统中的某些业务做日志记录,比如支付系统中的支付业务需要记录支付相关日志,对于支付系统可能相当复杂,比如可能有自己的支付系统,也可能引入第三方支付平 ...
- struts2在web.xml中的配置
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...
- python中的md5加密
import md5 import types def get_md5(data): if type(data) is not types.StringType: # 检验输入的数据是否为字符串 pr ...
- Reverse Linked List II [LeetCode]
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- template模版与Underscore.js
template模版与Underscore.js 在项目中经常使用的模版是Underscore这个js框架的实用功能. 在html里面设定模板,然后js绑定数据,这样能避免在js中出现非常多的html ...
- [工作技能]SVN
有的时候SVN上传txt文本文件,会报是bin文件的错误,解决方式是在.subversion文件夹下的config文件中加这么一句 *.txt = svn:mime-type=text/plain;s ...
- synchronized和static synchronized的比较
群里讨论的一个问题,网上别人已经贴出了很详细的说明,这里补充记录下,后面加入个人测试代码. 起因:1月份的时候看群里讨论一道问题,问题内容如下: 一个日本作者-结成浩的<java多线程设计模式& ...
- Eclipse如何设置代码提示功能
Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...
- Installing a single-server IBM FileNet P8 Platform system with IBM Content Navigator
Product documentation Abstract You can install all of the IBM FileNet P8 Platform components on a si ...