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;
const int N = 1e5 + 5;
int n;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin >> n;
int a[3][n + 1];
memset(a, 0, sizeof(a));
long long ans = 0;
for(int i = 1; i <= 2 * n; i++){
int x, y;
cin >> x >> y;
if(y < 1){
ans += 1 - y;
y = 1;
}
else if(y > 2){
ans += y - 2;
y = 2;
}
if(x < 1){
ans += 1 - x;
x = 1;
}
else if(x > n){
ans += x - n;
x = n;
}
a[y][x] += 1;
}
vector < int > q[3], s[3];
for(int i = 1; i <= n; i++){
q[1].push_back(i);
q[2].push_back(i);
for(int j = 1; j <= 2; j++){
for(int k = 1; k <= a[j][i]; k++){
s[j].push_back(i);
}
}
for(int j = 1; j <= 2; j++){
while(!q[j].empty() && !s[j].empty()){
ans += abs(q[j].back() - s[j].back());
q[j].pop_back();
s[j].pop_back();
}
}
for(int j = 1; j <= 2; j++){
while(!q[j].empty() && !s[3 - j].empty()){
ans += abs(q[j].back() - s[3 - j].back()) + 1;
q[j].pop_back();
s[3 - j].pop_back();
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |