//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define F first
#define S second
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int N = 500001;
using namespace std;
using namespace __gnu_pbds;
ll n, a, b, p[N][3], ans, f[N][3];
vector <ll> v[3], q[3];
ll gcd (ll a, ll b){while (a > 0 && b > 0){if (a >= b){a %= b;}else{b %= a;}}return a + b;}
ll binpow (ll a, ll b){
a %= mod;if (b == 0){return 1;}
else if (b % 2 == 1){
return binpow (a, b - 1) % mod * a % mod;
}
else{
ll t = binpow (a, b / 2) % mod;
return t * t % mod;
}
}
signed main (){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n + n; i++){
cin >> a >> b;
if (a < 1){
if (b >= 2){
p[1][2]++;
ans += abs (a - 1) + abs (b - 2);
}
else{
p[1][1]++;
ans += abs (a - 1) + abs (b - 1);
}
}
else if (a > n){
if (b >= 2){
p[n][2]++;
ans += abs (a - n) + abs (b - 2);
}
else{
p[n][1]++;
ans += abs (a - n) + abs (b - 1);
}
}
else{
if (b >= 2){
p[a][2]++;
ans += abs (b - 2);
}
else{
p[a][1]++;
ans += abs (b - 1);
}
}
}
// for (int i = 1; i <= n; i++){
// for (int y = 1; y <= 2; y++){
// cout << p[i][y] << ' ';
// }
// cout << '\n';
// }
for (int i = 1; i <= n; i++){
for (int y = 1; y <= 2; y++){
if (p[i][y] == 0){
a = y;
if (y == 1){
b = 2;
}
else{
b = 1;
}
if (v[a].size()){
ans += abs (v[a][v[a].size() - 1] - i);
p[v[a][v[a].size() - 1]][a]--;
if (p[v[a][v[a].size() - 1]][a] == 1){
v[a].ppb();
}
}
else if (v[b].size()){
ans += abs (v[b][v[b].size() - 1] - i);
ans++;
p[v[b][v[b].size() - 1]][b]--;
if (p[v[b][v[b].size() - 1]][b] == 1){
v[b].ppb();
}
}
else{
q[y].pb(i);
}
}
else if (p[i][y] > 1){
if (y == 1){
b = 2;
}
else{
b = 1;
}
while (p[i][y] > 1 && q[y].size()){
ans += abs (i - q[y][q[y].size() - 1]);
q[y].ppb();
p[i][y]--;
}
while (p[i][y] > 1 && q[b].size()){
ans += abs (i - q[b][q[b].size() - 1]) + 1;
q[b].ppb();
p[i][y]--;
}
if (p[i][y] > 1){
v[y].pb (i);
}
}
}
// cout << ans << ' ' << v.size() << '\n';
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Incorrect |
1 ms |
408 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Incorrect |
1 ms |
408 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Incorrect |
1 ms |
408 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |