This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: hashman
* created:
**/
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> pts(2 * n + 1);
for (int i = 1; i <= 2 * n; i++) {
cin >> pts[i].first >> pts[i].second;
}
sort(pts.begin() + 1, pts.end());
vector<vector<int64_t>> dp(n + 1, vector<int64_t>(n + 1));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i > 0 || j > 0) {
dp[i][j] = INT64_MAX;
if (i > 0) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + abs(pts[i + j].first - i) + abs(pts[i + j].second - 1));
}
if (j > 0) {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + abs(pts[i + j].first - j) + abs(pts[i + j].second - 2));
}
}
}
}
cout << dp[n][n] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |