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. Beta Scrum博客集

    听说 Beta Scrum Day 1

  2. 20162308 实验一《Java开发环境的熟悉》实验报告

    a 20162308 实验一<Java开发环境的熟悉>实验报告 实验内容 使用JDK编译.运行简单的Java程序. 使用IDEA 编辑.编译.运行.调试Java程序. 实验要求 没有Lin ...

  3. 201621123031 《Java程序设计》第1周学习总结

    作业01-Java基本概念 1.本周学习总结 1.本周学习内容:Java发展史(简述).Java语言特点.JDK .JRE .JVM .Java的开发步骤.Java开发工具. 2.关键概念之间的联系: ...

  4. 《高级软件测试》Windows平台Jira的配置

    昨天完成了Jira的下载,很开心地去睡觉等明天天亮秒配环境愉快进行使用,撰写文档,开始徜徉于软件管理测试实践,早日走向代码巅峰. 我们把安装和配置的过程来走一遍. 安装完成汤姆猫长这样子: 安装Jir ...

  5. LeetCode & Q14-Longest Common Prefix-Easy

    String Description: Write a function to find the longest common prefix string amongst an array of st ...

  6. WPF treeview扩展

    记录一下工作中遇到的问题,以便以后忘记了可以来看. 在工作中遇到一个问题,就是要实现类型如下的界面,没有使用Telerik和Dev库.本来最开始是想使用Datagrid,但不知道怎么实现treevie ...

  7. Linux系统把/home重新挂载到其他硬盘或分区

    一开始没有做好规划,导致/home空间不足,再加上分区表不是GPT,导致无法扩展超过2T,因此需要重新划分一块更大的硬盘给/home. 1.把新挂载的4T硬盘进行分区和格式化 2.创建目录 sudo ...

  8. win10 如何让其他机器访问自己机器上的mysql

    一.修改mysql 1.执行sql GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Abc1234%' WITH GRANT OPTI ...

  9. sql server 常用的查询语句

    最近在加强sql 语句的学习,整理一下基本语法,现在记录下 select * from dbo.cangku where city='河南' select  distinct(city), cangk ...

  10. python中文编码坑

    打印list的时候unicode对象不会自动转为中文!