# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
63907 | Kubalionzzale | Boxes with souvenirs (IOI15_boxes) | C++14 | 2 ms | 420 KiB |
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 "boxes.h"
#include <map>
#include <iostream>
#include <algorithm>
long long delivery(int n, int k, int l, int p[]) {
if (n == 1)
{
return std::min(p[0] * 2LL, (l - p[0]) * 2LL);
}
int here = n;
for (int i = 0; i < n; ++i)
{
if (p[i] > l - p[i] && here == n)
{
here = i;
}
if (i >= here)
{
p[i] = l - p[i];
}
}
if (here == 0)
{
int cur = k - 1;
long long int sum = p[n - 1];
for (int i = n - 2; i >= 0; --i)
{
if (cur == 0)
{
sum += p[i + 1] * 2LL + p[i] - p[i + 1];
cur = k - 1;
}
else
{
sum += p[i] - p[i + 1] * 1LL;
--cur;
}
}
return sum + p[0] * 1LL;
}
else if (here == n)
{
int cur = k - 1;
long long int sum = p[0];
for (int i = 1; i < n; ++i)
{
if (cur == 0)
{
sum += p[i - 1] * 2LL + p[i] - p[i - 1];
cur = k - 1;
}
else
{
sum += p[i] - p[i - 1] * 1LL;
--cur;
}
}
return sum + p[n - 1] * 1LL;
}
long long int sumleft = p[here - 1], leftleft = -1;
int cur = k - 1;
for (int i = here - 2; i >= 0; --i)
{
if (cur == 0)
{
sumleft += p[i + 1] + p[i] * 1LL;
cur = k - 1;
}
else
{
sumleft += p[i + 1] - p[i] * 1LL;
--cur;
}
}
sumleft += p[0];
long long int sumright = p[here], leftright = -1;
cur = k - 1;
for (int i = here + 1; i < n; ++i)
{
if (cur == 0)
{
sumright += p[i - 1] + p[i] * 1LL;
cur = k - 1;
}
else
{
sumright += p[i - 1] - p[i] * 1LL;
--cur;
}
}
sumright += p[n - 1];
leftright = cur;
long long int curmin = sumleft + sumright;
if (k == 1)
return curmin;
long long int curval, max = 0;
int where = std::max(here - k + 1, 0);
for (int i = std::max(here - k + 1, 0);; ++i)
{
if (i >= here)
break;
if (i + k - 1 >= n)
break;
curval = 0;
if (i - 1 > 0)
{
curval += p[i] - p[i - 1] * 1LL;
}
else
{
curval += p[i] * 1LL;
}
if (i + k < n)
{
if (i + k == here)
{
curval += (l - p[i + k]) * 1LL - p[i + k - 1];
}
else
{
curval += p[i + k - 1] - p[i + k] * 1LL;
}
}
else
{
curval += p[i + k - 1] * 1LL;
}
if (curval > max)
{
max = curval;
where = i;
}
}
long long int cursum = l;
if (where != 0)
{
cur = k - 1;
cursum += p[where - 1];
for (int i = where - 2; i >= 0; --i)
{
if (cur == 0)
{
cursum += p[i + 1] + p[i] * 1LL;
cur = k - 1;
}
else
{
cursum += p[i + 1] - p[i] * 1LL;
--cur;
}
}
cursum += p[0];
}
if (where + k - 1 != n - 1)
{
cur = k - 1;
cursum += p[where + k];
for (int i = where + k + 1; i < n; ++i)
{
if (cur == 0)
{
cursum += p[i - 1] + p[i] * 1LL;
cur = k - 1;
}
else
{
cursum += p[i - 1] - p[i] * 1LL;
--cur;
}
}
cursum += p[n - 1];
}
return std::min(curmin, cursum);
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |