题目描述

Farmer John has discovered that his cows produce higher quality milk when they are subject to strenuous exercise. He therefore decides to send his N cows (1 <= N <= 25,000) to climb up and then back down a nearby mountain!

Cow i takes U(i) time to climb up the mountain and then D(i) time to climb down the mountain. Being domesticated cows, each cow needs the help of a farmer for each leg of the climb, but due to the poor economy, there are only two farmers available, Farmer John and his cousin Farmer Don. FJ plans to guide cows for the upward climb, and FD will then guide the cows for the downward climb. Since every cow needs a guide, and there is only one farmer for each part of the voyage, at most one cow may be climbing upward at any point in time (assisted by FJ), and at most one cow may be climbing down at any point in time (assisted by FD). A group of cows may temporarily accumulate at the top of the mountain if they climb up and then need to wait for FD's assistance before climbing down. Cows may climb down in a different order than they climbed up.

Please determine the least possible amount of time for all N cows to make the entire journey.

农场主约翰发现他的奶牛剧烈运动后产奶的质量更高,所以他决定让N头(1 <= N <= 25,000)奶牛去附近爬山再返回来。

第i头奶牛用时U(i)爬上山,用时D(i)下山。作为家畜,奶牛们每段路都要有农夫的帮助,可是由于经济疲软,农场里只有两个农夫John和Don。John计划引导奶牛爬山,Don引导奶牛下山。虽然每个奶牛都需要向导,但每段旅途只有一名农夫。所有任何时刻只有一头奶牛爬山也只能有一头奶牛下山,奶牛爬上山后,可以暂时停留在山顶上等待Don的帮助。奶牛上山的顺序和下山的顺序不一定要相同。

请计算出所有N 头牛完成旅程的最短时间。

输入输出格式

输入格式:

第一行,一个整数N

第2 到第N+1 行,每行两个用空格隔开的整数U(i)和D(i)。

(1 <= U(i), D(i) <= 50,000).

输出格式:

一行一个整数,表示所有N 头牛完成旅程的最短时间。

输入输出样例

输入样例#1:

  1. 3
  2. 6 4
  3. 8 1
  4. 2 3
输出样例#1:

  1. 17
    随机跳题。。。

我们可以证明,本题的最优解即为max(总上山时间+最快奶牛下山时间,总下山时间+最快奶牛上山时间)

  1.  

首先我们可以知道,当上山总时间大于下山总时间时,所有奶牛总上山时间恒定。

  1.  

而最后总有一只奶牛在上山总时间后在山顶,要想最优,只要让在山上的那只奶牛下山时间最小即可。

  1.  

下山同理,当上山总时间小于下山总时间时,所有奶牛总下山时间恒定。

  1.  

在下山之前,总有一只奶牛在山顶(否则他还没上山就下山可能吗),要想最优,只要让在山上的那只奶牛上山时间最小即可

  1.  

边读边做,每次读入累加上山和下山时间和记录最小上山下山时间最后去max即可

  1.  
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int u,d,p,q,minu=2e9,mind=2e9,n;
  7. int main()
  8. {int i;
  9. cin>>n;
  10. for (i=;i<=n;i++)
  11. {
  12. scanf("%d%d",&u,&d);
  13. p+=u;q+=d;
  14. if (u<minu) minu=u;
  15. if (d<mind) mind=d;
  16. }
  17. cout<<max(p+mind,q+minu);
  18. }

[USACO12JAN]爬山Mountain Climbing的更多相关文章

  1. P1561 [USACO12JAN]爬山Mountain Climbing

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

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

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

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

    传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...

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

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

  5. BUGKU (Mountain climbing)

    UPX壳,手脱后打不开,可能是在windows10脱壳的缘故吧.放在XP虚拟机上手脱可能会好,但是提示缺少dll.无奈,智能用工具了. 用的这个,感觉蛮好用的. 先打开程序.随便输入一个数,提示err ...

  6. [USACO 12JAN]Mountain Climbing

    Description Farmer John has discovered that his cows produce higher quality milk when they are subje ...

  7. How to do Mathematics

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/30087053/answer/47815698来源 ...

  8. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  9. BUGKU-逆向(reverse)-writeup

    目录 入门逆向 Easy_vb Easy_Re 游戏过关 Timer(阿里CTF) 逆向入门 love LoopAndLoop(阿里CTF) easy-100(LCTF) SafeBox(NJCTF) ...

随机推荐

  1. C语言博客作业—函数

    一.PTA实验作业 题目1:使用函数输出水仙花数 1. 本题PTA提交列表 2. 设计思路 (1)首先定义函数narcissistic(number)判断number是否为水仙花数: (2)narc用 ...

  2. alpha-咸鱼冲刺day1

    一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花!) 四,问题 ...

  3. Alpha冲刺第十二天

    Alpha冲刺第十二天 站立式会议 项目进展 项目核心功能,如学生基本信息管理模块,学生信用信息模块,奖惩事务管理模块等等都已完成,测试工作大体结束. 问题困难 项目结束后对项目的阶段性总结缺乏一定的 ...

  4. 在Windows上安装「算法 第四版」组件

    这篇文档将向你介绍如何在Windows系统上安装本书将用到的Java开发环境,同时我们也提供了一个手把手的.使用我们提供的DrJava工具或者用命令行来创建.编译和运行你的第一个Java程序的手册,这 ...

  5. ThreadLocal源码分析:(三)remove()方法

    在ThreadLocal的get(),set()的时候都会清除线程ThreadLocalMap里所有key为null的value. 而ThreadLocal的remove()方法会先将Entry中对k ...

  6. linux下面的打包压缩命令

    tar命令 tar [-cxtzjvfpPN] 文件与目录 ....linux下面压缩之前要把一堆文件打个包再压缩,即使只有一个文件也需要打个包.例子:tar czvf 1.tar.gz hello. ...

  7. PHP trait

    ps:由于PHP是单继承的,无法继承多个类所以可以用triat(关键字,特性)来命名达到子类继承多个父类的效果:暂且理解为类吧.class = trait <?php trait A { pub ...

  8. LAMP 搭建

    p { margin-bottom: 0.25cm; line-height: 120% } LAMP 搭建 承 Ubuntu 17.10.1安装, 定制. 参考 电子工业出版社, Ubuntu完美应 ...

  9. eclipse版本对应的jdk版本

    Installing Eclipse is relatively easy, but does involve a few steps and software from at least two d ...

  10. 工作笔记 | Visual Studio 调用 Web Service

    引言 最近笔者负责ERP财务系统跟中粮集团财务公司的财务系统做对接,鉴于ERP系统中应付结算单结算量比较大,而且管理相对集中,ERP系统与中粮财务公司的支付平台系统对接,实现银企直联,将网银录入的环节 ...