#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,fma,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 1e5 + 5;
ll n, dp[maxn][3];
map<int, vi> t;
vector<pi> a;
ll dist(pi a, pi b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int x, y, i = 0; i < 2*n; i++) cin >> x >> y, t[x].pb(y);
for(auto i : t) {
int l = 0, r = i.second.size(), q = 0;
while(l<r) {
if(q) a.pb({i.first, i.second[l++]});
else a.pb({i.first, i.second[--r]});
q ^= 1;
}
}
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for(int i = 0; i < 2*n; i++) {
dp[1 + (i/2)][0] = min(dp[1 + (i/2)][0], dp[i/2][2] + dist(a[i], {1 + (i/2), 1}));
dp[1 + (i/2)][0] = min(dp[1 + (i/2)][0], dp[i/2][1] + dist(a[i], {1 + (i/2), 2}));
dp[i/2][1] = min(dp[i/2][1], dp[i/2][0] + dist(a[i], {1 + (i/2), 1}));
dp[i/2][2] = min(dp[i/2][2], dp[i/2][0] + dist(a[i], {1 + (i/2), 2}));
}
cout << dp[n][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2684 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Incorrect |
6 ms |
2684 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2684 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Incorrect |
6 ms |
2684 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2684 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Incorrect |
6 ms |
2684 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |