#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
using namespace std;
const int inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int hizalay(int &y){
if(y >= 2){
int k = y - 2;
y = 2;
return k;
}
else{
int k = 1 - y;
y = 1;
return k;
}
}
int hizalax(int &x, int n){
if(x >= n){
int k = x - n;
x = n;
return k;
}
else if(x <= 1){
int k = 1 - x;
x = 1;
return k;
}
else return 0;
}
int32_t main(){
ios_base::sync_with_stdio(false), cin.tie();
int n;
cin>>n;
vector<pair<int, int> > a(n*2);
vector<vector<int> > grid(n+1, vector<int>(2));
int ans = 0, diff = 0, sz;
for(int i = 0; i < 2*n; i++){
cin>>a[i].first>>a[i].second;
ans += hizalay(a[i].second);
ans += hizalax(a[i].first, n);
a[i].first--, a[i].second--;
grid[a[i].first][a[i].second]++;
}
vector<int> rem[2];
for(int i = 0; i < n; i++){
int cnt = min((int)(rem[1].size()), grid[i][1]);
sz = rem[1].size();
for(int j = sz-1; j >= sz - cnt; j--){
ans += (i - rem[1][j]);
rem[1].pop_back();
}
grid[i][1] -= cnt;
cnt = min((int)(rem[0].size()), grid[i][0]);
sz = rem[0].size();
for(int j = sz-1; j >= sz - cnt; j--){
ans += (i - rem[0][j]);
rem[0].pop_back();
}
grid[i][0] -= cnt;
// şüpheli olan yer
if(rem[0].size() && grid[i][1]){
cnt = min((int)(rem[0].size()), grid[i][1]);
sz = rem[0].size();
for(int j = sz-1; j >= sz - cnt; j--){
ans += (i - rem[0][j]);
rem[0].pop_back();
}
grid[i][1] -= cnt;
}
else if(rem[1].size() && grid[i][0]){
cnt = min((int)(rem[1].size()), grid[i][0]);
sz = rem[1].size();
for(int j = sz-1; j >= sz - cnt; j--){
ans += (i - rem[1][j]);
rem[1].pop_back();
}
grid[i][0] -= cnt;
}
if(grid[i][1] > 1 && !grid[i][0]){
ans++;
grid[i][1]--;
grid[i][0]++;
}
if(!grid[i][1] && grid[i][0] > 1){
ans++;
grid[i][0]--;
grid[i][1]++;
}
if(grid[i][1] > 1){
int ver = grid[i][1] - 1;
grid[i+1][1] += ver;
grid[i][1] -= ver;
ans += ver;
}
if(grid[i][0] > 1){
int ver = grid[i][0] - 1;
grid[i+1][0] += ver;
grid[i][0] -= ver;
ans += ver;
}
if(!grid[i][1]) rem[1].pb(i);
if(!grid[i][0]) rem[0].pb(i);
}
cout<<ans<<endl;
return 0;
}
Compilation message
joi2019_ho_t4.cpp: In function 'int32_t main()':
joi2019_ho_t4.cpp:44:18: warning: unused variable 'diff' [-Wunused-variable]
44 | int ans = 0, diff = 0, sz;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |