zoukankan      html  css  js  c++  java
  • [USACO12JAN]爬山Mountain Climbing

    题目描述

    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:
    3
    6 4
    8 1
    2 3
    输出样例#1:
    17
    随机跳题。。。

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

    
    

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

    
    

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

    
    

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

    
    

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

    
    

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

    
    
     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=1;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 }
  • 相关阅读:
    Excel 之查找与替换
    重拾Excel之为什么
    taobao
    祝我生日快乐
    啊哈哈哈哈!自由啦
    我是不是得了抑郁症?
    Be quiet
    tcpdump tutorial
    Java Thread 多线程同步、锁、通信
    java 堆、栈、常量池等
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7486948.html
Copyright © 2011-2022 走看看