POJ-3045 Cow Acrobats (C++ 贪心)
Description
The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.
Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.
Input
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
设Di表示第i头奶牛的难受值,Wi表示第i头奶牛的体重,Si表示第i头奶牛的力量,令i,j相邻,且Wi+Si>Wj+Sj,设∑表示i和j上面的奶牛的重量之和
当i在j的上方时有
- Di=∑−Si
①
- Dj=∑+Wi−Sj
②
当j在i的上方时有
- Di=∑+Wj−Si
③
- Dj=∑−Sj
④
显然我们可以得到
③>①,②>④,②>③
这里面②最大,所以如果我们让i在j的上方最终答案一定不会更优,即证得此贪心策略的正确性。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct cow{
int w,p,sum;
};
int cmp(cow c1,cow c2)
{
return c1.sum<c2.sum;
}
cow c[50100];
int main()
{
int n;
//int w[10100],p[10100];
//memset(w,0,sizeof(w));
//memset(p,0,sizeof(p));
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&c[i].w,&c[i].p);
c[i].sum=c[i].p+c[i].w;
}
//int sum[10100];
sort(c,c+n,cmp);
int ans=-0x3f3f3f3f;
int sum=0;
for(int i=0;i<n;i++)
{
//ans+=c[i].p-c[i-1].w;
ans=max(ans,sum-c[i].p);
sum+=c[i].w;
}
printf("%d\n",ans);
}
return 0;
}
POJ-3045 Cow Acrobats (C++ 贪心)的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- POJ - 3045 Cow Acrobats (二分,或者贪心)
一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...
- poj 3045 Cow Acrobats(二分搜索?)
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- POJ 3045 Cow Acrobats
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- POJ 3045 Cow Acrobats (最大化最小值)
题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风 ...
- 【POJ - 3045】Cow Acrobats (贪心)
Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...
- 【POJ3045】Cow Acrobats(贪心)
BUPT2017 wintertraining(16) #4 B POJ - 3045 题意 n(1 <= N <= 50,000) 个牛,重wi (1 <= W_i <= 1 ...
- 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...
- BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...
随机推荐
- 安卓APP测试容易忽略的地方
我们手机APP测试,主要针对的是android和ios两大主流操作系统,总体上来说android手机型号.版本多,bug也多:ios相对bug少.下面就针对Android说一下最容易忽略的测试点吧. ...
- Python - Seaborn可视化:图形个性化设置的几个小技巧
1 概述 在可视化过程中,经常会对默认的制图效果不满意,希望能个性化进行各种设置. 本文通过一个简单的示例,来介绍seaborn可视化过程中的个性化设置.包括常用的设置,如: 设置图表显示颜色 设置图 ...
- Hibernate注解-类级别注解
- 关于C#开发 windows服务进程
最近在做一个物联网项目,其中有一个模块是需要实现热水工程的自动化补水和回水功能 实现的方式有多种,我选用了VS C#的Windows服务方式. 首先是创建一个windows服务项目(名称随你喜欢的规范 ...
- BeautifulSoup练习第一节
一.pip install beautilfulsoup4 二.主要使用html.parser这个python标准库 三.打印首页博客的时间.打印摘要 # coding:utf-8from bs4 i ...
- 大数问题:打印从1到最大的n位数
//打印从1到最大的n位数:大数问题,用字符串表示数字来避免溢出 bool increment(char* number){ bool isOverFlow = false; int nTakeOve ...
- matlab-常用函数(4)
find()函数的用法 搜索矩阵中指定数值的下标,若指定值有多个,则返回多个下标: x = 1:2:20 x = 1 3 5 7 9 11 13 15 17 19 k = find(x==13) k ...
- Apache Camel之FTP组件学习
写在最前面 哎,最近提了离职,手头的活也基本上清理的差不多了.想着这个把月可以舒服的晃悠晃悠的离开,但是运维的小伙伴总是不架势,走之前还是提了个新需求. 先说下需求吧,我们的系统概括的讲就是一个接口系 ...
- CPU和GPU的差别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt317 首先需要解释CPU和GPU这两个缩写分别代表什么.CPU即中央处理器, ...
- 数据库学习任务四:数据读取器对象SqlDataReader、数据适配器对象SqlDataAdapter、数据集对象DataSet
数据库应用程序的开发流程一般主要分为以下几个步骤: 创建数据库 使用Connection对象连接数据库 使用Command对象对数据源执行SQL命令并返回数据 使用DataReader和DataSet ...