#include <bits/stdc++.h>
#define DIM 200010
using namespace std;
pair <long long, long long> v[DIM];
vector <int> coins[3],libere[3];
long long a[DIM][3], sol;
int n,i,j;
inline long long modul (long long n){
if (n < 0)
return -n;
return n;
}
long long dist (long long x, long long y, long long x2, long long y2){
return modul (x - x2) + modul (y - y2);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<=2*n;i++){
cin>>v[i].first>>v[i].second;
if (v[i].first < 1 && v[i].second > 2){
a[1][2]++;
sol += dist (v[i].first,v[i].second,1,2);
continue;
}
if (v[i].first >= 1 && v[i].first <= n && v[i].second > 2){
a[v[i].first][2]++;
sol += dist (v[i].first,v[i].second,v[i].first,2);
continue;
}
if (v[i].first > n && v[i].second > 2){
a[n][2]++;
sol += dist (v[i].first,v[i].second,n,2);
continue;
}
if (v[i].first > n && v[i].second >= 1 && v[i].second <= 2){
a[n][v[i].second]++;
sol += dist (v[i].first,v[i].second,n,v[i].second);
continue;
}
if (v[i].first > n && v[i].second < 1){
a[n][1]++;
sol += dist (v[i].first,v[i].second,n,1);
continue;
}
if (v[i].first >= 1 && v[i].second <= n && v[i].second < 1){
a[v[i].first][1]++;
sol += dist (v[i].first,v[i].second,v[i].first,1);
continue;
}
if (v[i].first < 1 && v[i].second < 1){
a[1][1]++;
sol += dist (v[i].first, v[i].second,1,1);
continue;
}
if (v[i].first < 1 && v[i].second >= 1 && v[i].second <= 2){
a[1][v[i].second]++;
sol += dist (v[i].first,v[i].second,1,v[i].second);
continue;
}
a[v[i].first][v[i].second]++;
}
for (i=1;i<=n;i++){
if (!a[i][1])
libere[1].push_back(i);
if (!a[i][2])
libere[2].push_back(i);
while (a[i][1] > 1){
coins[1].push_back (i);
a[i][1]--;
}
while (a[i][2] > 1){
coins[2].push_back(i);
a[i][2]--;
}
/// pun banii de pe prima linie in pozitiile libere tot de pe prima linie
while (!coins[1].empty() && !libere[1].empty()){
int poz1 = coins[1].back(), poz2 = libere[1].back();
sol += modul (poz1 - poz2);
coins[1].pop_back(), libere[1].pop_back();
}
/// la fel si pt linia a doua
while (!coins[2].empty() && !libere[2].empty()){
int poz1 = coins[2].back(), poz2 = libere[2].back();
sol += modul (poz1 - poz2);
coins[2].pop_back(), libere[2].pop_back();
}
/// acum mut de pe prima pe a doua si invers
while (!coins[1].empty() && !libere[2].empty()){
int poz1 = coins[1].back(), poz2 = libere[2].back();
sol += modul(poz1 - poz2) + 1;
coins[1].pop_back(), libere[2].pop_back();
}
while (!coins[2].empty() && !libere[1].empty()){
int poz1 = coins[2].back(), poz2 = libere[1].back();
sol += modul (poz1 - poz2) + 1;
coins[2].pop_back(), libere[1].pop_back();
}
}
/*
int poz1 = 1, poz2 = 1;
for (i=1;i<=n;i++){
/// incerc sa le duc cat mai mult in stanga
while (a[i][1] > 1){
while (poz1 < i && a[poz1][1])
poz1++;
if (poz1 < i){
sol += i - poz1;
a[i][1]--;
a[poz1][1] = 1;
} else break;
}
while (a[i][2] > 1){
while (poz1 < i && a[poz2][2])
poz2++;
if (poz2 < i){
sol += i - poz2;
a[i][2]--;
a[poz2][2] = 1;
} else break;
}
/// mut moneda de pe prima linie pe a doua si invers
while (a[i][1] > 1){
while (poz2 <= i && a[poz2][2])
poz2++;
if (poz2 <= i){
sol += i - poz2 + 1;
a[i][1]--;
a[poz2][2] = 1;
} else break;
}
while (a[i][2] > 1){
while (poz1 <= i && a[poz1][1])
poz1++;
if (poz1 <= i){
sol += i - poz1 + 1;
a[i][2]--;
a[poz1][1] = 1;
} else break;
}
}
/// acum duc spre dreapta monedele
poz1 = n, poz2 = n;
for (i=1;i<=n;i++){
while (a[i][1] > 1){
while (poz1 > i && a[poz1][1])
poz1--;
if (poz1 > i){
sol += poz1 - i;
a[i][1]--;
a[poz1][1] = 1;
} else break;
}
while (a[i][2] > 1){
while (poz2 > i && a[poz2][2])
poz2--;
if (poz2 > i){
sol += poz2 - i;
a[i][2]--;
a[poz2][2] = 1;
} else break;
}
while (a[i][1] > 1){
while (poz2 >= i && a[poz2][2])
poz2--;
if (poz2 >= i){
sol += poz2 - i + 1;
a[i][1]--;
a[poz2][2] = 1;
} else break;
}
while (a[i][2] > 1){
while (poz1 >= i && a[poz1][1])
poz1--;
if (poz1 >= i){
sol += poz1 - i + 1;
a[i][2]--;
a[poz1][1] = 1;
} else break;
}
}
*/
cout<<sol;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
384 KB |
Output is correct |
35 |
Correct |
7 ms |
512 KB |
Output is correct |
36 |
Correct |
7 ms |
384 KB |
Output is correct |
37 |
Correct |
6 ms |
384 KB |
Output is correct |
38 |
Correct |
7 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
384 KB |
Output is correct |
40 |
Correct |
7 ms |
384 KB |
Output is correct |
41 |
Correct |
7 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
400 KB |
Output is correct |
44 |
Correct |
6 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
512 KB |
Output is correct |
46 |
Correct |
6 ms |
384 KB |
Output is correct |
47 |
Correct |
7 ms |
384 KB |
Output is correct |
48 |
Correct |
7 ms |
384 KB |
Output is correct |
49 |
Correct |
6 ms |
384 KB |
Output is correct |
50 |
Correct |
7 ms |
384 KB |
Output is correct |
51 |
Correct |
6 ms |
384 KB |
Output is correct |
52 |
Correct |
7 ms |
384 KB |
Output is correct |
53 |
Correct |
6 ms |
384 KB |
Output is correct |
54 |
Correct |
7 ms |
384 KB |
Output is correct |
55 |
Correct |
6 ms |
384 KB |
Output is correct |
56 |
Correct |
7 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
6 ms |
384 KB |
Output is correct |
59 |
Correct |
7 ms |
384 KB |
Output is correct |
60 |
Correct |
7 ms |
512 KB |
Output is correct |
61 |
Correct |
7 ms |
384 KB |
Output is correct |
62 |
Correct |
7 ms |
384 KB |
Output is correct |
63 |
Correct |
6 ms |
384 KB |
Output is correct |
64 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
384 KB |
Output is correct |
35 |
Correct |
7 ms |
512 KB |
Output is correct |
36 |
Correct |
7 ms |
384 KB |
Output is correct |
37 |
Correct |
6 ms |
384 KB |
Output is correct |
38 |
Correct |
7 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
384 KB |
Output is correct |
40 |
Correct |
7 ms |
384 KB |
Output is correct |
41 |
Correct |
7 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
400 KB |
Output is correct |
44 |
Correct |
6 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
512 KB |
Output is correct |
46 |
Correct |
6 ms |
384 KB |
Output is correct |
47 |
Correct |
7 ms |
384 KB |
Output is correct |
48 |
Correct |
7 ms |
384 KB |
Output is correct |
49 |
Correct |
6 ms |
384 KB |
Output is correct |
50 |
Correct |
7 ms |
384 KB |
Output is correct |
51 |
Correct |
6 ms |
384 KB |
Output is correct |
52 |
Correct |
7 ms |
384 KB |
Output is correct |
53 |
Correct |
6 ms |
384 KB |
Output is correct |
54 |
Correct |
7 ms |
384 KB |
Output is correct |
55 |
Correct |
6 ms |
384 KB |
Output is correct |
56 |
Correct |
7 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
6 ms |
384 KB |
Output is correct |
59 |
Correct |
7 ms |
384 KB |
Output is correct |
60 |
Correct |
7 ms |
512 KB |
Output is correct |
61 |
Correct |
7 ms |
384 KB |
Output is correct |
62 |
Correct |
7 ms |
384 KB |
Output is correct |
63 |
Correct |
6 ms |
384 KB |
Output is correct |
64 |
Correct |
6 ms |
384 KB |
Output is correct |
65 |
Correct |
139 ms |
7416 KB |
Output is correct |
66 |
Correct |
294 ms |
9328 KB |
Output is correct |
67 |
Correct |
224 ms |
9080 KB |
Output is correct |
68 |
Correct |
218 ms |
8952 KB |
Output is correct |
69 |
Correct |
227 ms |
9080 KB |
Output is correct |
70 |
Correct |
226 ms |
9084 KB |
Output is correct |
71 |
Correct |
225 ms |
8952 KB |
Output is correct |
72 |
Correct |
218 ms |
8176 KB |
Output is correct |
73 |
Correct |
220 ms |
8556 KB |
Output is correct |
74 |
Correct |
213 ms |
7480 KB |
Output is correct |
75 |
Correct |
210 ms |
7160 KB |
Output is correct |
76 |
Correct |
215 ms |
7544 KB |
Output is correct |
77 |
Correct |
212 ms |
7472 KB |
Output is correct |
78 |
Correct |
215 ms |
7544 KB |
Output is correct |
79 |
Correct |
216 ms |
7372 KB |
Output is correct |
80 |
Correct |
212 ms |
7284 KB |
Output is correct |
81 |
Correct |
213 ms |
7484 KB |
Output is correct |
82 |
Correct |
213 ms |
6904 KB |
Output is correct |
83 |
Correct |
215 ms |
7032 KB |
Output is correct |
84 |
Correct |
215 ms |
7288 KB |
Output is correct |
85 |
Correct |
214 ms |
7160 KB |
Output is correct |
86 |
Correct |
216 ms |
8568 KB |
Output is correct |
87 |
Correct |
223 ms |
8568 KB |
Output is correct |
88 |
Correct |
220 ms |
9080 KB |
Output is correct |
89 |
Correct |
219 ms |
8952 KB |
Output is correct |
90 |
Correct |
229 ms |
8952 KB |
Output is correct |
91 |
Correct |
226 ms |
8952 KB |
Output is correct |
92 |
Correct |
225 ms |
9080 KB |
Output is correct |
93 |
Correct |
230 ms |
9004 KB |
Output is correct |
94 |
Correct |
216 ms |
8952 KB |
Output is correct |
95 |
Correct |
217 ms |
8824 KB |
Output is correct |
96 |
Correct |
221 ms |
10224 KB |
Output is correct |
97 |
Correct |
222 ms |
9968 KB |
Output is correct |
98 |
Correct |
221 ms |
8856 KB |
Output is correct |
99 |
Correct |
217 ms |
8820 KB |
Output is correct |
100 |
Correct |
223 ms |
8860 KB |
Output is correct |
101 |
Correct |
215 ms |
9332 KB |
Output is correct |
102 |
Correct |
215 ms |
8908 KB |
Output is correct |
103 |
Correct |
220 ms |
8688 KB |
Output is correct |