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 <bits/stdc++.h>
#include <set>
long long delivery(int numTeams, int capacity, int numSections, int p[])
{
std::sort(p, p + numTeams);
auto DistTo0 = [&] (int x)
{
return std::min(x, numSections - x);
};
//auto mid = std::lower_bound(p, p + numTeams, numSections / 2);
int num0 = 0;
int numL = 0;
int numR = 0;
for (int i = 0; i < numTeams; i++)
{
if (p[i] == 0)
num0++;
else if (p[i] * 2 < numSections)
numL++;
else
numR++;
}
if (capacity == numTeams)
{
if (numL == 0)
return DistTo0(p[num0]) * 2;
if (numR == 0)
return DistTo0(p[numTeams - 1]) * 2;
return numSections;
}
int doneL = 0;
int doneR = 0;
long long sec = 0;
while ((doneL + doneR) < numTeams)
{
int boxes = capacity;
auto Give = [&](int x, int numToGive)
{
if (x < numSections)
numL -= numToGive;
else
numR -= numToGive;
boxes -= numToGive;
};
if (numL > numR)
{
sec += DistTo0(p[doneL]);
while (true)
{
int oldPos = p[doneL];
int numToGive = 0;
while (numToGive < boxes && p[doneL + numToGive] == oldPos)
numToGive++;
Give(oldPos, numToGive);
doneL += numToGive;
if (boxes <= 0 || (doneL + doneR) >= numTeams)
{
sec += DistTo0(oldPos);
break;
}
sec += std::abs(p[doneL] - oldPos);
}
}
else
{
sec += DistTo0(p[numTeams - doneR - 1]);
while (true)
{
int oldPos = p[numTeams - doneR - 1];
int numToGive = 0;
while (numToGive < boxes && p[numTeams - doneR - numToGive - 1] == oldPos)
numToGive++;
Give(oldPos, numToGive);
doneR += numToGive;
if (boxes <= 0 || (doneL + doneR) >= numTeams)
{
sec += DistTo0(oldPos);
break;
}
sec += std::abs(p[numTeams - doneR - 1] - oldPos);
}
}
}
return sec;
}
# | 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... |