洛谷P1561 [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

题解:
这题也是看题解才会的。。。

首先,当上山总时间大于下山总时间时,所有奶牛总上山时间恒定。最后总有一只奶牛在上山结束后在山顶,要想最优,只需让在山上的那只奶牛下山时间最小即可。
同理,当上山总时间小于下山总时间时,所有奶牛总下山时间恒定。开始总会有一只奶牛要上山,因此在下山之前,总有一只奶牛在山顶,要想最优,只需让在山上的那只奶牛上山时间最小即可。
比较一下上山和下山时间的大小,输出。

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
    int n,ss=0,xs=0;//ss==上山 xs==下山 
    int mins=1e9,minx=1e9;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        ss+=a,xs+=b;
        if(a<mins) mins=a;
        if(b<minx) minx=b;
    }
    if(ss>xs)
        printf("%d",ss+minx);
    else if(xs>ss)
        printf("%d\n",xs+mins);
    else
        printf("%d\n",min(ss+minx,xs+mins));
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值