Submission #384779

#TimeUsernameProblemLanguageResultExecution timeMemory
384779valerikkCoin Collecting (JOI19_ho_t4)C++17
8 / 100
19 ms9196 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1005; int n; int x[2 * N], y[2 * N]; ll ans = 0; ll d[N][N]; int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> x[i] >> y[i]; if (x[i] < 1) { ans += 1 - x[i]; x[i] = 1; } if (x[i] > n) { ans += x[i] - n; x[i] = n; } if (y[i] < 1) { ans += 1 - y[i]; y[i] = 1; } if (y[i] > 2) { ans += y[i] - 2; y[i] = 2; } x[i]--; y[i]--; } vector<int> q, w; for (int i = 0; i < 2 * n; i++) { if (y[i] == 0) { q.push_back(x[i]); } else { w.push_back(x[i]); } } sort(q.begin(), q.end()); sort(w.begin(), w.end()); int qq = q.size(), ww = w.size(); for (int i = 0; i <= qq; i++) { for (int j = 0; j <= ww; j++) d[i][j] = 1e18; } d[0][0] = 0; for (int i = 0; i <= qq; i++) { for (int j = 0; j <= ww; j++) { int xx = (i + j) / 2, yy = (i + j) % 2; if (i < qq) { d[i + 1][j] = min(d[i + 1][j], d[i][j] + abs(xx - q[i]) + yy); } if (j < ww) { d[i][j + 1] = min(d[i][j + 1], d[i][j] + abs(xx - w[j]) + 1 - yy); } } } ans += d[qq][ww]; cout << ans << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...