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 ll long long
const int mxN=1e5;
int n;
ll a1, dp[2*mxN+1][100];
array<int, 3> a[2*mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=0; i<2*n; ++i) {
cin >> a[i][0] >> a[i][2];
a[i][1]=rand();
if(a[i][0]<1) {
a1+=1-a[i][0];
a[i][0]=1;
} else if(a[i][0]>n) {
a1+=a[i][0]-n;
a[i][0]=n;
}
if(a[i][2]<1) {
a1+=1-a[i][2];
a[i][2]=1;
} else if(a[i][2]>2) {
a1+=a[i][2]-2;
a[i][2]=2;
}
}
sort(a, a+2*n);
memset(dp, 0x3f, sizeof(dp));
dp[0][48]=a1;
for(int i=0; i<2*n; ++i) {
for(int j=max(i/2-46, 0); j<=min(i/2+46, n); ++j) {
if(j<n)
dp[i+1][j-(i+1)/2+49]=min(dp[i][j-i/2+48]+abs(j+1-a[i][0])+(a[i][2]!=1), dp[i+1][j-(i+1)/2+49]);
dp[i+1][j-(i+1)/2+48]=min(dp[i][j-i/2+48]+abs(i-j+1-a[i][0])+(a[i][2]!=2), dp[i+1][j-(i+1)/2+48]);
}
}
cout << dp[2*n][48];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |