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 <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e16;
long long min_total_length(std::vector<int> RR, std::vector<int> BB)
{
vector<ll> r,b;
for(auto i:RR)r.push_back(i);
for(auto i:BB)b.push_back(i);
ll n=r.size();
ll m=b.size();
ll dp[n+1][m+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
dp[i][j]=inf;
}
}
dp[0][0]=0;
priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>>pq;
pq.push({0,{0,0}});
while(pq.size()>0)
{
auto it=pq.top();
pq.pop();
int i=it.second.first;
int j=it.second.second;
if(i==n and j==m)break;
if(i==n or j==m)continue;
// cout<<"At "<<i<<' '<<j<<' '<<dp[i][j]<<endl;
ll nc=dp[i][j]+abs(r[i]-b[j]);
if(nc<dp[i+1][j+1])
{
dp[i+1][j+1]=nc;
pq.push({nc,{i+1,j+1}});
}
if(nc<dp[i+1][j])
{
dp[i+1][j]=nc;
pq.push({nc,{i+1,j}});
}
if(nc<dp[i][j+1])
{
dp[i][j+1]=nc;
pq.push({nc,{i,j+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... |