传送门
题目大意:
两个人教练和农夫爬山,教练必须得在农夫之前,而且教练的速度要比农夫的速度要快。然后上山途中有N个休息点,农夫不用休息,而教练可以选择休息。每个休息点有一个数值c,当教练每休息一秒钟,就会获得c个收益。问教练可以获得最大的收益。这里的单位要注意一下,是秒每米,即走一米所需要花费的时间。
题目分析:
这是一道贪心题,不是很难想,但是题意emmm,特别是对于速度的描述蜜汁尴尬,第一眼成 m/s,然后发现无解,幸好我的小学老师告诉我,如果此题无解,那么你一定是读错题了,再仔细看了n遍才发现是 s/m (逃)。
这里越贪越好,我们把c排下序,每次只在最大的那个休息,当农夫追上来时,去找该点后面c最大的点,我这里用了数组模拟队列来实现,每次取c最大的点,如果该点的位置小于当前的位置,则跳过该点,去找下一个点。
然后就模拟行走过程,每次+1m,可在点逗留时间+(农夫走1m需要的时间-教练走1m需要的时间)。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int maxn = 1e6+10;
typedef pair<ll,ll> P;
P t[maxn];
ll l,n,rf,rb;
bool cmp(P a,P b){
if(a.second==b.second) return a.first>b.first;
else return a.second>b.second;
}
int main(){
cin>>l>>n>>rf>>rb;
for(int i=0;i<n;i++){
cin>>t[i].first>>t[i].second;
}
sort(t,t+n,cmp);
ll time=0,s=0,cnt=0;
for(int i=0;i<l;i++){
while(t[s].first<i) s++;
if(t[s].first==i){
cnt+=(time*t[s].second);
time=0;
}
time+=(rf-rb);
}
cout<<cnt<<endl;
return 0;
}