답안 #171541

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
171541 2019-12-29T05:53:31 Z ho94949 Long Distance Coach (JOI17_coach) C++17
100 / 100
853 ms 39728 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 202020;
long long X, N, M, W, T;
long long S[MAXN];
long long D[MAXN], C[MAXN];

// used water before kickout
long long nw[MAXN];

//1~(i-1) customer minimum value when ith person do not kicked
// -(C_1+...+C_i)+ WV[i] customer's required value.
long long DP[MAXN];

// drank water of ith customer if none was kicked
long long WV[MAXN];

int pv[MAXN];

// min li-chao tree
struct Line
{
    long long a;
    long long b;
};
const long long INFV = (long long)1e18;
Line emptyl = {INFV, INFV};
struct Node
{
    long long s, e;
    Node* l;
    Node* r;
    Line v;
    Node(long long _s, long long _e)
    :s(_s), e(_e), l(nullptr), r(nullptr), v(emptyl)
    {

    }
    __int128 getv(long long x)
    {
        assert(v.a != INFV);
        __int128 ans = (__int128)v.a*x+v.b;
        long long m = (s+e)>>1;
        if(x<=m && l) ans = min(ans, l->getv(x));
        if(x>m && r) ans = min(ans, r->getv(x));
        return ans;
    }
    void setv(Line x)
    {
        if(v.a==INFV && v.b==INFV)
        {
            v = x;
            return;
        }
        if(s==e)
        {
            if((__int128)s*v.a+v.b > (__int128)s*x.a+x.b)
                swap(v, x);
            return;
        }
        long long m = (s+e)>>1;
        long long pt = 2*m+1;
        __int128 exist = (__int128)v.a*pt+2*v.b;
        __int128 nv = (__int128)x.a*pt+2*x.b;
        if(exist > nv)
            swap(v, x);
        
        if(v.a < x.a)
        {
            if(!l) l = new Node(s, m);
            l->setv(x);
        }
        else
        {
            if(!r) r = new Node(m+1, e);
            r->setv(x);
        }
    }
};


long long do_dp()
{
    Node* root = new Node(-INFV, INFV);
    DP[0] = 0; root->setv({-0, DP[0]});
    for(int i=1; i<=M+1; ++i)
    {
        DP[i] = DP[i-1]; // not kicking (i-1)th customer

        if(nw[i] != -1) // if can start kicking strategy,
        {
            DP[i] = min(DP[i], (long long)(root->getv(nw[i])+(i-1)*nw[i]));
            
            /*
            for(int j=i-2; j>=0; --j) //not kicking jth customer, kicking all
            
                DP[i] = min(DP[i], DP[j]+(i-j-1)*nw[i]);
            */    
        }
        if(pv[i] != 0) //kick through pv player and let what they want.
        {
            //cout << "!" << i << " " << pv[i] << endl;
            DP[i] = min(DP[i], DP[pv[i]] - WV[pv[i]] +C[pv[i]] + (i-pv[i])*nw[i]);
        }
        DP[i] += -C[i] + WV[i];
        root->setv({-i, DP[i]});
        //printf("%d %lld %lld\n", i, nw[i], DP[i]);
    }

    long long ans = DP[M+1]; //basic DP value;
    //we remove C_1...C_M
    for(int i=1; i<=M; ++i) ans += C[i];
    // WV[M+1] is defined as 0
    // we should add bus drivers' cost
    ans += W*(1+X/T);
    return ans;
}
int main()
{
    cin >> X >> N >> M >> W >> T;
    for(int i=0; i<N; ++i) cin >> S[i];
    vector<pair<long long, long long> > VDC;
    for(int i=0; i<M; ++i)
    {
        long long d, c;
        cin >> d >> c;
        VDC.emplace_back(d, c);
    }
    sort(VDC.begin(), VDC.end());
    for(int i=1; i<=M; ++i)
    {
        tie(D[i], C[i]) = VDC[i-1];
        WV[i] = W*(1+(X-D[i])/T);
        //printf("%d: %lld %lld\n", i, WV[i], C[i]);
    }
    S[N] = X;
    sort(S, S+N+1);

    memset(nw, -1, sizeof nw);
    set<int> SS;
    for(int i=0; i<=N; ++i)
    {
        long long kickout_water = W*(S[i]/T);
        int kick_index = lower_bound(D+1, D+1+M, S[i]%T) - D;
        //printf("%lld %d\n", kickout_water, kick_index);
        // we will decide whether kick 1~kick_index
        if(kick_index == 1) continue;
        if(nw[kick_index] != -1) continue;
        nw[kick_index] = kickout_water;

        SS.insert(kick_index);
        auto it = SS.find(kick_index);
        if(it != SS.begin())
            pv[kick_index] = *(--it);
    }

    printf("%lld\n", do_dp());
}




# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1784 KB Output is correct
4 Correct 4 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 4 ms 1912 KB Output is correct
9 Correct 4 ms 1912 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 4 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 3 ms 1912 KB Output is correct
18 Correct 3 ms 1912 KB Output is correct
19 Correct 3 ms 1912 KB Output is correct
20 Correct 3 ms 1912 KB Output is correct
21 Correct 3 ms 1912 KB Output is correct
22 Correct 4 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1784 KB Output is correct
4 Correct 4 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 4 ms 1912 KB Output is correct
9 Correct 4 ms 1912 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 4 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 3 ms 1912 KB Output is correct
18 Correct 3 ms 1912 KB Output is correct
19 Correct 3 ms 1912 KB Output is correct
20 Correct 3 ms 1912 KB Output is correct
21 Correct 3 ms 1912 KB Output is correct
22 Correct 4 ms 1912 KB Output is correct
23 Correct 3 ms 1912 KB Output is correct
24 Correct 3 ms 2040 KB Output is correct
25 Correct 4 ms 2040 KB Output is correct
26 Correct 3 ms 1912 KB Output is correct
27 Correct 3 ms 1912 KB Output is correct
28 Correct 4 ms 1912 KB Output is correct
29 Correct 4 ms 1912 KB Output is correct
30 Correct 4 ms 1912 KB Output is correct
31 Correct 3 ms 1912 KB Output is correct
32 Correct 4 ms 1912 KB Output is correct
33 Correct 4 ms 1912 KB Output is correct
34 Correct 4 ms 1912 KB Output is correct
35 Correct 4 ms 2088 KB Output is correct
36 Correct 4 ms 1912 KB Output is correct
37 Correct 4 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1784 KB Output is correct
4 Correct 4 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 4 ms 1912 KB Output is correct
9 Correct 4 ms 1912 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 4 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 3 ms 1912 KB Output is correct
18 Correct 3 ms 1912 KB Output is correct
19 Correct 3 ms 1912 KB Output is correct
20 Correct 3 ms 1912 KB Output is correct
21 Correct 3 ms 1912 KB Output is correct
22 Correct 4 ms 1912 KB Output is correct
23 Correct 3 ms 1912 KB Output is correct
24 Correct 3 ms 2040 KB Output is correct
25 Correct 4 ms 2040 KB Output is correct
26 Correct 3 ms 1912 KB Output is correct
27 Correct 3 ms 1912 KB Output is correct
28 Correct 4 ms 1912 KB Output is correct
29 Correct 4 ms 1912 KB Output is correct
30 Correct 4 ms 1912 KB Output is correct
31 Correct 3 ms 1912 KB Output is correct
32 Correct 4 ms 1912 KB Output is correct
33 Correct 4 ms 1912 KB Output is correct
34 Correct 4 ms 1912 KB Output is correct
35 Correct 4 ms 2088 KB Output is correct
36 Correct 4 ms 1912 KB Output is correct
37 Correct 4 ms 1912 KB Output is correct
38 Correct 9 ms 2168 KB Output is correct
39 Correct 9 ms 2168 KB Output is correct
40 Correct 10 ms 2268 KB Output is correct
41 Correct 8 ms 2168 KB Output is correct
42 Correct 8 ms 2140 KB Output is correct
43 Correct 8 ms 2172 KB Output is correct
44 Correct 9 ms 2168 KB Output is correct
45 Correct 8 ms 2208 KB Output is correct
46 Correct 8 ms 2296 KB Output is correct
47 Correct 9 ms 2296 KB Output is correct
48 Correct 9 ms 2168 KB Output is correct
49 Correct 9 ms 2168 KB Output is correct
50 Correct 8 ms 2168 KB Output is correct
51 Correct 9 ms 2168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1784 KB Output is correct
4 Correct 4 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 4 ms 1912 KB Output is correct
9 Correct 4 ms 1912 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 4 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 3 ms 1912 KB Output is correct
18 Correct 3 ms 1912 KB Output is correct
19 Correct 3 ms 1912 KB Output is correct
20 Correct 3 ms 1912 KB Output is correct
21 Correct 3 ms 1912 KB Output is correct
22 Correct 4 ms 1912 KB Output is correct
23 Correct 3 ms 1912 KB Output is correct
24 Correct 3 ms 2040 KB Output is correct
25 Correct 4 ms 2040 KB Output is correct
26 Correct 3 ms 1912 KB Output is correct
27 Correct 3 ms 1912 KB Output is correct
28 Correct 4 ms 1912 KB Output is correct
29 Correct 4 ms 1912 KB Output is correct
30 Correct 4 ms 1912 KB Output is correct
31 Correct 3 ms 1912 KB Output is correct
32 Correct 4 ms 1912 KB Output is correct
33 Correct 4 ms 1912 KB Output is correct
34 Correct 4 ms 1912 KB Output is correct
35 Correct 4 ms 2088 KB Output is correct
36 Correct 4 ms 1912 KB Output is correct
37 Correct 4 ms 1912 KB Output is correct
38 Correct 9 ms 2168 KB Output is correct
39 Correct 9 ms 2168 KB Output is correct
40 Correct 10 ms 2268 KB Output is correct
41 Correct 8 ms 2168 KB Output is correct
42 Correct 8 ms 2140 KB Output is correct
43 Correct 8 ms 2172 KB Output is correct
44 Correct 9 ms 2168 KB Output is correct
45 Correct 8 ms 2208 KB Output is correct
46 Correct 8 ms 2296 KB Output is correct
47 Correct 9 ms 2296 KB Output is correct
48 Correct 9 ms 2168 KB Output is correct
49 Correct 9 ms 2168 KB Output is correct
50 Correct 8 ms 2168 KB Output is correct
51 Correct 9 ms 2168 KB Output is correct
52 Correct 716 ms 24412 KB Output is correct
53 Correct 853 ms 29428 KB Output is correct
54 Correct 585 ms 23048 KB Output is correct
55 Correct 594 ms 23424 KB Output is correct
56 Correct 619 ms 23516 KB Output is correct
57 Correct 651 ms 23848 KB Output is correct
58 Correct 638 ms 23788 KB Output is correct
59 Correct 602 ms 23380 KB Output is correct
60 Correct 607 ms 23220 KB Output is correct
61 Correct 594 ms 23404 KB Output is correct
62 Correct 598 ms 23296 KB Output is correct
63 Correct 579 ms 39728 KB Output is correct
64 Correct 555 ms 19136 KB Output is correct
65 Correct 804 ms 29500 KB Output is correct
66 Correct 794 ms 29276 KB Output is correct
67 Correct 813 ms 29148 KB Output is correct
68 Correct 827 ms 29360 KB Output is correct
69 Correct 707 ms 24420 KB Output is correct
70 Correct 714 ms 24284 KB Output is correct
71 Correct 714 ms 24388 KB Output is correct
72 Correct 710 ms 24300 KB Output is correct
73 Correct 712 ms 24344 KB Output is correct
74 Correct 729 ms 24404 KB Output is correct
75 Correct 775 ms 24540 KB Output is correct
76 Correct 685 ms 24660 KB Output is correct
77 Correct 663 ms 24296 KB Output is correct
78 Correct 675 ms 24568 KB Output is correct
79 Correct 694 ms 23948 KB Output is correct
80 Correct 687 ms 23840 KB Output is correct