POJ 2184 Cow Exhabition
fun..."
- Cows with Guns by Dana Lyons
The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
Input
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
Output
Sample Input
- 5
- -5 7
- 8 -6
- 6 -3
- 2 1
- -8 -5
Sample Output
- 8
Hint
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int inf = <<;
- const int dir = ;
- int dp[];
- struct s{
- int s, f;
- }arr[];
- int main(){
- int n;
- scanf("%d",&n);
- for(int i=;i<n;i++)
- scanf("%d%d",&arr[i].s,&arr[i].f);
- for(int i=;i<=;i++)
- dp[i] = -inf;
- dp[] = ;
- for(int i=;i<n;i++){
- if(arr[i].s < && arr[i].f < )
- continue;
- if(arr[i].s>){
- for(int j=;j>=arr[i].s;j--){
- if(dp[j-arr[i].s] > -inf){
- dp[j] = max(dp[j],dp[j-arr[i].s]+arr[i].f);
- }
- }
- }else {// arr[i].s < 0 arr[i].f>=0
- for(int j=arr[i].s;j<=+arr[i].s;j++){
- if(dp[j-arr[i].s] > -inf){
- dp[j] = max(dp[j],dp[j-arr[i].s]+arr[i].f);
- }
- }
- }
- }
- int ans = ;
- for(int i=;i<=;i++){
- if(dp[i]>=)
- ans = max(ans, dp[i]+i-);
- }
- printf("%d\n",ans);
- return ;
- }
对代码进行了优化。
首先是将dp从数组名变成了指针,令dp指向buf[100000]
这样、数组下标为负数时也不会越界。
优化关键在设置了两个变量,分别记录了有效区间的两个端点。
比如说 在处理第一个数的时候整个区间的都是无法达到的(除了原点)
而再像优化前的代码一样遍历一个200000的区间是无用的冗余。
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int inf = <<;
- int buf[];
- int *const dp = buf + ;
- int main(){
- int n, s, f;
- scanf("%d",&n);
- for(int i=;i<=;i++)
- buf[i] = -inf;
- dp[] = ;// dp[0] == buf[100000]
- int l = , r = ;// 有效区间的左端点和右端点
- for(int i=;i<n;i++){
- scanf("%d%d",&s,&f);
- if(s< && f <)//舍弃无用点
- continue;
- if(s > ){// 体积为正数 所以从大到小dp
- for(int i=r;i>=l;i--)
- dp[i+s] = max(dp[i+s],dp[i]+f);
- }else {
- for(int i=l;i<=r;i++)
- dp[i+s] = max(dp[i+s],dp[i]+f);
- }
- if(s>)
- r += s;
- else
- l += s;
- }
- int ans = ;
- for(int i=;i<=r;i++){
- if(dp[i] >= )
- ans = max(ans, dp[i]+i);
- }
- printf("%d\n",ans);
- return ;
- }
POJ 2184 Cow Exhabition的更多相关文章
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
- POJ 2184 Cow Exhibition (01背包的变形)
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...
- poj 2184 Cow Exhibition(背包变形)
这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...
- POJ 2184 Cow Exhibition 01背包
题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负 且 sum(x[k]) >= 0 sum(y[k] ...
随机推荐
- 数据库——MySQL——索引——索引原理及B+树
索引原理 我们使用索引,就是为了提高查询的效率,如同查书一样,先找到章,再找到章中对于的小节,再找到具体的页码,再到我们需要的内容. 事实上索引的本质就是不断缩小获取数据的筛选范围,找出我们想要的结果 ...
- 架构风格:你真的懂REST吗?
本文探讨如下几个问题: 什么是REST REST包含哪些约束 什么是RESTful 纯RESTful API的难点在哪里 如果你去搜索「什么是REST」的话,大部分情况下,你看到的基本都是RESTfu ...
- Spring 整合Mybatis dao原始方法
先看一下项目图,基本就理解了整合的内容 这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationC ...
- LeetCode 中级 - 优势洗牌(870)
给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 2: 输入: ...
- tomcat启动startup.bat一闪而过【亲测有效】
遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...
- Django中间件执行顺序
中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django ...
- DataSet和泛型之间相互转换
取数据的时候,存储过程返回了多个结果集,后台用DataSet去接收这几个结果集,然后接收之后,需要将结果集转换为不同的实体,于是下面的代码便出现了. /// <summary> /// 将 ...
- xshell安装教程
Xshell安装使用教程 Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议.Xshell 通过互联网到远程主机 ...
- ueditor 富文本编辑器 Uncaught TypeError: Cannot set property 'innerHTML' of undefined问题
ueditor.addListener("ready", function () { ueditor.setContent(‘内容'); });
- Python2和Python3
1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节 ...