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>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<pair<int,int>> P;
multiset<int> L[2], R[2];
int X[100000][2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N;
long long ans=0;
cin>>N;
P.resize(2*N);
for(auto &[x,y]: P) cin>>x>>y;
sort(P.begin(),P.end());
for(int i=0;i<N;i++) {
ans+=abs(i+1LL-P[2*i].first)+abs(i+1LL-P[2*i+1].first)+abs(1LL-P[2*i].second)+abs(2LL-P[2*i+1].second);
if(P[2*i].second>1) {
bool chk=false;
if(P[2*i].first<=i+1) {
auto it=L[0].lower_bound(P[2*i].first);
if(it!=L[0].end()) {
L[0].erase(it);
ans-=2;
chk=true;
}
}
if(!chk && P[2*i].first>=i+1) {
auto it=R[0].lower_bound(i+1);
if(it!=R[0].end()) {
R[0].erase(it);
ans-=2;
chk=true;
}
}
if(!chk) {
if(P[2*i].first<=i+1) L[1].insert(i+1);
else R[1].insert(P[2*i].first);
}
}
if(P[2*i+1].second<2) {
if(P[2*i+1].first<=i+1) {
auto it=L[1].lower_bound(P[2*i+1].first);
if(it!=L[1].end()) {
L[1].erase(it);
ans-=2;
continue;
}
}
if(P[2*i+1].first>=i+1) {
auto it=R[1].lower_bound(i+1);
if(it!=R[1].end()) {
R[1].erase(it);
ans-=2;
continue;
}
}
if(P[2*i+1].first<=i+1) L[0].insert(i+1);
else R[0].insert(P[2*i+1].first);
}
}
cout<<ans<<'\n';
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... |