题面
outputstandard 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 rF 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 N rest stops along the trail (1≤N≤105); the i-th stop is xi meters from the start of the trail (0<xi<L) and has a tastiness value ci (1≤ci≤106). 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≤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 i 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.
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).
题意
两个人走路长L,路上有N个休息站,给出两人的速度,rF一定大于rB,注意这里的单位是秒每米,即一米走多少秒,那么肯定越少速度越快了。
速度快的那位可以在休息站的时候停下来吃草,给出每个休息站 的位置和草的价值,在休息站每停1s就可以获得该价值的草。但你要保证不能掉到速度慢那位的后面。
分析
贪心就完事,排序价值,在最大价值前面的肯定不吃,攒时间到最大价值那里再吃,然后再看次大价值是否在最大价值之后,是就重复刚才的操作,否则再往下看,直到最后一个。
代码
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
const int maxn=1e5+5;
struct node{
int pos,c;
}stop[maxn];
bool cmp(node a,node b){
if(a.c!=b.c) return a.c>b.c;
return a.pos>b.pos;
}
int main(){
int l,n,rf,rb,i;
scanf("%d%d%d%d",&l,&n,&rf,&rb);
for(i=0;i<n;i++) scanf("%d %d",&stop[i].pos,&stop[i].c);
sort(stop,stop+n,cmp);
int now=0;
ll ans=0;
for(i=0;i<n;i++){
if(stop[i].pos>now){
ans+=(ll)(stop[i].pos-now)*(rf-rb)*stop[i].c;
now=stop[i].pos;
}
}
printf("%lld\n",ans);
return 0;
}