This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |