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>
//#include "slow.h"
#define f first
#define s second
#define ent '\n'
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
typedef long long ll;
using namespace std;
struct seg{
int m1,m2,sum,cnt;
};
const string out[2]={"NO\n","YES\n"};
const ll dx[]={0,1,0,-1,-1,1,1,-1};
const ll dy[]={1,0,-1,0,-1,1,-1,1};
const int mod=998244353;
const int md=1e9+7;
const int mx=2e5+12;
const bool T=0;
ll dp[501][501];
ll c[501];
int n,m;
long long min_total_length(vector<int> a, vector<int> b){
n=a.size(), m=b.size();
if(n>m){
swap(n,m);
swap(a,b);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i=0;i<m;i++){
for(int j=1;j<n;j++){
if(abs(b[i]-a[j])<abs(b[i]-a[c[i]])){
c[i]=j;
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++) {
dp[i][j] = 1e18;
}
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++) {
long long sum = 0;
for (int k = j; k; k--) {
long long val = sum + abs(b[k - 1] - a[i - 1]);
dp[i][j] = min(dp[i][j], dp[i - 1][k - 1] + val);
sum += abs(b[k - 1] - a[c[k - 1]]);
}
}
}
return dp[n][m];
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |