This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
const int N = 2e5 + 5;
const int K = 10;
ll dp[N][K * 2];
void solve() {
int n;
cin >> n;
n *= 2;
vector<pair<ll, ll>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
fill(dp[i], dp[i] + K * 2, 1e18);
}
fill(dp[n], dp[n] + K * 2, 1e18);
sort(a.begin(), a.end());
dp[0][K] = 0;
for (int i = 1; i <= n; i++) {
for (int diff = -K; diff < K; diff++) {
int x = (i + diff) / 2;
int y = i - x;
if (x < 0 || x > i || y < 0 || y > i || x - y != diff) continue;
if (diff > -K) {
dp[i][diff + K] = min(dp[i][diff + K], dp[i - 1][diff + K - 1] + abs(a[i - 1].first - x) + abs(a[i - 1].second - 1));
}
if (diff + 1 < K) {
dp[i][diff + K] = min(dp[i][diff + K], dp[i - 1][diff + K + 1] + abs(a[i - 1].first - y) + abs(a[i - 1].second - 2));
}
}
}
cout << dp[n][K] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |