http://www.lydsy.com/JudgeOnline/problem.php?id=1629

这题我想了很久都没想出来啊。。。

其实任意两头相邻的牛交换顺序对其它牛是没有影响的。。

那么我们考虑哪个在前。。(假设现在是待交换的是 a和b,a<b)

当重b-力a < 重a-力b时,就不需交换,否则交换。。

这个贪心自己想想就懂了的。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
int n;
struct nod { int x, y; } p[N];
bool cmp(const nod &a, const nod &b) { return b.x-a.y<a.x-b.y; }
int main() {
read(n);
for1(i, 1, n) read(p[i].x), read(p[i].y);
sort(p+1, p+1+n, cmp);
int sum=0, ans=(~0u>>1)+1;
for3(i, n, 1) {
ans=max(ans, sum-p[i].y);
sum+=p[i].x;
}
print(ans);
return 0;
}

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 within 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

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.

HINT

Source

【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)的更多相关文章

  1. bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】

    仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...

  2. BZOJ 1629: [Usaco2007 Demo]Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  3. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  4. BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...

  5. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

    题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...

  6. [USACO2007 Demo] Cow Acrobats

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1629 [算法] 贪心 考虑两头相邻的牛 , 它们的高度值和力量值分别为ax , ay ...

  7. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序

    Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个 ...

  8. bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路(置换)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...

  9. bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】

    至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...

随机推荐

  1. WCF学习笔记之序列化

    DataContractAttribute 与 DataMenberAttribute DataContractAttribute该特性只能用于枚举.类和结构体,而不能用于接口:又因为DataCont ...

  2. 关于图片无缝拼接的学习(PTGui)

    一.简介 在用到单反.无人机.手机等拍照工具,需要无缝拼接. 二.下载 官网:http://www.ptgui.com/download.html 其他:http://pan.baidu.com/sh ...

  3. 用vue开发顶端粘滞效果的页面

    概述 通常一个长的页面,需要滚动浏览,有部分重要信息会随着滚动而看不见,因此需要粘滞在顶端,而又不影响滚动浏览,这个demo基于vue2,实现这个需求. 详细 代码下载:http://www.demo ...

  4. jQuery方法一览

    Attribute: $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(”img”).attr({src:”test.jpg”,alt:”test Image”}); ...

  5. Jenkins部署java项目实例02

    源码管理 选择 “git”, Repository URL填写你的git地址,并配置对应的key,大家最好事前先在jenkins这台机器上能够直接远程git操作私有仓库 构建触发器.构建环境.Pre ...

  6. Ubuntu开机自动禁用无线网络

    让ubuntu开机自动禁用无线网络. 1.自启动脚本 将下面这条禁用无线网络的命令添加到“启动应用程序“中,这样开机时无线网络就会被自动禁用. dbus-send --system --type=me ...

  7. fatal error LNK1123: 转换到 COFF 期间失败:文件无效或损坏

    问题出现背景: 原本电脑里是装着VS2015的,其使用的是.NET 4.5,当再安装VS2010之后,不能与当前的.NET平台兼容.卸载VS2015时,不会恢复.NET 4.0. l 当VS2015安 ...

  8. js处理url

    需求:用js获得url的电话号码和状态 针对url地址:http://www.deikang.com/index.php?tel=15811296111&status=1&id=100 ...

  9. webqq协议分析之~~~~验证是否需要验证码

    对于小黄鸡我想大家(喜欢在群里bb的人...)肯定一点都不陌生,那段时间大家在群里对小鸡是各种调戏啊,都有点不忍直视.那时我便想能不能自己也做个呢,后来想想还是算了吧,自己技术太渣渣,然后就不了了之. ...

  10. VS2008 格式化时候乱码 或者 为全为0

    前几天一直发现写入文件的数据全是0 ,找了非常长时间发现问题在以下的地方: P:a是一个float数,如31.000000,然后运行以下的格式化语句时,结果str的值全是0. (我知道讲float格式 ...