#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define inf 2000000000
#define N 100005
#define MOD 998244353
using namespace std;
ll ans;
int n;
int a[3][N],no[3][N];
void solve() {
// cerr << ans << "\n";
multiset<int> s[3];
for(int i=1;i<=2;i++) {
for(int j=1;j<=n;j++) {
if(a[i][j]) no[i][j]=1;
for(int k=1;k<=a[i][j]-1;k++) s[i].insert(j);
}
}
for(int j=1;j<=n;j++) {
bool ok[3]={0};
for(int i=1;i<=2;i++) {
if(no[i][j]) {
ok[i]=1;
continue ;
}
vector<ii> cd;
auto pos=s[i].lower_bound(j);
if(pos!=s[i].end()) cd.pb({abs(j-*pos),*pos});
if(pos!=s[i].begin()) {
pos=prev(pos);
cd.pb({abs(j-*pos),*pos});
}
if(!sz(cd)) continue ;
sort(all(cd));
ans+=cd[0].st;
s[i].erase(s[i].find(cd[0].nd));
ok[i]=1;
}
for(int i=1;i<=2;i++) {
if(ok[i]) continue ;
vector<ii> cd;
auto pos=s[3-i].lower_bound(j);
if(pos!=s[3-i].end()) cd.pb({abs(j-*pos)+1,*pos});
if(pos!=s[3-i].begin()) {
pos=prev(pos);
cd.pb({abs(j-*pos)+1,*pos});
}
sort(all(cd));
ans+=cd[0].st;
s[3-i].erase(s[3-i].find(cd[0].nd));
ok[i]=1;
}
}
}
int dst(ii a,ii b) {
return abs(a.st-b.st)+abs(a.nd-b.nd);
}
ii fnear(int x,int y) {
ii res;
if(y>=2) res.nd=2;
else res.nd=1;
if(x>n) res.st=n;
else if(x<1) res.st=1;
else res.st=x;
ans+=dst({x,y},res);
return res;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n*2;i++) {
int x,y;
scanf("%d %d",&x,&y);
ii near=fnear(x,y);
a[near.nd][near.st]++;
}
// for(int i=1;i<=2;i++,cerr<<"\n") for(int j=1;j<=n;j++) cerr<<a[i][j]<<" ";
solve();
printf("%lld",ans);
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:137:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
joi2019_ho_t4.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |