Description

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 头牛完成旅程的最短时间。

Input

第一行,一个整数N

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

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

Output

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

Sample Input

3
6 4
8 1
2 3

Sample Output

17

题解

贪心策略

max(总上山时间+最快奶牛下山时间,总下山时间+最快奶牛上山时间)

算法构造

1.设置集合$F$、$M$、$S$:先让$F$中奶牛的爬山,再让$M$中奶牛的爬山,最后让$S$中奶牛的爬山。

2.对第$i$件,若$U[i]>D[i]$,则归入$S$;若$U[i]=D[i]$,则归入$M$,否则归入$F$。

3.对$F$中的元素按$U[i]$升序排列,$S$中的按$D[i]$降序排列。

证明思路

1.$F$中的能“拉开”$John$、$Don$让同一头奶牛上下山的结束时刻,为后面的奶牛爬山“拉开时间差”,利于节省总时间。$S$中的刚好相反。因而,$F$中元素放在最前一定是最优策略之一。

2.$F$中$U[i]$小的前置,可以缩短开始时$B$的空闲时间,但会使F所有奶牛“拉开的时间差”缩短。不过可以证明,后者带来的损失不大于前者获得的优势。对称地,对$S$也一样。因而步骤$3$是可行的。

思路很简单,就是实际编写比较麻烦。

 //It is made by Awson on 2017.10.18
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Abs(x) ((x) < 0 ? (-(x)) : (x))
using namespace std;
const int INF = ~0u>>; int a, b, c = INF, d = INF, n, u, v; void work() {
scanf("%d", &n);
while (n--) {
scanf("%d%d", &u, &v);
a += u, b += v;
c = Min(c, u), d = Min(d, v);
}
printf("%d\n", Max(a+d, b+c));
}
int main() {
work();
return ;
}

[USACO 12JAN]Mountain Climbing的更多相关文章

  1. BUGKU (Mountain climbing)

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

  2. P1561 [USACO12JAN]爬山Mountain Climbing

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

  3. [USACO12JAN]爬山Mountain Climbing

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

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

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

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

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

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

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

  7. How to do Mathematics

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

  8. BUGKU-逆向(reverse)-writeup

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

  9. 【伪随机数】【搜索】【RE】【bugku】mountainclimbing WriteUp

    Mountain Climbing WP 拿到题首先熟练地查个壳再用各种脱壳工具脱个壳. 脱壳之后熟练地双击感受一下出题者的恶意: 根据字面意思得知,是要根据一系列的操作来得到收益最大值,于是用ida ...

随机推荐

  1. 敏捷冲刺每日报告——Day5

    1.情况简述 Alpha阶段第一次Scrum Meeting 敏捷开发起止时间 2017.10.29 00:00 -- 2017.10.30 00:00 讨论时间地点 2017.10.29晚6:00, ...

  2. Beta敏捷冲刺每日报告——Day3

    1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.4 00:00 -- 2017.11.5 00:00 讨论时间地点 2017.11.4 晚9:30,电话会议会议 ...

  3. java封装的概念

    继承.封装.多态.抽象是面向对象编程的四大基本概念,其中封装尤为重要,因为从我们学习JAVA开始,就基本上接触了封装,因为JAVA中的所有程序都是写在类中的,类也能当做一种封装. 在面向对象中封装是指 ...

  4. 【iOS】swift-文字宽度的计算

    如图所示,需要sectionView的标题宽度可以动态变化 举例说明: 只需在tableView的代理方法 func tableView(tableView: UITableView, viewFor ...

  5. Angular 学习笔记 ( 链接服务器 )

    ng 是做前端开发的, 所以通常我们会配上一个 API server. 本地调试时通常使用 proxy https://github.com/angular/angular-cli/blob/mast ...

  6. 数据结构与算法 —— 链表linked list(03)

    继续关于linked list的算法题: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素使得每个元素只留下一个. 案例: 给定 1->1->2,返回 1->2 给定  ...

  7. 配置Android开发环境遇到的问题

    1.给Eclipse设置android的SDK位置时,出现这个:This Android SDK requires Andr...ate ADT to the latest 一个升级ADT到指定版本或 ...

  8. python/SQLAchemy

    python/SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数 ...

  9. python Django之文件上传

    python Django之文件上传 使用Django框架进行文件上传共分为俩种方式 一.方式一 通过form表单进行文件上传 #=================================== ...

  10. uva 1411 Ants

    题意: 一个平面上有n个黑色的点,n个白色的点,要求黑色的点与白色点之间一一配对,且线段之间不相交. 思路: 线段不相交并不好处理,想了很久想不出,所以看了蓝书的讲解. 一个很明显的结论是,不相交的线 ...