Submission #584700

#TimeUsernameProblemLanguageResultExecution timeMemory
5847001neWiring (IOI17_wiring)C++14
0 / 100
1 ms212 KiB
#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;
struct node{
	long long u,v,cost;	
};
long long min_total_length(std::vector<int> r, std::vector<int> b) {
	vector<pair<long long,long long>>arr;
	for (auto x:r){
		arr.push_back({x,0});
	}
	for (auto x:b){
		arr.push_back({x,1});
	}
	map<long long,long long>minny;
	vector<node>edge;
	long long rr = -1,bb = -1;
	sort(arr.begin(),arr.end());
	for (auto x:arr){
		if (x.second == 0){
			if (bb!=-1){
				edge.push_back({x.first,bb,x.first - bb});
				minny[x.first] = x.first - bb;
			}
			rr = x.first;
		}
		else{
			if (rr!=-1){
				edge.push_back({x.first,rr,x.first - rr});
				minny[x.first] = x.first - rr;
			}
			bb = x.first;
		}
	}
	sort(arr.rbegin(),arr.rend());
	rr = -1,bb =-1;
	for (auto x:arr){
		if (x.second == 0){
			if (bb!=-1){
				edge.push_back({x.first,bb,bb - x.first});
				minny[x.first] = min(minny[x.first],bb - x.first);
			}
			rr = x.first;
		}
		else{
			if (rr!=-1){
				edge.push_back({x.first,rr,rr - x.first});
				minny[x.first] = min(minny[x.first],rr - x.first);
			}
			bb = x.first;
		}
	}
	sort(edge.begin(),edge.end(),[&](auto x,auto y){
		return x.cost < y.cost;
	});
	map<long long,bool>mp;
	int64_t ans = 0;
	for (auto x:edge){
		if (!mp[x.u] && !mp[x.v]){
			mp[x.u] = true;
			mp[x.v] = true;
			ans+=x.cost;
			//cout<<x.u<<" "<<x.v<<" "<<x.cost<<'\n';
		}
	}
	set<int>brr;
	for (auto x:b){
		if (!mp[x]){
			brr.insert(x);
		}
	}
	const long long maxn = 1e15;
	for (auto x:r){
		if (!mp[x]){
			int64_t dist = maxn;
			int64_t index = maxn;
			for (auto y:brr){
				if (y - x<dist){
					index = y;
					dist = y - x;
				}
			}
			if (index ==maxn){
				ans+=minny[x];
				//cout<<x<<" "<<minny[x]<<'\n';
			}
			else if (minny[x] + minny[index] < dist){
				ans+=minny[x];
				//cout<<x<<" "<<minny[x]<<'\n';
			}
			else{
			//	cout<<x<<" "<<index<<" "<<dist<<'\n';
				ans+=dist;
				brr.erase(index);
			}
		}
	}
	for (auto x:brr){
		if (!mp[x]){
			//cout<<x<<" "<<minny[x]<<'\n';
			ans+=minny[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...