#include "shortcut.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
typedef long long llong;
const int MAXN = 1000000 + 10;
const llong INF = 1e16;
struct Point
{
llong x, y;
};
struct Rectangle
{
Point low, up;
Rectangle()
{
low.x = low.y = -INF;
up.x = up.y = INF;
}
bool isInside(Point p)
{
return low.x <= p.x && low.y <= p.y && p.x <= up.x && p.y <= up.y;
}
};
int n, c;
int l[MAXN];
int d[MAXN];
int sortedPlus[MAXN];
int sortedMinus[MAXN];
llong p[MAXN];
bool check(llong allowed)
{
Rectangle area;
llong maxSum = -INF;
llong maxSub = -INF;
int idxSum = 0;
int idxSub = 0;
int ptr = n + 1;
for (int i = 1 ; i <= n ; ++i)
{
while (ptr > 1 && p[sortedPlus[i]] + d[sortedPlus[i]] - p[sortedMinus[ptr - 1]] + d[sortedMinus[ptr - 1]] > allowed)
{
ptr--;
if (maxSum < p[sortedMinus[ptr]] + d[sortedMinus[ptr]])
{
maxSum = p[sortedMinus[ptr]] + d[sortedMinus[ptr]];
idxSum = sortedMinus[ptr];
}
if (maxSub < d[sortedMinus[ptr]] - p[sortedMinus[ptr]])
{
maxSub = d[sortedMinus[ptr]] - p[sortedMinus[ptr]];
idxSub = sortedMinus[ptr];
}
}
if (ptr <= n)
{
if (idxSum < sortedPlus[i]) area.low.x = std::max(area.low.x, p[sortedPlus[i]] + d[sortedPlus[i]] + maxSum + c - allowed);
if (idxSub < sortedPlus[i]) area.low.y = std::max(area.low.y, p[sortedPlus[i]] + d[sortedPlus[i]] + maxSub + c - allowed);
if (idxSub < sortedPlus[i]) area.up.x = std::min(area.up.x, p[sortedPlus[i]] - d[sortedPlus[i]] - maxSub - c + allowed);
if (idxSum < sortedPlus[i]) area.up.y = std::min(area.up.y, p[sortedPlus[i]] - d[sortedPlus[i]] - maxSum - c + allowed);
}
}
ptr = 1;
for (int i = 2 ; i <= n ; ++i)
{
while (ptr < i && (p[ptr] + p[i] < area.low.x || p[i] - p[ptr] > area.up.y) && (p[i] + p[ptr + 1] <= area.up.x && p[i] - p[ptr + 1] >= area.low.y))
{
ptr++;
}
if (ptr < i)
{
Point curr = {p[i] + p[ptr], p[i] - p[ptr]};
if (area.isInside(curr))
{
return true;
}
}
}
return false;
}
llong find_shortcut(int N, std::vector <int> L, std::vector <int> D, int C)
{
n = N; c = C;
for (int i = 0 ; i < n - 1 ; ++i)
{
l[i + 1] = L[i];
}
for (int i = 0 ; i < n ; ++i)
{
d[i + 1] = D[i];
}
p[1] = 0;
for (int i = 2 ; i <= n ; ++i)
{
p[i] = p[i - 1] + l[i - 1];
}
std::iota(sortedPlus + 1, sortedPlus + 1 + n, 1);
std::iota(sortedMinus + 1, sortedMinus + 1 + n, 1);
std::sort(sortedPlus + 1, sortedPlus + 1 + n, [&](int x, int y)
{
return d[x] + p[x] < d[y] + p[y];
});
std::sort(sortedMinus + 1, sortedMinus + 1 + n, [&](int x, int y)
{
return d[x] - p[x] < d[y] - p[y];
});
llong l = -1, r = INF, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (!check(mid)) l = mid;
else r = mid;
}
return r;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
0 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
312 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |