zoukankan      html  css  js  c++  java
  • [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

    一行一个整数,表示所有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$是可行的。

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

     1 //It is made by Awson on 2017.10.18
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <string>
    10 #include <cstdio>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Min(a, b) ((a) < (b) ? (a) : (b))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
    19 using namespace std;
    20 const int INF = ~0u>>1;
    21 
    22 int a, b, c = INF, d = INF, n, u, v;
    23 
    24 void work() {
    25     scanf("%d", &n);
    26     while (n--) {
    27         scanf("%d%d", &u, &v);
    28         a += u, b += v;
    29         c = Min(c, u), d = Min(d, v);
    30     }
    31     printf("%d
    ", Max(a+d, b+c));
    32 }
    33 int main() {
    34     work();
    35     return 0;
    36 }
  • 相关阅读:
    Linux常用命令_(系统设置)
    Linux常用命令_(系统管理)
    Linux常用命令_(基本命令)
    敏捷测试的流程
    Web测试Selenium:如何选取元素
    Selenium学习
    Selenium介绍
    Selenium测试规划
    HTTPS传输协议原理
    常见的加密算法
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7688752.html
Copyright © 2011-2022 走看看