#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo it[100001][2];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n;
cin>>n;
memset(it,0,sizeof(it));
vector<pair<llo,llo>> ac;
llo aa,bb;
llo ans=0;
for(llo i=0;i<2*n;i++){
cin>>aa>>bb;
if(bb<=1){
if(aa<1){
it[0][0]+=1;
ac.pb({0,0});
}
else if(aa>n){
it[n-1][0]+=1;
ac.pb({n-1,0});
}
else{
it[aa-1][0]+=1;
ac.pb({aa-1,0});
}
}
else{
if(aa<1){
it[0][1]+=1;
ac.pb({0,1});
}
else if(aa>n){
it[n-1][1]+=1;
ac.pb({n-1,1});
}
else{
it[aa-1][1]+=1;
ac.pb({aa-1,1});
}
}
ans+=abs(aa-(ac[i].a+1))+abs(bb-(ac[i].b+1));
// cout<<ans<<endl;
// cout<<ac[ac.size()-1].a<<","<<ac[ac.size()-1].b<<endl;
}
//cout<<ans<<endl;
sort(ac.begin(),ac.end());
llo dp[2*n][n+1];
for(llo i=0;i<2*n;i++){
for(llo j=0;j<=n;j++){
dp[i][j]=2*n*n;
}
}
for(llo i=0;i<2*n;i++){
// cout<<ac[i].a<<":"<<ac[i].b<<endl;
llo co=0;
if(ac[i].b==1){
co=1;
}
if(i==0){
dp[0][0]=abs(ac[i].a)+1-co;
dp[0][1]=abs(ac[i].a)+co;
}
else{
if(i<n){
dp[i][0]=dp[i-1][0]+abs(i-ac[i].a)+1-co;
// cout<<dp[i-1][0]<<i<<" "<<ac[i].a<<" "<<co<<endl;
}
for(llo j=1;j<=min(i+1,n);j++){
if((i-j)>=0 and (i-j)<n){
dp[i][j]=min(dp[i][j],dp[i-1][j]+abs((i-j)-ac[i].a)+1-co);
}
//if(j<n){
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+abs((j-1)-ac[i].a)+co);
//}
}
}
/* for(llo j=0;j<=n;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;*/
}
ans+=dp[2*n-1][n];
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
6 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
6 ms |
1920 KB |
Output is correct |
6 |
Correct |
6 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
6 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
6 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
6 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
6 ms |
1920 KB |
Output is correct |
6 |
Correct |
6 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
6 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
6 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
34 |
Correct |
18 ms |
17664 KB |
Output is correct |
35 |
Correct |
18 ms |
17664 KB |
Output is correct |
36 |
Correct |
18 ms |
17664 KB |
Output is correct |
37 |
Correct |
19 ms |
17664 KB |
Output is correct |
38 |
Correct |
18 ms |
17664 KB |
Output is correct |
39 |
Correct |
19 ms |
17664 KB |
Output is correct |
40 |
Correct |
18 ms |
17664 KB |
Output is correct |
41 |
Correct |
18 ms |
17664 KB |
Output is correct |
42 |
Correct |
21 ms |
17664 KB |
Output is correct |
43 |
Correct |
20 ms |
17664 KB |
Output is correct |
44 |
Correct |
19 ms |
17664 KB |
Output is correct |
45 |
Correct |
18 ms |
17664 KB |
Output is correct |
46 |
Correct |
19 ms |
17664 KB |
Output is correct |
47 |
Correct |
18 ms |
17664 KB |
Output is correct |
48 |
Correct |
18 ms |
17664 KB |
Output is correct |
49 |
Correct |
18 ms |
17664 KB |
Output is correct |
50 |
Correct |
18 ms |
17664 KB |
Output is correct |
51 |
Correct |
18 ms |
17664 KB |
Output is correct |
52 |
Correct |
19 ms |
17664 KB |
Output is correct |
53 |
Correct |
18 ms |
17664 KB |
Output is correct |
54 |
Correct |
18 ms |
17664 KB |
Output is correct |
55 |
Correct |
18 ms |
17664 KB |
Output is correct |
56 |
Correct |
19 ms |
17664 KB |
Output is correct |
57 |
Correct |
19 ms |
17664 KB |
Output is correct |
58 |
Correct |
19 ms |
17792 KB |
Output is correct |
59 |
Correct |
18 ms |
17664 KB |
Output is correct |
60 |
Correct |
18 ms |
17664 KB |
Output is correct |
61 |
Correct |
18 ms |
17664 KB |
Output is correct |
62 |
Correct |
19 ms |
17664 KB |
Output is correct |
63 |
Correct |
18 ms |
17664 KB |
Output is correct |
64 |
Correct |
18 ms |
17792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
6 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
6 ms |
1920 KB |
Output is correct |
6 |
Correct |
6 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
5 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
6 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
6 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
1920 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
1920 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
1920 KB |
Output is correct |
34 |
Correct |
18 ms |
17664 KB |
Output is correct |
35 |
Correct |
18 ms |
17664 KB |
Output is correct |
36 |
Correct |
18 ms |
17664 KB |
Output is correct |
37 |
Correct |
19 ms |
17664 KB |
Output is correct |
38 |
Correct |
18 ms |
17664 KB |
Output is correct |
39 |
Correct |
19 ms |
17664 KB |
Output is correct |
40 |
Correct |
18 ms |
17664 KB |
Output is correct |
41 |
Correct |
18 ms |
17664 KB |
Output is correct |
42 |
Correct |
21 ms |
17664 KB |
Output is correct |
43 |
Correct |
20 ms |
17664 KB |
Output is correct |
44 |
Correct |
19 ms |
17664 KB |
Output is correct |
45 |
Correct |
18 ms |
17664 KB |
Output is correct |
46 |
Correct |
19 ms |
17664 KB |
Output is correct |
47 |
Correct |
18 ms |
17664 KB |
Output is correct |
48 |
Correct |
18 ms |
17664 KB |
Output is correct |
49 |
Correct |
18 ms |
17664 KB |
Output is correct |
50 |
Correct |
18 ms |
17664 KB |
Output is correct |
51 |
Correct |
18 ms |
17664 KB |
Output is correct |
52 |
Correct |
19 ms |
17664 KB |
Output is correct |
53 |
Correct |
18 ms |
17664 KB |
Output is correct |
54 |
Correct |
18 ms |
17664 KB |
Output is correct |
55 |
Correct |
18 ms |
17664 KB |
Output is correct |
56 |
Correct |
19 ms |
17664 KB |
Output is correct |
57 |
Correct |
19 ms |
17664 KB |
Output is correct |
58 |
Correct |
19 ms |
17792 KB |
Output is correct |
59 |
Correct |
18 ms |
17664 KB |
Output is correct |
60 |
Correct |
18 ms |
17664 KB |
Output is correct |
61 |
Correct |
18 ms |
17664 KB |
Output is correct |
62 |
Correct |
19 ms |
17664 KB |
Output is correct |
63 |
Correct |
18 ms |
17664 KB |
Output is correct |
64 |
Correct |
18 ms |
17792 KB |
Output is correct |
65 |
Runtime error |
74 ms |
11872 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
66 |
Halted |
0 ms |
0 KB |
- |