Description
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≤10e6). Farmer John will hike the trail at a constant travel rate of rF seconds per meter (1≤rF≤10e6). 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 N rest stops along the trail (1≤N≤10e5); the ii-th stop is xi meters from the start of the trail (0<xi<L) and has a tastiness value ci(1≤ci≤10e6). If Bessie rests at stop i for t seconds, she receives ci⋅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≤10e6). 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 ii-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).
解析
Bessie比较快,他需要在休息站吃东西,每个休息站每秒能吃的也不一样。
Farmer John比较慢,但是他不会在休息站停留
现在要求B先生吃的最大值。要保证B停留不会被F超过
因为吃东西是ci⋅t,所以把时间都留在ci最大的地方这样能吃最多!
所以将休息站排序,尽量吃大的。
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct stop
{
int first;//地点
int second;//ci
}a[1000005];
bool cmp(stop &a,stop &b)
{
return a.second>b.second;
}
int main()
{
int L,N,rf,rb;
scanf("%d%d%d%d",&L,&N,&rf,&rb);
int i,last=0;
long long ans=0;
for(i=0;i<N;i++)
scanf("%d %d",&a[i].first,&a[i].second);
sort(a,a+N,cmp); //休息站排序
for(i=0;i<N;i++)
{
if(a[i].first>last)//保证休息站在路上
{
ans+=(long long)(a[i].first-last)*(rf-rb)*a[i].second;
last=a[i].first;
}
}
printf("%I64d\n",ans);
return 0;
}