Submission #1061051

#TimeUsernameProblemLanguageResultExecution timeMemory
1061051Faisal_SaqibWiring (IOI17_wiring)C++17
20 / 100
1088 ms52088 KiB
#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();
    if(r.back()<b[0])
    {
        ll sm=0;
        int i=n-1;
        int j=0;
        while(0<=i and j<m)
        {
            // cout<<"At "<<i<<' '<<j<<endl;
            ll mp=b[j]-r[i];
            ll oth=b[j]-r[n-1] + b[0]-r[i];
            sm+=min(mp,oth);
            // break;
            j++;
            i--;
        }
        while(0<=i)
        {
            sm+=(b[0]-r[i]);
            i--;
        }
        while(j<m)
        {
            sm+=(b[j]-r[n-1]);
            j++;
        }
        return sm;
    }
    else{
        map<pair<ll,ll>,ll> dp;
        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(dp.find({i+1,j+1})==dp.end() or nc<dp[{i+1,j+1}])
            {
                dp[{i+1,j+1}]=nc;
                pq.push({nc,{i+1,j+1}});
            }
            if(dp.find({i+1,j})==dp.end() or nc<dp[{i+1,j}])
            {
                dp[{i+1,j}]=nc;
                pq.push({nc,{i+1,j}});
            }
            if(dp.find({i,j+1})==dp.end() or nc<dp[{i,j+1}])
            {
                dp[{i,j+1}]=nc;
                pq.push({nc,{i,j+1}});
            }
        }
        return dp[{n,m}];
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...