[USACO13OPEN]燃油经济性Fuel Economy

[USACO13OPEN]燃油经济性Fuel Economy

贪心

题解:

先特判-1和0.

在一个加油站x时,它往后G的范围内,如果:
1. 有比它费用小的加油站y,就在x加恰好能走到y的油,直接跳到y。
2. 没有。此时在x加满油,跳到范围内费用最小的加油站。

细节见代码。

Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll N = 100005;
const ll INF = 1e17;

ll n,G,B,D,ans;

struct Station{
    ll p,c;
    bool operator < (const Station &other) const { return p < other.p; }
} o[N];

ll nxt(ll s,ll lim){
    for(ll i=s+1,to=N-1;i<=n;i++){
        if(o[i].p-o[s].p > lim) return to;
        if(o[i].c < o[s].c) return i;
        if(o[i].c < o[to].c) to=i;
    }
}

int main(){
    freopen("a.in","r",stdin);
    scanf("%lld%lld%lld%lld",&n,&G,&B,&D);
    for(ll i=1;i<=n;i++) scanf("%lld%lld",&o[i].p,&o[i].c);
    sort(o+1,o+1+n);
    for(ll i=2;i<=n;i++) if(o[i].p-o[i-1].p>G){ puts("-1"); return 0; }
    if(o[1].p>B || D-o[n].p>G){ puts("-1"); return 0; }
    if(D<B){ puts("0"); return 0; }
    if(o[n].p==D){ o[n].c=0; }
    else{ n++; o[n].p=D; o[n].c=0; }
    o[N-1].c=INF;
    ll now=0, fuel=B, to=nxt(now,fuel);
    now=to; fuel-=o[to].p;
    for(ll i=1;i<n;i++){
        to=nxt(now,G);
        if(o[to].c > o[now].c){
            ans+=(G-fuel)*o[now].c;
            fuel=G-(o[to].p-o[now].p); 
        }
        else{
            ans+=(o[to].p-o[now].p-fuel)*o[now].c;
            fuel=0;
        }
        now=to;
    }
    printf("%lld\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值