C. Rest Stops
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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 L meters (1≤L≤106). Farmer John will hike the trail at a constant travel rate of rFrF seconds per meter (1≤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≤105); the ii-th stop is xixi meters from the start of the trail (0<xi<L) and has a tastiness value cici (1≤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 rB
seconds per meter (1≤rB≤106). Since Bessie is young and fit, rB is strictly less than rF.
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: L, N, rF, and rB. The next N lines describe the rest stops. For each ii between 1 and N, the i+1-st line contains two integers xi and ci, describing the position of the i-th rest stop and the tastiness of the grass there.
It is guaranteed that rF>rB, and 0<x1<…<xN<L.
Note that rF and rB 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 7 seconds at the x=7 rest stop (acquiring 14 tastiness units) and then stop for an additional 1 second at the x=8 rest stop (acquiring 1 more tastiness unit, for a total of 15 tastiness units).
分析
题意:FJ和他的母牛Bessie一起去爬山,Bessie速度比FJ快。Bessie可以在休息站停下来吃草,因为Bessie不能落后于FJ,所以FJ追上来Bessie就要继续前进。每个休息站的草都有对应的美味值,在休息站获得的美味度等于停留的时间乘于美味值,求怎样吃草才能才能使美味度总和最大;
注意一下速度单位是秒每米。
因为二者的距离关系关于时间是线性的,那么在过程中总吃草时间便是一定的,选取最大的值的休息站去吃即可;
唯一的问题就是FJ会追上来,Bessie可以选择在FJ到达之前就离开,可是这并不会为她提供更多的时间;
那只需要在后面区间上最大值的休息站悠闲地等FJ来再离开,前往该休息站后面区间的最大值休息站继续吃草就可以得到最大美味值。
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
typedef long long LL;
using namespace std;
struct stops{
int x,c;
} n[100005],goodstops[100005];
int L,N,rf,rb;
int main(){
scanf("%d%d%d%d",&L,&N,&rb,&rf);
int rdis=rb-rf;
for(int i=0;i<N;i++){
scanf("%d%d",&n[i].x,&n[i].c);
}
int maxc=0,pt=0;
for(int i=N-1;i>=0;i--){
if(n[i].c>maxc){
maxc=n[i].c;
goodstops[pt++]=n[i];
}
}
LL ans=0;goodstops[pt].x=0;
for(int i=pt-1;i>=0;i--){
ans+=(LL)(goodstops[i].x-goodstops[i+1].x)*rdis*goodstops[i].c;
}
printf("%I64d\n",ans);
return 0;
}