POJ:3045-Cow Acrobats
Cow Acrobats
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6253 Accepted: 2345
Description
Farmer John’s N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.
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
Line 1: A single line with the integer N.
Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
- Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
OUTPUT DETAILS:
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.
解题心得:
- 有n头牛,每一只牛有重量和力量,现在n只牛叠罗汉,每只牛的风险等于他头上牛的重量总和减去这只牛的力量。问怎么排列,使最大风险牛的风险最小。有两种想法。
- 挑战上面说这是一个二分题,其实就是一个贪心啊,先假设每一只牛都放在最下面,算出风险,然后将风险最小的那个放在下面,这样风险大的就可以减去已经安放了风险小的牛的重量,这样得到的风险就会逐步减小,最后风险最大的就是最小的。
- 也可以这样想,假设有两只牛a和b,a的重量为w1,力量为p1,b的重量为w2,力量为p2,如果将a放在下面,a的风险就是p1-w2,如果将b放在下面,风险就是p2-w1。如果a的风险比b的风险更小就会得到一个关系式:p1-w2
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn =5e4+100;
struct COW{
int va,w;
long long press;
}cow[maxn];
int n;
bool cmp(COW a,COW b) {
return a.press < b.press;
}
void init() {
scanf("%d",&n);
long long sum = 0;
for(int i=0;i<n;i++) {
scanf("%d%d",&cow[i].w,&cow[i].va);
sum += cow[i].w;
}
for(int i=0;i<n;i++) {
cow[i].press = sum - cow[i].w - cow[i].va;
}
sort(cow,cow+n,cmp);
}
long long get_oder() {
long long cut = 0;
long long ans = -10*(long long)0x7f7f7f7f;
for(int i=0;i<n;i++) {
cow[i].press = cow[i].press - cut;
ans = max(ans,cow[i].press);
cut += cow[i].w;
}
return ans;
}
int main() {
init();
long long ans = get_oder();
printf("%lld\n",ans);
return 0;
}
第二种想法的代码:
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn =5e4+100;
struct COW{
int va,w;
}cow[maxn];
int n;
bool cmp(COW a,COW b) {
return a.va + a.w > b.va + b.w;
}
void init() {
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d%d",&cow[i].w,&cow[i].va);
}
sort(cow,cow+n,cmp);
}
long long get_oder() {
long long sum = 0;
long long Max = -10*(long long)0x7f7f7f7f7f;
for(int i=n-1;i>=0;i--) {
long long temp = sum - cow[i].va;
sum += cow[i].w;
Max = max(Max,temp);
}
return Max;
}
int main() {
init();
long long ans = get_oder();
printf("%lld\n",ans);
return 0;
}
POJ:3045-Cow Acrobats的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- 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 (二分,或者贪心)
一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...
- POJ:3268-Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26184 Accepted: 11963 De ...
- POJ3045 Cow Acrobats —— 思维证明
题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- poj3045 Cow Acrobats (思维,贪心)
题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...
- 【POJ - 3045】Cow Acrobats (贪心)
Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...
随机推荐
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- AWS ELB Sticky Session有问题?别忘了AWSELB cookie
我们的产品中有两个Module,分别部署在独立的Linux机器上,Module 1需要向Module 2发起Http请求来获得服务.由于Module 2有多台,因此我们会在Module 2前部署一台负 ...
- 解决javascript四舍五入不准确
function roundFixed(num, fixed) { var pos = num.toString().indexOf('.'), decimal_places = num.toStri ...
- lunix重启service network restart错误Job for network.service failed. See 'system 或Failed to start LSB: Bring
1.mac地址不对 通过ip addr查看mac地址,然后修改cd /etc/sysconfig/network-scripts/目录下的文件里面的mac地址 2.通过以下方法 systemctl s ...
- $("#Upfile").MultiFile();
Jquery的multifile 1.多文件上传: 2.如上几个验证不重复,和限制上传数量的验证显示的是英文,改成中文文本时,如果不用国标解码,到时候提示框会出现乱码现象.所以一般需要中文显示的时候, ...
- 如何用C# 动态创建Access数据库和表?
记得以前要动态的创建Access数据库的mdb文件都是采用DAO,用VC开发,一大堆的API,很是麻烦.而且以前工作中需要全新的access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新 ...
- 增量数据同步中间件DataLink分享(已开源)
项目介绍 名称: DataLink['deitə liŋk]译意: 数据链路,数据(自动)传输器语言: 纯java开发(JDK1.8+)定位: 满足各种异构数据源之间的实时增量同步,一个分布式.可扩展 ...
- 少年开始学习c#编程,过路的大神请担待!
这应该真正算开始学习编程, 安装好了 linux, 开通了博客员的博客, 同时今天也需要把sublime安好, 安装MonoDevelop Codeblocks mysql-workbench 配置好 ...
- 微信的 rpx
微信小程序新单位rpx与自适应布局 rpx是微信小程序新推出的一个单位,按官方的定义,rpx可以根据屏幕宽度进行自适应,在rpx出现之前,web页面的自适应布局已经有了多种解决方案,为什么微信还捣 ...
- 使用selenium 检测js报错
背景:接到一个需求,想检测页面是否能检测js报错,何为js报错,如下图所示,在控制台中,使用console,如果有js报错,就会出现错误 如何检测,简单版操作,打开一个url,使用manage获取浏览 ...