#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 1010
int n, mark[maxn][maxn], dp[maxn][maxn];
vector<pair<int,int>> p;
int sol(int i, int j) {
if(mark[i][j]) return dp[i][j];
mark[i][j] = 1;
dp[i][j] = inf;
int id = i+j;
if(id == 2*n) {
return dp[i][j] = 0;
}
if(i != n) {
//pegar com id em cima
int val = abs(p[id].fr-i-1) + abs(p[id].sc-2);
dp[i][j] = min(sol(i+1,j)+val,dp[i][j]);
}
if(j != n) {
//pegar com id em cima
int val = abs(p[id].fr-j-1) + abs(p[id].sc-1);
dp[i][j] = min(sol(i,j+1)+val,dp[i][j]);
}
return dp[i][j];
}
void solve() {
cin >> n;
for(int i = 0; i < 2*n; i++) {
int x, y;
cin >> x >> y;
p.pb(mp(x,y));
}
sort(all(p));
cout << sol(0,0) << endl;
// for(int i = 0; i <= n; i++) {
// for(int j = 0; j <= n; j++) {
// cout << i << " " << j << " " << sol(i,j) << endl;
// }
// }
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
316 KB |
Output is correct |
34 |
Correct |
20 ms |
16288 KB |
Output is correct |
35 |
Correct |
21 ms |
16328 KB |
Output is correct |
36 |
Correct |
20 ms |
16324 KB |
Output is correct |
37 |
Correct |
21 ms |
16352 KB |
Output is correct |
38 |
Correct |
21 ms |
16372 KB |
Output is correct |
39 |
Correct |
20 ms |
16332 KB |
Output is correct |
40 |
Correct |
20 ms |
16388 KB |
Output is correct |
41 |
Correct |
20 ms |
16400 KB |
Output is correct |
42 |
Correct |
20 ms |
16292 KB |
Output is correct |
43 |
Correct |
20 ms |
16264 KB |
Output is correct |
44 |
Correct |
20 ms |
16336 KB |
Output is correct |
45 |
Correct |
20 ms |
16328 KB |
Output is correct |
46 |
Correct |
20 ms |
16332 KB |
Output is correct |
47 |
Correct |
24 ms |
16332 KB |
Output is correct |
48 |
Correct |
20 ms |
16328 KB |
Output is correct |
49 |
Correct |
21 ms |
16264 KB |
Output is correct |
50 |
Correct |
21 ms |
16304 KB |
Output is correct |
51 |
Correct |
20 ms |
16376 KB |
Output is correct |
52 |
Correct |
20 ms |
16344 KB |
Output is correct |
53 |
Correct |
20 ms |
16296 KB |
Output is correct |
54 |
Correct |
20 ms |
16332 KB |
Output is correct |
55 |
Correct |
22 ms |
16288 KB |
Output is correct |
56 |
Correct |
20 ms |
16332 KB |
Output is correct |
57 |
Correct |
20 ms |
16296 KB |
Output is correct |
58 |
Correct |
20 ms |
16284 KB |
Output is correct |
59 |
Correct |
20 ms |
16332 KB |
Output is correct |
60 |
Correct |
20 ms |
16332 KB |
Output is correct |
61 |
Correct |
20 ms |
16376 KB |
Output is correct |
62 |
Correct |
20 ms |
16364 KB |
Output is correct |
63 |
Correct |
20 ms |
16332 KB |
Output is correct |
64 |
Correct |
22 ms |
16336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
316 KB |
Output is correct |
34 |
Correct |
20 ms |
16288 KB |
Output is correct |
35 |
Correct |
21 ms |
16328 KB |
Output is correct |
36 |
Correct |
20 ms |
16324 KB |
Output is correct |
37 |
Correct |
21 ms |
16352 KB |
Output is correct |
38 |
Correct |
21 ms |
16372 KB |
Output is correct |
39 |
Correct |
20 ms |
16332 KB |
Output is correct |
40 |
Correct |
20 ms |
16388 KB |
Output is correct |
41 |
Correct |
20 ms |
16400 KB |
Output is correct |
42 |
Correct |
20 ms |
16292 KB |
Output is correct |
43 |
Correct |
20 ms |
16264 KB |
Output is correct |
44 |
Correct |
20 ms |
16336 KB |
Output is correct |
45 |
Correct |
20 ms |
16328 KB |
Output is correct |
46 |
Correct |
20 ms |
16332 KB |
Output is correct |
47 |
Correct |
24 ms |
16332 KB |
Output is correct |
48 |
Correct |
20 ms |
16328 KB |
Output is correct |
49 |
Correct |
21 ms |
16264 KB |
Output is correct |
50 |
Correct |
21 ms |
16304 KB |
Output is correct |
51 |
Correct |
20 ms |
16376 KB |
Output is correct |
52 |
Correct |
20 ms |
16344 KB |
Output is correct |
53 |
Correct |
20 ms |
16296 KB |
Output is correct |
54 |
Correct |
20 ms |
16332 KB |
Output is correct |
55 |
Correct |
22 ms |
16288 KB |
Output is correct |
56 |
Correct |
20 ms |
16332 KB |
Output is correct |
57 |
Correct |
20 ms |
16296 KB |
Output is correct |
58 |
Correct |
20 ms |
16284 KB |
Output is correct |
59 |
Correct |
20 ms |
16332 KB |
Output is correct |
60 |
Correct |
20 ms |
16332 KB |
Output is correct |
61 |
Correct |
20 ms |
16376 KB |
Output is correct |
62 |
Correct |
20 ms |
16364 KB |
Output is correct |
63 |
Correct |
20 ms |
16332 KB |
Output is correct |
64 |
Correct |
22 ms |
16336 KB |
Output is correct |
65 |
Runtime error |
83 ms |
25176 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |