Submission #49414

# Submission time Handle Problem Language Result Execution time Memory
49414 2018-05-28T14:30:03 Z SpaimaCarpatilor Long Distance Coach (JOI17_coach) C++17
100 / 100
252 ms 173984 KB
#include<bits/stdc++.h>

using namespace std;

int N, M, W, T, how[200009][2];
long long X, dp[200009][2], need[200009], partialC[200009];
const long long INF = 1LL << 60;
pair < int, long long > station[200009];
pair < long long, int > passenger[200009];

void read ()
{
    scanf ("%lld %d %d %d %d", &X, &N, &M, &W, &T);
    for (int i=1; i<=N; i++)
        scanf ("%lld", &station[i].second), station[i].first = station[i].second % T;
    if (X % T != 0)
        station[++N] = {X % T, X};
    sort (station + 1, station + N + 1);
    for (int i=1; i<=M; i++)
        scanf ("%lld %d", &passenger[i].first, &passenger[i].second);
    sort (passenger + 1, passenger + M + 1);
}

namespace DynamicCHT
{
    const int NMAX = 200000;
    pair < long long, long long > line[4 * NMAX];
    void build (int nod = 1, int l = 1, int r = NMAX)
    {
        line[nod] = {0, INF};
        if (r == l + 1) return ;
        int mid = (l + r) >> 1;
        build (nod << 1, l, mid);
        build (nod << 1 | 1, mid, r);
    }

    long long eval (pair < long long, long long > &f, int x)
    {
        return 1LL * f.first * x + f.second;
    }

    void addLine (int nod, int l, int r, pair < long long, long long > aux)
    {
        int m = (l + r) >> 1;
        bool lef = (eval (aux, l) < eval (line[nod], l));
        bool mid = (eval (aux, m) < eval (line[nod], m));
        if (mid)
            swap (aux, line[nod]);
        if (r == l + 1)
            return ;
        if (lef != mid)
            addLine (nod << 1, l, m, aux);
        else
            addLine (nod << 1 | 1, m, r, aux);
    }

    long long getMin (int nod, int l, int r, int pos)
    {
        int m = (l + r) >> 1;
        long long curr = eval (line[nod], pos);
        if (r == l + 1)
            return curr;
        if (pos < m)
            return min (getMin (nod << 1, l, m, pos), curr);
        return min (getMin (nod << 1 | 1, m, r, pos), curr);
    }

    void addLine (pair < long long, long long > aux)
    {
        addLine (1, 1, NMAX, aux);
    }

    long long getMin (int pos)
    {
        return getMin (1, 1, NMAX, pos);
    }
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

read ();
for (int i=1; i<=M; i++)
    need[i] = 1LL * W * ((X - passenger[i].first) / T + 1);
for (int i=0; i<=M; i++)
    dp[i][0] = dp[i][1] = INF;
dp[M + 1][1] = 1LL * W * (X / T + 1);
dp[M + 1][0] = INF;
///dp[i][j] = considered all passengers >= i, and j=0/1 depending on whether I've taken i or not
for (int i=1; i<=M; i++)
    partialC[i] = partialC[i - 1] + passenger[i].second;
passenger[M + 1].first = T + 1;
int pntr = N + 1; station[N + 1] = {T + 2, 0};
DynamicCHT::build ();
for (int i=M + 1; i>=1; i--)
{
    ///refresh dp[i][0]
    dp[i][0] = DynamicCHT::getMin (i) - partialC[i - 1];
    ///simple update
    if (min (dp[i][0], dp[i][1]) + need[i - 1] < dp[i - 1][1])
        dp[i - 1][1] = min (dp[i][0], dp[i][1]) + need[i - 1], how[i - 1][1] = i;
    ///now suppose some customers ending with i - 1 are to be aborted
    while (pntr > 0 && station[pntr].first > passenger[i].first)
        pntr --;
    ///the first one that doesn't get rid of i
    if (station[pntr].first < passenger[i - 1].first || i == 1 || pntr == 0 || pntr > N)
        continue;///first make sure you can abort i - 1, that ensures you can abort any contiguous segment [j, i - 1]
    long long fastestAbort = station[pntr].second;
    while (pntr - 1 > 0 && station[pntr - 1].first > passenger[i - 1].first)
        pntr --, fastestAbort = min (fastestAbort, station[pntr].second);
    ///computed fastest abort
    long long currDp = min (dp[i][0], dp[i][1]), currSlope = 1LL * W * (fastestAbort / T);
    DynamicCHT::addLine ({-currSlope, currDp + partialC[i - 1] + 1LL * currSlope * i});
/*    for (int j=i - 1; j>=1; j--)
    {
        long long curr = currDp + partialC[i - 1] - partialC[j - 1] + 1LL * currSlope * (i - j);
        ///=currDp + partialC[i - 1] + 1LL * currSlope * i + (j * (-currSlope))
        ///-partialC[j - 1]
        if (curr < dp[j][0])
            dp[j][0] = curr, how[j][0] = i;
    }*/
}
printf ("%lld\n", min (dp[0][0], dp[0][1]));
return 0;
}

Compilation message

coach.cpp: In function 'void read()':
coach.cpp:13:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%lld %d %d %d %d", &X, &N, &M, &W, &T);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:15:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%lld", &station[i].second), station[i].first = station[i].second % T;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:20:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%lld %d", &passenger[i].first, &passenger[i].second);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8568 KB Output is correct
2 Correct 9 ms 8676 KB Output is correct
3 Correct 9 ms 8676 KB Output is correct
4 Correct 9 ms 8676 KB Output is correct
5 Correct 10 ms 8744 KB Output is correct
6 Correct 9 ms 8744 KB Output is correct
7 Correct 10 ms 8744 KB Output is correct
8 Correct 10 ms 8744 KB Output is correct
9 Correct 10 ms 8804 KB Output is correct
10 Correct 9 ms 8804 KB Output is correct
11 Correct 8 ms 8804 KB Output is correct
12 Correct 8 ms 8804 KB Output is correct
13 Correct 8 ms 8804 KB Output is correct
14 Correct 8 ms 8880 KB Output is correct
15 Correct 9 ms 8880 KB Output is correct
16 Correct 8 ms 8880 KB Output is correct
17 Correct 8 ms 8880 KB Output is correct
18 Correct 8 ms 8880 KB Output is correct
19 Correct 9 ms 8880 KB Output is correct
20 Correct 9 ms 8880 KB Output is correct
21 Correct 13 ms 8880 KB Output is correct
22 Correct 11 ms 8880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8568 KB Output is correct
2 Correct 9 ms 8676 KB Output is correct
3 Correct 9 ms 8676 KB Output is correct
4 Correct 9 ms 8676 KB Output is correct
5 Correct 10 ms 8744 KB Output is correct
6 Correct 9 ms 8744 KB Output is correct
7 Correct 10 ms 8744 KB Output is correct
8 Correct 10 ms 8744 KB Output is correct
9 Correct 10 ms 8804 KB Output is correct
10 Correct 9 ms 8804 KB Output is correct
11 Correct 8 ms 8804 KB Output is correct
12 Correct 8 ms 8804 KB Output is correct
13 Correct 8 ms 8804 KB Output is correct
14 Correct 8 ms 8880 KB Output is correct
15 Correct 9 ms 8880 KB Output is correct
16 Correct 8 ms 8880 KB Output is correct
17 Correct 8 ms 8880 KB Output is correct
18 Correct 8 ms 8880 KB Output is correct
19 Correct 9 ms 8880 KB Output is correct
20 Correct 9 ms 8880 KB Output is correct
21 Correct 13 ms 8880 KB Output is correct
22 Correct 11 ms 8880 KB Output is correct
23 Correct 9 ms 8880 KB Output is correct
24 Correct 10 ms 8880 KB Output is correct
25 Correct 11 ms 8880 KB Output is correct
26 Correct 9 ms 8880 KB Output is correct
27 Correct 9 ms 8880 KB Output is correct
28 Correct 11 ms 8880 KB Output is correct
29 Correct 10 ms 8880 KB Output is correct
30 Correct 10 ms 8880 KB Output is correct
31 Correct 12 ms 8880 KB Output is correct
32 Correct 9 ms 8880 KB Output is correct
33 Correct 10 ms 8936 KB Output is correct
34 Correct 9 ms 8936 KB Output is correct
35 Correct 9 ms 8936 KB Output is correct
36 Correct 9 ms 8936 KB Output is correct
37 Correct 9 ms 8936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8568 KB Output is correct
2 Correct 9 ms 8676 KB Output is correct
3 Correct 9 ms 8676 KB Output is correct
4 Correct 9 ms 8676 KB Output is correct
5 Correct 10 ms 8744 KB Output is correct
6 Correct 9 ms 8744 KB Output is correct
7 Correct 10 ms 8744 KB Output is correct
8 Correct 10 ms 8744 KB Output is correct
9 Correct 10 ms 8804 KB Output is correct
10 Correct 9 ms 8804 KB Output is correct
11 Correct 8 ms 8804 KB Output is correct
12 Correct 8 ms 8804 KB Output is correct
13 Correct 8 ms 8804 KB Output is correct
14 Correct 8 ms 8880 KB Output is correct
15 Correct 9 ms 8880 KB Output is correct
16 Correct 8 ms 8880 KB Output is correct
17 Correct 8 ms 8880 KB Output is correct
18 Correct 8 ms 8880 KB Output is correct
19 Correct 9 ms 8880 KB Output is correct
20 Correct 9 ms 8880 KB Output is correct
21 Correct 13 ms 8880 KB Output is correct
22 Correct 11 ms 8880 KB Output is correct
23 Correct 9 ms 8880 KB Output is correct
24 Correct 10 ms 8880 KB Output is correct
25 Correct 11 ms 8880 KB Output is correct
26 Correct 9 ms 8880 KB Output is correct
27 Correct 9 ms 8880 KB Output is correct
28 Correct 11 ms 8880 KB Output is correct
29 Correct 10 ms 8880 KB Output is correct
30 Correct 10 ms 8880 KB Output is correct
31 Correct 12 ms 8880 KB Output is correct
32 Correct 9 ms 8880 KB Output is correct
33 Correct 10 ms 8936 KB Output is correct
34 Correct 9 ms 8936 KB Output is correct
35 Correct 9 ms 8936 KB Output is correct
36 Correct 9 ms 8936 KB Output is correct
37 Correct 9 ms 8936 KB Output is correct
38 Correct 11 ms 8956 KB Output is correct
39 Correct 10 ms 8956 KB Output is correct
40 Correct 10 ms 8956 KB Output is correct
41 Correct 14 ms 9084 KB Output is correct
42 Correct 14 ms 9084 KB Output is correct
43 Correct 14 ms 9084 KB Output is correct
44 Correct 12 ms 9084 KB Output is correct
45 Correct 15 ms 9084 KB Output is correct
46 Correct 12 ms 9084 KB Output is correct
47 Correct 14 ms 9084 KB Output is correct
48 Correct 11 ms 9084 KB Output is correct
49 Correct 9 ms 9084 KB Output is correct
50 Correct 10 ms 9084 KB Output is correct
51 Correct 10 ms 9084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8568 KB Output is correct
2 Correct 9 ms 8676 KB Output is correct
3 Correct 9 ms 8676 KB Output is correct
4 Correct 9 ms 8676 KB Output is correct
5 Correct 10 ms 8744 KB Output is correct
6 Correct 9 ms 8744 KB Output is correct
7 Correct 10 ms 8744 KB Output is correct
8 Correct 10 ms 8744 KB Output is correct
9 Correct 10 ms 8804 KB Output is correct
10 Correct 9 ms 8804 KB Output is correct
11 Correct 8 ms 8804 KB Output is correct
12 Correct 8 ms 8804 KB Output is correct
13 Correct 8 ms 8804 KB Output is correct
14 Correct 8 ms 8880 KB Output is correct
15 Correct 9 ms 8880 KB Output is correct
16 Correct 8 ms 8880 KB Output is correct
17 Correct 8 ms 8880 KB Output is correct
18 Correct 8 ms 8880 KB Output is correct
19 Correct 9 ms 8880 KB Output is correct
20 Correct 9 ms 8880 KB Output is correct
21 Correct 13 ms 8880 KB Output is correct
22 Correct 11 ms 8880 KB Output is correct
23 Correct 9 ms 8880 KB Output is correct
24 Correct 10 ms 8880 KB Output is correct
25 Correct 11 ms 8880 KB Output is correct
26 Correct 9 ms 8880 KB Output is correct
27 Correct 9 ms 8880 KB Output is correct
28 Correct 11 ms 8880 KB Output is correct
29 Correct 10 ms 8880 KB Output is correct
30 Correct 10 ms 8880 KB Output is correct
31 Correct 12 ms 8880 KB Output is correct
32 Correct 9 ms 8880 KB Output is correct
33 Correct 10 ms 8936 KB Output is correct
34 Correct 9 ms 8936 KB Output is correct
35 Correct 9 ms 8936 KB Output is correct
36 Correct 9 ms 8936 KB Output is correct
37 Correct 9 ms 8936 KB Output is correct
38 Correct 11 ms 8956 KB Output is correct
39 Correct 10 ms 8956 KB Output is correct
40 Correct 10 ms 8956 KB Output is correct
41 Correct 14 ms 9084 KB Output is correct
42 Correct 14 ms 9084 KB Output is correct
43 Correct 14 ms 9084 KB Output is correct
44 Correct 12 ms 9084 KB Output is correct
45 Correct 15 ms 9084 KB Output is correct
46 Correct 12 ms 9084 KB Output is correct
47 Correct 14 ms 9084 KB Output is correct
48 Correct 11 ms 9084 KB Output is correct
49 Correct 9 ms 9084 KB Output is correct
50 Correct 10 ms 9084 KB Output is correct
51 Correct 10 ms 9084 KB Output is correct
52 Correct 206 ms 22860 KB Output is correct
53 Correct 192 ms 28712 KB Output is correct
54 Correct 219 ms 33748 KB Output is correct
55 Correct 200 ms 38640 KB Output is correct
56 Correct 203 ms 44248 KB Output is correct
57 Correct 228 ms 49296 KB Output is correct
58 Correct 252 ms 55112 KB Output is correct
59 Correct 240 ms 60176 KB Output is correct
60 Correct 194 ms 65060 KB Output is correct
61 Correct 205 ms 70084 KB Output is correct
62 Correct 228 ms 75284 KB Output is correct
63 Correct 194 ms 79364 KB Output is correct
64 Correct 196 ms 85048 KB Output is correct
65 Correct 233 ms 91032 KB Output is correct
66 Correct 238 ms 96672 KB Output is correct
67 Correct 198 ms 102408 KB Output is correct
68 Correct 234 ms 108212 KB Output is correct
69 Correct 234 ms 113692 KB Output is correct
70 Correct 225 ms 119096 KB Output is correct
71 Correct 203 ms 124660 KB Output is correct
72 Correct 234 ms 130224 KB Output is correct
73 Correct 228 ms 135696 KB Output is correct
74 Correct 229 ms 141360 KB Output is correct
75 Correct 240 ms 146964 KB Output is correct
76 Correct 249 ms 152708 KB Output is correct
77 Correct 247 ms 158024 KB Output is correct
78 Correct 249 ms 163804 KB Output is correct
79 Correct 219 ms 168892 KB Output is correct
80 Correct 226 ms 173984 KB Output is correct