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;
#define int long long
const int N = (int)2e3+10;
const int LINF = (int)2e18;
int n, dp[N][N];
array<int,2> a[2][N], loc[N];
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; n*=2;
for(int i = 1; i <= n; i++)
cin >> loc[i][0] >> loc[i][1];
sort(loc+1,loc+n+1);
for(int i = 1; i <= n/2; i++)
a[0][i] = {i,1}, a[1][i] = {i,2};
for(int i = 0; i <= n/2; i++)
for(int j = 0; j <= n/2; j++)
dp[i][j]=LINF;
dp[0][0] = 0;
for(int i = 0; i <= n/2; i++){
for(int j = 0; j <= n/2; j++){
if(i) dp[i][j] = min(dp[i][j], dp[i-1][j]
+abs(i-loc[i+j][0])+abs(1-loc[i+j][1]));
if(j) dp[i][j] = min(dp[i][j], dp[i][j-1]
+abs(j-loc[i+j][0])+abs(2-loc[i+j][1]));
}
}
cout << dp[n/2][n/2] << "\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... |