Submission #49412

#TimeUsernameProblemLanguageResultExecution timeMemory
49412SpaimaCarpatilorLong Distance Coach (JOI17_coach)C++17
71 / 100
2048 ms14780 KiB
#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);
}

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};
for (int i=M + 1; i>=1; i--)
{
    ///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);
    for (int j=i - 1; j>=1; j--)
    {
        long long curr = currDp + partialC[i - 1] - partialC[j - 1] + 1LL * currSlope * (i - j);
        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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...