#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,a,b) for (int i=(a);i<(b);i++)
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define pq priority_queue
#define debug(x) cerr<<#x<<"="<<x<<"\n";
const int maxn = 2e5+10;
int a[2][maxn],b[maxn];
int bg[2]={0,0},ed[2]={0,0};
int n;
int main() {
cin.tie(0);
cin>>n;
ll ans = 0;
rep(i,0,2*n) {
int x,y;
cin>>x>>y;
ans += abs(y-1);
if (y<=1) a[0][ed[0]++] = x;
else a[1][ed[1]++]=x;
}
sort(a[0],a[0]+ed[0]);
sort(a[1],a[1]+ed[1]);
int curx =0;
// debug(ans);
while (bg[0]<ed[0] or bg[1]<ed[1]) {
curx++;
int cur = 0;
if (bg[0]==ed[0] or (bg[1]<ed[1] and a[1][bg[1]] < a[0][bg[0]])) cur = 1;
if (bg[cur]+1==ed[cur] or (bg[1-cur]!=ed[1-cur] and a[1-cur][bg[1-cur]]-a[cur][bg[cur]+1]<=0)) {
ans--;
ans += abs(a[1-cur][bg[1-cur]]-curx);
// debug(a[1-cur][bg[1-cur]]);
bg[1-cur]++;
// debug(233);
}
else {
if (cur==0) ans++;
else ans--;
ans += abs(a[cur][bg[cur]]-curx);
// debug(a[cur][bg[cur]]);
bg[cur]++;
}
ans += abs(a[cur][bg[cur]]-curx);
// debug(a[cur][bg[cur]]);
bg[cur]++;
// debug(ans);
// debug(curx);
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |