#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo it[100001][2];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n;
cin>>n;
memset(it,0,sizeof(it));
vector<pair<llo,llo>> ac;
llo aa,bb;
llo ans=0;
llo freq[n][2];
for(llo i=0;i<n;i++){
freq[i][0]=0;
freq[i][1]=0;
}
for(llo i=0;i<2*n;i++){
cin>>aa>>bb;
if(bb<=1){
if(aa<1){
it[0][0]+=1;
ac.pb({0,0});
}
else if(aa>n){
it[n-1][0]+=1;
ac.pb({n-1,0});
}
else{
it[aa-1][0]+=1;
ac.pb({aa-1,0});
}
}
else{
if(aa<1){
it[0][1]+=1;
ac.pb({0,1});
}
else if(aa>n){
it[n-1][1]+=1;
ac.pb({n-1,1});
}
else{
it[aa-1][1]+=1;
ac.pb({aa-1,1});
}
}
freq[ac.back().a][ac.back().b]+=1;
ans+=abs(aa-(ac[i].a+1))+abs(bb-(ac[i].b+1));
}
queue<llo> cur[2];
llo ind[2];
ind[0]=0;
ind[1]=0;
/*for(auto j:ac){
cout<<j.a<<","<<j.b<<endl;
}*/
for(llo i=0;i<n;i++){
for(llo j=0;j<2;j++){
for(llo k=0;k<freq[i][j];k++){
cur[j].push(i);
}
}
while(cur[0].size() and ind[0]<=i){
ans+=abs(cur[0].front()-ind[0]);
ind[0]+=1;
cur[0].pop();
}
while(cur[1].size() and ind[1]<=i){
ans+=abs(cur[1].front()-ind[1]);
ind[1]+=1;
cur[1].pop();
}
while(cur[0].size() and ind[1]<=i){
ans+=abs(cur[0].front()-ind[1])+1;
ind[1]+=1;
cur[0].pop();
}
while(cur[1].size() and ind[0]<=i){
ans+=abs(cur[1].front()-ind[0])+1;
ind[0]+=1;
cur[1].pop();
}
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
5 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
5 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
5 ms |
1920 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
5 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
5 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
5 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
5 ms |
1920 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
5 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
34 |
Correct |
6 ms |
2048 KB |
Output is correct |
35 |
Correct |
6 ms |
2048 KB |
Output is correct |
36 |
Correct |
6 ms |
2048 KB |
Output is correct |
37 |
Correct |
6 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2048 KB |
Output is correct |
39 |
Correct |
6 ms |
2048 KB |
Output is correct |
40 |
Correct |
6 ms |
2048 KB |
Output is correct |
41 |
Correct |
6 ms |
2048 KB |
Output is correct |
42 |
Correct |
6 ms |
2048 KB |
Output is correct |
43 |
Correct |
7 ms |
2048 KB |
Output is correct |
44 |
Correct |
6 ms |
2048 KB |
Output is correct |
45 |
Correct |
6 ms |
2048 KB |
Output is correct |
46 |
Correct |
6 ms |
2048 KB |
Output is correct |
47 |
Correct |
6 ms |
2048 KB |
Output is correct |
48 |
Correct |
6 ms |
2048 KB |
Output is correct |
49 |
Correct |
6 ms |
2048 KB |
Output is correct |
50 |
Correct |
7 ms |
2048 KB |
Output is correct |
51 |
Correct |
6 ms |
2048 KB |
Output is correct |
52 |
Correct |
6 ms |
2048 KB |
Output is correct |
53 |
Correct |
6 ms |
2048 KB |
Output is correct |
54 |
Correct |
6 ms |
2048 KB |
Output is correct |
55 |
Correct |
6 ms |
2048 KB |
Output is correct |
56 |
Correct |
6 ms |
2048 KB |
Output is correct |
57 |
Correct |
6 ms |
2048 KB |
Output is correct |
58 |
Correct |
6 ms |
2048 KB |
Output is correct |
59 |
Correct |
6 ms |
2048 KB |
Output is correct |
60 |
Correct |
6 ms |
2048 KB |
Output is correct |
61 |
Correct |
6 ms |
2048 KB |
Output is correct |
62 |
Correct |
6 ms |
2048 KB |
Output is correct |
63 |
Correct |
6 ms |
2048 KB |
Output is correct |
64 |
Correct |
6 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
5 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
5 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
5 ms |
1920 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
5 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
34 |
Correct |
6 ms |
2048 KB |
Output is correct |
35 |
Correct |
6 ms |
2048 KB |
Output is correct |
36 |
Correct |
6 ms |
2048 KB |
Output is correct |
37 |
Correct |
6 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2048 KB |
Output is correct |
39 |
Correct |
6 ms |
2048 KB |
Output is correct |
40 |
Correct |
6 ms |
2048 KB |
Output is correct |
41 |
Correct |
6 ms |
2048 KB |
Output is correct |
42 |
Correct |
6 ms |
2048 KB |
Output is correct |
43 |
Correct |
7 ms |
2048 KB |
Output is correct |
44 |
Correct |
6 ms |
2048 KB |
Output is correct |
45 |
Correct |
6 ms |
2048 KB |
Output is correct |
46 |
Correct |
6 ms |
2048 KB |
Output is correct |
47 |
Correct |
6 ms |
2048 KB |
Output is correct |
48 |
Correct |
6 ms |
2048 KB |
Output is correct |
49 |
Correct |
6 ms |
2048 KB |
Output is correct |
50 |
Correct |
7 ms |
2048 KB |
Output is correct |
51 |
Correct |
6 ms |
2048 KB |
Output is correct |
52 |
Correct |
6 ms |
2048 KB |
Output is correct |
53 |
Correct |
6 ms |
2048 KB |
Output is correct |
54 |
Correct |
6 ms |
2048 KB |
Output is correct |
55 |
Correct |
6 ms |
2048 KB |
Output is correct |
56 |
Correct |
6 ms |
2048 KB |
Output is correct |
57 |
Correct |
6 ms |
2048 KB |
Output is correct |
58 |
Correct |
6 ms |
2048 KB |
Output is correct |
59 |
Correct |
6 ms |
2048 KB |
Output is correct |
60 |
Correct |
6 ms |
2048 KB |
Output is correct |
61 |
Correct |
6 ms |
2048 KB |
Output is correct |
62 |
Correct |
6 ms |
2048 KB |
Output is correct |
63 |
Correct |
6 ms |
2048 KB |
Output is correct |
64 |
Correct |
6 ms |
2048 KB |
Output is correct |
65 |
Correct |
57 ms |
9068 KB |
Output is correct |
66 |
Correct |
82 ms |
12908 KB |
Output is correct |
67 |
Correct |
83 ms |
9960 KB |
Output is correct |
68 |
Correct |
81 ms |
9960 KB |
Output is correct |
69 |
Correct |
82 ms |
9964 KB |
Output is correct |
70 |
Correct |
75 ms |
9960 KB |
Output is correct |
71 |
Correct |
79 ms |
9960 KB |
Output is correct |
72 |
Correct |
69 ms |
11488 KB |
Output is correct |
73 |
Correct |
72 ms |
11492 KB |
Output is correct |
74 |
Correct |
70 ms |
10724 KB |
Output is correct |
75 |
Correct |
73 ms |
10592 KB |
Output is correct |
76 |
Correct |
68 ms |
10468 KB |
Output is correct |
77 |
Correct |
71 ms |
10332 KB |
Output is correct |
78 |
Correct |
68 ms |
10464 KB |
Output is correct |
79 |
Correct |
71 ms |
10208 KB |
Output is correct |
80 |
Correct |
68 ms |
10136 KB |
Output is correct |
81 |
Correct |
68 ms |
10596 KB |
Output is correct |
82 |
Correct |
69 ms |
10080 KB |
Output is correct |
83 |
Correct |
67 ms |
9960 KB |
Output is correct |
84 |
Correct |
66 ms |
10084 KB |
Output is correct |
85 |
Correct |
67 ms |
9964 KB |
Output is correct |
86 |
Correct |
69 ms |
9964 KB |
Output is correct |
87 |
Correct |
68 ms |
9968 KB |
Output is correct |
88 |
Correct |
74 ms |
9960 KB |
Output is correct |
89 |
Correct |
75 ms |
9960 KB |
Output is correct |
90 |
Correct |
84 ms |
9960 KB |
Output is correct |
91 |
Correct |
79 ms |
9964 KB |
Output is correct |
92 |
Correct |
84 ms |
9960 KB |
Output is correct |
93 |
Correct |
82 ms |
9960 KB |
Output is correct |
94 |
Correct |
71 ms |
9960 KB |
Output is correct |
95 |
Correct |
77 ms |
9836 KB |
Output is correct |
96 |
Correct |
73 ms |
10976 KB |
Output is correct |
97 |
Correct |
73 ms |
10976 KB |
Output is correct |
98 |
Correct |
72 ms |
10728 KB |
Output is correct |
99 |
Correct |
71 ms |
10596 KB |
Output is correct |
100 |
Correct |
70 ms |
10592 KB |
Output is correct |
101 |
Correct |
68 ms |
10848 KB |
Output is correct |
102 |
Correct |
74 ms |
10600 KB |
Output is correct |
103 |
Correct |
75 ms |
10720 KB |
Output is correct |