传送门

题目大意:

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的更多相关文章

  1. 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

    https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...

  2. P1561 [USACO12JAN]爬山Mountain Climbing

    P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher q ...

  3. 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing

    我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...

  4. [USACO12JAN]爬山Mountain Climbing

    题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to s ...

  5. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  6. 洛谷 P1361 小猫爬山

    题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...

  7. 洛谷——P1361 小猫爬山

    https://www.luogu.org/problem/show?pid=1361#sub 题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲 ...

  8. 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share

    P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...

  9. 洛谷 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 ...

随机推荐

  1. linux驱动调试--修改系统时钟终端来定位僵死问题【转】

    本文转载自:http://blog.chinaunix.net/uid-20671208-id-4940381.html 原文地址:linux驱动调试--修改系统时钟终端来定位僵死问题 作者:枫露清愁 ...

  2. SpringBoot Bean作用域

    Bean在一般容器中都存在以下2种作用域: singleton 默认值,IoC容器只存在单例 prototype 每当从IoC容器中取出一个Bean,则创建一个新的Bean 在Web容器中存在4种作用 ...

  3. bower安装使用、git安装、node安装、weui安装开发

    bower安装使用以及git安装 bower需要:node 和 git 1.Git安装:(选择第二项:Use Git from the Windows Command Prompt)2.node安装: ...

  4. Java条件语句之 if

    生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种“需要先判断条件,条件满足后才执行的情况”,就可以使用if 条件语 ...

  5. Windows下使用MinGW在命令行编译运行C++程序

    之前学习C语言的时候都是用IDE类似CodeBlocks的工具写完直接编译运行的,今天突然心血来潮,自己下一个编译器,在命令行下,编译运行C++程序,了解一下编译过程. 一.安装编译器 首先你需要下载 ...

  6. ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐

    环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...

  7. chrome跨域拓展工具

    下载chrome跨域扩展工具 1) http://crx.2333.me/ 扩展程序id:nlfbmbojpeacfghkpbjhddihlkkiljbi

  8. uva11827gcd

    gcd裸题,不过输入要注意gets会tle,要用快速读入 #include<map> #include<set> #include<cmath> #include& ...

  9. time模块详解

    本文转自这里: 在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: 在Python中,通常有 ...

  10. 【枚举】【最小生成树】【kruscal】bzoj3754 Tree之最小方差树

    发现,若使方差最小,则使Σ(wi-平均数)2最小即可. 因为权值的范围很小,所以我们可以枚举这个平均数,每次把边权赋成(wi-平均数)2,做kruscal. 但是,我们怎么知道枚举出来的平均数是不是恰 ...