R2_C_Rest Stops

在一场与Farmer John的登山比赛中,Bessie通过精心规划休息点以最大化品尝美味草地的机会,同时确保不会落后于Farmer John。此策略涉及贪心算法和单调栈的应用,以选择收益最高的休息点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

Rest Stops

Farmer John and his personal trainer Bessie are hiking up Mount Vancowver. For their purposes (and yours), the mountain can be represented as a long straight trail of length LL meters (1≤L≤1061≤L≤106). Farmer John will hike the trail at a constant travel rate of rFrFseconds per meter (1≤rF≤1061≤rF≤106). Since he is working on his stamina, he will not take any rest stops along the way.

Bessie, however, is allowed to take rest stops, where she might find some tasty grass. Of course, she cannot stop just anywhere! There are NN rest stops along the trail (1≤N≤1051≤N≤105); the ii-th stop is xixi meters from the start of the trail (0<xi<L0<xi<L) and has a tastiness value cici(1≤ci≤1061≤ci≤106). If Bessie rests at stop ii for tt seconds, she receives ci⋅tci⋅t tastiness units.

When not at a rest stop, Bessie will be hiking at a fixed travel rate of rBrB seconds per meter (1≤rB≤1061≤rB≤106). Since Bessie is young and fit, rBrB is strictly less than rFrF.

Bessie would like to maximize her consumption of tasty grass. But she is worried about Farmer John; she thinks that if at any point along the hike she is behind Farmer John on the trail, he might lose all motivation to continue!

Help Bessie find the maximum total tastiness units she can obtain while making sure that Farmer John completes the hike.

Input

The first line of input contains four integers: LL, NN, rFrF, and rBrB. The next NN lines describe the rest stops. For each ii between 11 and NN, the i+1i+1-st line contains two integers xixi and cici, describing the position of the ii-th rest stop and the tastiness of the grass there.

It is guaranteed that rF>rBrF>rB, and 0<x1<…<xN<L0<x1<…<xN<L. Note that rFrF and rBrB are given in seconds per meter!

Output

A single integer: the maximum total tastiness units Bessie can obtain.

Example

input

10 2 4 3
7 2
8 1

output

15

Note

In this example, it is optimal for Bessie to stop for 77 seconds at the x=7x=7 rest stop (acquiring 1414 tastiness units) and then stop for an additional 11 second at the x=8x=8 rest stop (acquiring 11 more tastiness unit, for a total of 1515 tastiness units).

time limit per testmemory limit per testinputoutput
1 second256 megabytesstandard inputstandard output

题目大意

两个人J和B爬山,B必须得在J之前,而且B的速度要比J的速度要快。然后上山途中有N个休息点,J不用休息,而B可以选择休息。每个休息点有一个c,当B每休息一秒钟,就会获得c个收益。问B选择在那些点休息可以获得最大的收益。这里的单位要注意一下,是秒每米,即走一米所需要花费的时间。

题目分析

其实很容易看出来应该是贪心。因为时间差是一定的。具体的来说,就是如果有X,Y两个休息点,B先在X休息等J,然后再出发,到达Y后再休息等J,休息的时间与B直接到达Y不在X休息的休息的时间是一定的。既然如此,我们当然要选择一个收益比较大的休息点休息。所以每得到一个点,我们就把它加进一个数组里面,若是上一个点比现行的点的收益要低,就把它弹出去。这其实是一个单调栈的思想。维护一个单调递减的单调栈。数据类型用long long,防止溢出。

一开始是贪极值……现在才明白应该是贪最值的……看来我不够贪。

代码

#include <cstdio>
using namespace std;
typedef long long ll;
typedef struct{
  int x, c;
}node;
const int maxn = 1e5 + 7;
node sta[maxn];
ll l, n, f, b;
ll calc(node p, node q){
  ll dis = q.x - p.x;
  ll time = (f - b) * dis;
  return time * q.c;
}
int main(int argc, char const *argv[]) {
  scanf("%lld%lld%lld%lld", &l, &n, &f, &b);
  int top = 1;
  for(int i = 0; i < n; i++){
    ll x, c;
    scanf("%lld%lld", &x, &c);
    while(top >= 1 && sta[top].c <= c) top--;
    sta[++top].x = x; sta[top].c = c;
  }
  sta[0].x = 0; sta[0].c = 0;
  ll ans = 0;
  for(int i = 0; i < top; i++){
    ans += calc(sta[i], sta[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值