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;
queue<pair<int,int>> L[2], R[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(auto[x,y]: P) {
if(x<1) L[y>1].emplace(x,y);
else R[y>1].emplace(x,y);
}
for(int i=1;i<=N;i++) {
bool c1=false, c2=false;
while(R[0].size() && R[0].front().first==i) {
L[0].push(R[0].front());
R[0].pop();
}
while(R[1].size() && R[1].front().first==i) {
L[1].push(R[1].front());
R[1].pop();
}
if(!c1 && L[0].size()) {
auto[x,y]=L[0].front();
L[0].pop();
ans+=abs(x-i)+abs(y-1);
c1=true;
}
if(!c2 && L[1].size()) {
auto[x,y]=L[1].front();
L[1].pop();
ans+=abs(x-i)+abs(y-2);
c2=true;
}
if(!c1 && L[1].size()) {
auto[x,y]=L[1].front();
L[1].pop();
ans+=abs(x-i)+abs(y-1);
c1=true;
}
if(!c2 && L[0].size()) {
auto[x,y]=L[0].front();
L[0].pop();
ans+=abs(x-i)+abs(y-2);
c2=true;
}
if(!c1 && R[0].size()) {
auto[x,y]=R[0].front();
R[0].pop();
ans+=abs(x-i)+abs(y-1);
c1=true;
}
if(!c2 && R[1].size()) {
auto[x,y]=R[1].front();
R[1].pop();
ans+=abs(x-i)+abs(y-2);
c2=true;
}
if(!c1 && R[1].size()) {
auto[x,y]=R[1].front();
R[1].pop();
ans+=abs(x-i)+abs(y-1);
c1=true;
}
if(!c2 && R[0].size()) {
auto[x,y]=R[0].front();
R[0].pop();
ans+=abs(x-i)+abs(y-2);
c2=true;
}
}
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... |