luogu4266 [USACO18FEB]Rest Stops

本文介绍了一个徒步问题,其中Farmer John和他的训练员Bessie在一条长度为L米的直线上徒步,Farmer John以恒定速度前进,而Bessie可以在特定的N个休息点停留。文章详细描述了如何通过算法找到最优的休息策略。

http://www.elijahqi.win/2018/03/06/luogu4266-usaco18febrest-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

L
L meters (

1 \leq L \leq 10^6
1≤L≤106 ). Farmer John will hike the trail at a constant travel rate of

r_F
rF​ seconds per meter (

1 \leq r_F \leq 10^6
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
N rest stops along the trail (

1 \leq N \leq 10^5
1≤N≤105 ); the

i
i -th stop is

x_i
xi​ meters from the start of the trail (

0 < x_i < L
0

#include<cstdio>
#include<algorithm>
#define ll long long
#define N 1100000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
ll l,n,rf,rb;int top;
struct node{int p;ll c;}da[N],q[N];
inline bool cmp(const node &a,const node &b){return a.p<b.p;}
int main(){
//  freopen("reststops.in","r",stdin);
//  freopen("reststops.out","w",stdout);
    l=read();n=read();rf=read();rb=read();
    for (int i=1;i<=n;++i) da[i].p=read(),da[i].c=read();
    sort(da+1,da+n+1,cmp);ll ans=0;int lm=0;
    for (int i=n;i;--i){
        if (da[i].c>lm) q[++top]=da[i],lm=da[i].c;
    }
    for (int i=top;i;--i) ans+=(rf-rb)*(q[i].p-q[i+1].p)*q[i].c;
    printf("%lld",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值