This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const bool typetest = 0;
const int N = 1e3 + 5;
const ll Inf = 1e17;
pair<ll, ll> a[N * 2];
int n;
ll dp[N][N];
void Read()
{
cin >> n;
for (int i = 1; i <= n * 2; ++i)
cin >> a[i].first >> a[i].second;
sort(a + 1, a + n * 2 + 1);
}
inline void Min(ll &x, ll y)
{
if (x > y)
x = y;
}
void Solve()
{
fill_n(&dp[0][0], N * N, Inf);
dp[0][0] = 0;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
{
if (i < n)
Min(dp[i + 1][j], dp[i][j] + abs(a[i + j + 1].first - i - 1) + abs(a[i + j + 1].second - 1));
if (j < n)
Min(dp[i][j + 1], dp[i][j] + abs(a[i + j + 1].first - j - 1) + abs(a[i + j + 1].second - 2));
}
cout << dp[n][n];
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |