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;
const ll INF = 1e18;
int n, x[200200], y[200200];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1;i<=n+n;i++) cin >> x[i] >> y[i];
vector<int> A, B;
ll ans = 0;
for(int i=1;i<=n+n;i++) {
int D = min(max(x[i], 1), n);
ans += abs(D-x[i]);
if(y[i] >= 2) {
A.push_back(D);
ans += y[i]-2;
} else {
B.push_back(D);
ans += 1-y[i];
}
}
if(A.size() > B.size()) swap(A, B);
sort(A.begin(), A.end()), sort(B.begin(), B.end());
vector<vector<ll>> dp(n+1, vector<ll>(A.size()+1, INF));
dp[0][0] = 0;
for(int i=1;i<=n;i++) {
for(int a=0;a<=A.size();a++) {
int b = i+i-a;
if(b < 0 || b > B.size()) continue;
if(a >= 2) dp[i][a] = min(dp[i][a], dp[i-1][a-2] + 1 + abs(A[a-2]-i) + abs(A[a-1]-i));
if(b >= 2) dp[i][a] = min(dp[i][a], dp[i-1][a] + 1 + abs(B[b-2]-i) + abs(B[b-1]-i));
if(a >= 1 && b >= 1) dp[i][a] = min(dp[i][a], dp[i-1][a-1] + abs(B[b-1]-i) + abs(A[a-1]-i));
}
}
cout << ans + dp[n][A.size()];
}
Compilation message (stderr)
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int a=0;a<=A.size();a++) {
| ~^~~~~~~~~~
joi2019_ho_t4.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if(b < 0 || b > B.size()) continue;
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |