제출 #1189009

#제출 시각아이디문제언어결과실행 시간메모리
118900912345678Long Distance Coach (JOI17_coach)C++20
100 / 100
120 ms22172 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll nx=2e5+5, inf=4e18;

ll x, n, m, w, t, s[nx], dp[nx], mn[nx], qs[nx], querymode;
vector<pair<ll, ll>> d(nx);

struct line
{
    ll m, c;
    mutable ll p;
    line(ll m, ll c, ll p): m(m), c(c), p(p) {}
    bool operator< (const line &o) const {return querymode?p<o.p:m>o.m;}
};

struct convexhull:multiset<line>
{
    ll div(ll a, ll b)
    {
        if (((a%b)==0)||((a^b)>=0)) return a/b;
        return a/b-1;
    }
    bool isect(iterator x, iterator y)
    {
        if (y==end()) return x->p=inf, 0;
        if (x->m==y->m) x->p=(x->c<=y->c)?inf:-inf;
        else x->p=div(y->c-x->c, x->m-y->m);
        return x->p>=y->p;
    }
    void add(line nw)
    {
        auto x=insert(nw), y=next(x);
        while (isect(x, y)) y=erase(y);
        while ((y=x)!=begin()&&isect(--x, y)) isect(x, erase(y));
        while ((y=x)!=begin()&&isect(--x, y)) isect(x, erase(y));
    }
    ll query(ll x)
    {
        querymode=1;
        auto itr=lower_bound(line(0, 0, x));
        querymode=0;
        return itr->m*x+itr->c;
    }
} c;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>x>>n>>m>>w>>t;
    for (int i=1; i<=n; i++) cin>>s[i];
    s[++n]=x;
    sort(s+1, s+n+1);
    for (int i=1; i<=m; i++) cin>>d[i].first>>d[i].second, mn[i]=inf;
    sort(d.begin()+1, d.begin()+m+1);
    for (int i=1; i<=n; i++)
    {
        auto idx=lower_bound(d.begin()+1, d.begin()+m+1, make_pair(s[i]%t, 0ll))-d.begin()-1;
        mn[idx]=min(mn[idx], (ll)i);
    }
    dp[0]=w*(x/t+1);
    for (int i=1; i<=m; i++)
    {
        qs[i]=qs[i-1]+d[i].second;
        c.add(line(-(i-1), dp[i-1]-qs[i-1], 0));
        dp[i]=dp[i-1]+w*((x-d[i].first)/t+1);
        if (mn[i]!=inf)
        {
            ll amt=(s[mn[i]]-d[i].first)/t;
            dp[i]=min(dp[i], c.query(w*amt)+qs[i]+i*w*amt);
        }
    }
    cout<<dp[m];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...