제출 #791844

#제출 시각아이디문제언어결과실행 시간메모리
791844KhizriWiring (IOI17_wiring)C++17
13 / 100
67 ms13188 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
long long min_total_length(vector<int> a, vector<int> b) {
	set<int>st;
	set<int>st2;
	if(a.size()>b.size()) swap(a,b);
	for(int v:b){
        st.insert(v);
	}
	for(int v:a){
        st2.insert(v);
	}
	ll ans=0;
	for(int x:a){
        auto l=st.upper_bound(x-1);
        auto r=st.lower_bound(x);
        if(l!=st.begin()&&r!=st.end()){
            l--;
            int y=*l,z=*r;
            if(abs(y-x)<abs(z-x)){
                ans+=abs(y-x);
                st.erase(l);
            }
            else{
                ans+=abs(z-x);
                st.erase(r);
            }
        }
        else if(l!=st.begin()){
            l--;
            int y=*l;
            ans+=abs(y-x);
            st.erase(l);
        }
        else if(r!=st.end()){
            int y=*r;
            ans+=abs(y-x);
            st.erase(r);
        }
	}
	for(int x:st){
        auto l=st2.upper_bound(x-1);
        auto r=st2.lower_bound(x);
        if(l!=st2.begin()&&r!=st2.end()){
            l--;
            int y=*l,z=*r;
            if(abs(y-x)<abs(z-x)){
                ans+=abs(y-x);
            }
            else{
                ans+=abs(z-x);
            }
        }
        else if(l!=st2.begin()){
            l--;
            int y=*l;
            ans+=abs(y-x);
        }
        else if(r!=st2.end()){
            int y=*r;
            ans+=abs(y-x);
        }
	}
	return ans;
}
#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...