洛谷 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 ...
随机推荐
- 20145217《信网络对抗》逆向与BOF基础实践
20145217<信网络对抗>逆向与BOF基础实践 内容: 一.简单机器指令,汇编语言 1.'objdump -d xxx|more'反汇编命令查看机器代码,'cat'显示文件内容,'xx ...
- 去掉xml中的空格和换行符
有时在拼接xml或是导入xml格式文件时,会无缘无故出现很多空格符合换行符,导致在转换json时会报各种错误,特此在网上找到了一中比较实用的方法: strxml = Regex.Replace(str ...
- Linux系统下wget命令的使用教程
一.Linux wget简介 wget是linux上的命令行的下载工具.这是一个GPL许可证下的自由软件.Linux wget支持HTTP和FTP协议,支持代理服务器和断点续传功能,能够自动递归远程主 ...
- PAT1054. The Dominant Color (20)
#include <iostream> #include <map> using namespace std; int n,m; map<int,int> imgM ...
- java XML解析
package com.kpsh.myself; import java.io.File;import java.io.FileInputStream;import java.util.List; i ...
- 使用mybatis报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.String]
报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.St ...
- vue2 遇到的问题汇集ing
1 .子路由 { path: '/order-list', //订单列表 name: "order-list", component(resolve) { require.ensu ...
- 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".'
引用:http://www.linuxidc.com/Linux/2015-02/114116.htm Yii2高级版本复制新项目会遇到下面的报错信息: exception 'yii\base\Inv ...
- yii定时任务(linux)
yii写好了,计划任务,那么需要测试,测试地址大致: 我的测试文件名称:TestControllers.php, windows环境下: 我的项目在f:\www下, CMD: f:/www/ php ...
- android6.0、7.0、8.0新特性总结之开发应用时加以考虑的一些主要变更。
android6.0 参考一:简书Android 6.0 新特性详解 参考二:关于Android6.0以上系统的权限问题 参考三:值得你关注的Android6.0上的重要变化(一) 参考四:值得你关注 ...