洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
题目大意:
n头牛,上山时间为u(i),下山为d(i).
要求每一时刻最多只有一头牛上山,一头牛下山。
问每头牛都上下山后花费最少时间。
题解:贪心
推了推样例,发现上山时间一定,那找个下山最快
的当最后一头山上的牛。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define N 25009
using namespace std; int n; LL ans;
int hh=; struct Cows{
int u,d;
}c[N]; bool cmp(Cows a,Cows b){
return a.u<b.u;
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&c[i].u,&c[i].d);
ans+=c[i].u;hh=min(hh,c[i].d);
}
cout<<ans+hh;
return ;
}
70骗分
发现正解和上面的结论是差不多的。
a、总上山时间大于总下山时间
这说明牛的总上山时间是恒定的,一定要记录答案的。
记录答案的还有最后一头牛的下山时间,所以最终结果是
总上山时间+牛最快的下山时间
b、总下山时间大于总上山时间
这说明牛的下山时间是恒定的,一定要记录答案的。
记录答案的还有一头牛的上山时间,所以最终的结果
是:总下山时间+最快牛的上山时间
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std; int n; LL su,sd,mnu,mnd; int main(){
scanf("%d",&n);
mnu=mnd=;
for(int i=;i<=n;i++){
int up,down;
scanf("%d%d",&up,&down);
su+=up;sd+=down;
if(up<mnu)mnu=up;
if(down<mnd)mnd=down;
}
if(su>sd)cout<<su+mnd;
else cout<<sd+mnu;
// cout<<max(su+mnd,sd+mnu);
return ;
}
洛谷 P1561 [USACO12JAN]爬山Mountain Climbing的更多相关文章
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...
- P1561 [USACO12JAN]爬山Mountain Climbing
P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher q ...
- 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing
我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...
- [USACO12JAN]爬山Mountain Climbing
题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to s ...
- 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- 洛谷 P1361 小猫爬山
题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...
- 洛谷——P1361 小猫爬山
https://www.luogu.org/problem/show?pid=1361#sub 题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲 ...
- 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share
P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...
- 洛谷 P3041 [USACO12JAN] Video Game Combos
题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...
随机推荐
- H5新特性---新应用
1.持久化本地存储 可以不通过第三方插件实现数据的本地存储 2.WebSocket 页面之间可以双向通信 3.服务器推送事件(SSE) 从Web服务器将消息推送给浏览器(在手机中常见) 例如: < ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
- AngularJS Source code
Angular.js 辅助函数 lowercase hasOwnProperty uppercase mannualLowercase mannualUppercase isArrayLike for ...
- 如何在深层嵌套ngRepeat中获取不同层级的$index
<ul class="list-group" ng-repeat="item in vm.appData" ng-init="outerInde ...
- 使用springmvc时报错org.springframework.beans.NullValueInNestedPathException: Invalid property 'department' of bean class [com.atguigu.springmvc.crud.entities.Employee]:
使用springmvc时报错 org.springframework.beans.NullValueInNestedPathException: Invalid property 'departmen ...
- jQuery中hover和blur使用代理delegate无效的解决方法
今天就遇到了这样的小问题: $(document).ready(function(){ $('.status_on').hover(function(){ $(this).html('点击禁用'); ...
- int(3)与int(11)的区别
注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的.其实当我们在选择使用int的类型的时候 ...
- python 爬虫003-正则表达式简单介绍
正则表达式,简单的说就是用一个“字符串”来描述一个特征,然后去验证另外一个“字符串”是否符合这个特征. 正则表达式在线测试工具 http://tool.chinaz.com/regex 实例一,判断字 ...
- datagrid中用tooltip
function msgFormat(value,row){ value = value.replace(/ /g," "); return '<span title='+ ...
- linux的find命令详解
find命令是用来在给定的目录下查找符合给定条件的文件 find [OPTIONS] [查找起始路径] [查找条件] [处理动作] 一.OPTIONS参数 -P.-L.-H:控制软连接的对待方式, ...