[USACO2007 Demo] Cow Acrobats
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1629
[算法]
贪心
考虑两头相邻的牛 , 它们的高度值和力量值分别为ax , ay , bx , by
我们发现 , 当ax + ay < bx + by时 , x排在前面比y排在前面更优
也就是说 , 当序列中有相邻的牛使得ax + ay >= bx + by时 , 可以通过交换两头牛使得答案更优
综上 , 按牛的(高度值 + 力量值)以关键字升序排序 , 即可
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 50010
typedef long long LL;
const LL inf = 1e18; struct info
{
LL x , y;
} a[MAXN]; int n; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool cmp(info a , info b)
{
return a.x + a.y < b.x + b.y;
} int main()
{ read(n);
for (int i = ; i <= n; i++)
{
read(a[i].x);
read(a[i].y);
}
sort(a + , a + n + , cmp);
LL ans = -inf , now = ;
for (int i = ; i <= n; i++)
{
chkmax(ans , now - a[i].y);
now += a[i].x;
}
cout<< ans << '\n'; return ; }
[USACO2007 Demo] Cow Acrobats的更多相关文章
- BZOJ1629: [Usaco2007 Demo]Cow Acrobats
1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 601 Solved: 305[Su ...
- 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 ...
- 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...
- bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】
仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...
- 【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats
[题目大意] 有n个头牛,给出体重和力量.每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大值的最小值. [思路] 贪心.一开始脑补的贪心是体重大的先放下面,体重相同的根据力 ...
- BZOJ1631: [Usaco2007 Feb]Cow Party
1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 459 Solved: 338[Submit ...
- BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 387 Solved: 215[S ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- BZOJ1638: [Usaco2007 Mar]Cow Traffic 奶牛交通
1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 571 Solved: 199 ...
随机推荐
- 自动调整文字高度With what should I replace the deprecated sizeWithFont:contrainedToSize:lineBreakMode method?
自动调整文字的高度: ios 2.0 ~ 7.0以下: UILabel *orgnizationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ...
- 【kotlin】报错 Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List<String>?
报错如下: 解决如下: 另一种情况: 解决如下:
- 临远的spring security教程
为啥选择Spring Security 欢迎阅读咱们写的Spring Security教程,咱们既不想写一个简单的入门教程,也不想翻译已有的国外教程.咱们这个教程就是建立在咱们自己做的OA的基础上,一 ...
- JDBC连接MySQL数据库的示例代码
虽然老调,但有时也需要用一下,从网上找的原型修改了下放这. import java.sql.Connection; import java.sql.DriverManager; import java ...
- css设置图片居中、居左、居右
CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...
- NSArray中存的是实体时的排序
NSArray中存储的是实体时的排序 by 伍雪颖 NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKe ...
- 【翻译自mos文章】怎么找到OGG Director Server使用的数据库和username?
APPLIES TO: Management Pack for Oracle GoldenGate - Version: 1.0.0.0 - Release: 1.0 Information in t ...
- sql select(A.B)拼接
需要做的工作:把DBtable里边的某两个字段,(当然可以更多)或者不同表,道理类似,用某个符号拼接起来. 比如(Table.A).(Tables.B) oracle里边可以这样写,sql没试: se ...
- 关于 underscore 中模板引擎的应用演示样例
//关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset=" ...
- dp求顺序hdu1160
题意是仅仅求一次的顺序.先依照速度从大到小排序,速度想等到按体重增序排列. 然后基本就变成了求已定顺序序列的最长递增序列递增,跟那个求一致最大序列和的基本一致. dp[i]里存储的是到当前i最大的递增 ...