/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n;
array<ll, 2> a[N];
void solve(){
cin >> n;
for(int i = 1; i <= 2*n; ++i) cin >> a[i][0] >> a[i][1];
ll ans = 0;
for(int i = 1; i <= 2*n; ++i){
if(a[i][1] >= 2){
ans += a[i][1] - 2;
a[i][1] = 2;
}else if(a[i][1] <= 1){
ans += 1 - a[i][1];
a[i][1] = 1;
}
if(a[i][0] <= 1){
ans += 1 - a[i][0];
a[i][0] = 1;
}else if(a[i][0] >= n){
ans += a[i][0] - n;
a[i][0] = n;
}
}
sort(a+1, a+1+2*n);
deque<int> X, Y;
for(int i = 1; i <= 2*n; ++i){
if(a[i][1] == 1) X.pb(a[i][0]);
else Y.pb(a[i][0]);
}
for(int i = 1; i <= n; ++i){
for(int rep = 0; rep < 2; ++rep){
if(!X.empty()){
if(Y.empty()){
ans += abs(X.front() - i);
X.pop_front();
}else{
if(X.front() < Y.front() + 1 || (X.front() == Y.front() + 1 && X.size() > Y.size())){
ans += abs(X.front() - i);
X.pop_front();
}else{
ans += abs(Y.front() - i) + 1;
Y.pop_front();
}
}
}else{
ans += abs(Y.front() - i) + 1;
Y.pop_front();
}
X.swap(Y);
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:72:15: warning: unused variable 'aa' [-Wunused-variable]
72 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |