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 ll = long long;
int const nmax = 1e5;
int v[5 + nmax][3], s[3], wait[3];
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::cin >> n;
ll result = 0;
for(int i = 1;i <= 2 * n; i++) {
int x, y;
std::cin >> x >> y;
if(x < 1) {
result += 1 - x;
x = 1;
}
if(x > n) {
result += x - n;
x = n;
}
if(y < 1) {
result += 1 - y;
y = 1;
}
if(y > 2) {
result += y - 2;
y = 2;
}
v[x][y]++;
}
int k1 = 1, k2 = 0;
for(int i = 1;i <= n; i++)
for(int j = 0;j < v[i][1] + v[i][2]; j++) {
if(k1 < i)
result += i - k1;
else if(k1 > i)
result += k1 - i;
if(k2 > 0)
k1++;
k2 ^= 1;
}
for(int i = 1;i <= n; i++) {
wait[1]++;
wait[2]++;
s[1] += v[i][1];
s[2] += v[i][2];
while(wait[1] > 0 && s[1] > 0) {
wait[1]--;
s[1]--;
}
while(wait[2] > 0 && s[2] > 0) {
wait[2]--;
s[2]--;
}
while(wait[1] > 0 && s[2] > 0) {
result++;
wait[1]--;
s[2]--;
}
while(wait[2] > 0 && s[1] > 0) {
result++;
wait[2]--;
s[1]--;
}
}
std::cout << result;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |