[USACO 12JAN]Mountain Climbing
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
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的更多相关文章
- BUGKU (Mountain climbing)
UPX壳,手脱后打不开,可能是在windows10脱壳的缘故吧.放在XP虚拟机上手脱可能会好,但是提示缺少dll.无奈,智能用工具了. 用的这个,感觉蛮好用的. 先打开程序.随便输入一个数,提示err ...
- P1561 [USACO12JAN]爬山Mountain Climbing
P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher q ...
- [USACO12JAN]爬山Mountain Climbing
题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to s ...
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...
- 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...
- 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing
我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...
- How to do Mathematics
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/30087053/answer/47815698来源 ...
- BUGKU-逆向(reverse)-writeup
目录 入门逆向 Easy_vb Easy_Re 游戏过关 Timer(阿里CTF) 逆向入门 love LoopAndLoop(阿里CTF) easy-100(LCTF) SafeBox(NJCTF) ...
- 【伪随机数】【搜索】【RE】【bugku】mountainclimbing WriteUp
Mountain Climbing WP 拿到题首先熟练地查个壳再用各种脱壳工具脱个壳. 脱壳之后熟练地双击感受一下出题者的恶意: 根据字面意思得知,是要根据一系列的操作来得到收益最大值,于是用ida ...
随机推荐
- JavaScript(第二十一天)【DOM元素尺寸和位置】
学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解. 一.获取元素CSS大小 ...
- django搭建web (一)
建立工程 django-admin.py startproject project_name 建立app python manage.py startapp app_name 将app添加进工程中 在 ...
- Basys3在线调试视频指南及代码
fpga在线调试视频链接 FPGA选择型号:xc7a35tcpg236-1 des文件 `timescale 1ns / 1ps module top( output [1:0] led, outpu ...
- ThreadLocal就是这么简单
前言 今天要研究的是ThreadLocal,这个我在一年前学习JavaWeb基础的时候接触过一次,当时在baidu搜出来的第一篇博文ThreadLocal,在评论下很多开发者认为那博主理解错误,给出了 ...
- 微信支付 chooseWXPay:fail
本来以为解决了微信支付get_brand_wcpay_request:faill这个问题后就万事大吉了,结果又迈入了另一个坑... 问题原因: 1.生成签名的时间戳参数名timestamp的s大小写问 ...
- var、let、const区别
1.let不存在变量提升,必须升明后才可用. 'use strict'; (function(){ console.log(varTest); console.log(letTest); var va ...
- 常用Mysql数据库操作语句
用户管理: 1.新建用户: 语法msyql>CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username - 你将创建 ...
- ArrayList源码学习----JDK1.7
什么是ArrayList? ArrayList是存储一组数据的集合,底层也是基于数组的方式实现,实际上也是对数组元素的增删改查:它的主要特点是: 有序:(基于数组实现) 随机访问速度快:(进行随机访问 ...
- angluarjs2入门学习资源
http://www.runoob.com/angularjs2/angularjs2-tutorial.htmlhttps://segmentfault.com/a/1190000008423981 ...
- Linux实战案例(5)关闭Centos的防火墙
1.检查防火墙的状态 [root@LxfN1 ~]# service iptables status表格:filterChain INPUT (policy ACCEPT)num target pro ...