제출 #313099

#제출 시각아이디문제언어결과실행 시간메모리
313099tengiz05전선 연결 (IOI17_wiring)C++17
0 / 100
1 ms256 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
long long min_total_length(vector<int> r, vector<int> b) {
	vector<pair<int, bool>> a;
	for(auto x : r)a.push_back({x, 0});
	for(auto x : b)a.push_back({x, 1});
	sort(a.begin(), a.end());
	vector<long long> v;
	for(int i=0;i<(int)a.size(); i++){
		int cnt = 1;
		while(i+1 < (int)a.size() && a[i+1].second == a[i].second)cnt++,i++;
		v.push_back(cnt);
	}
	int n = v.size();
	long long ans = 0;
	long long f, s;
	long long last=0;
	for(int i=1;i<n;i++){
		f = v[i-1], s = v[i];
		last = s;
		if(f==0)continue;
		long long mn = f==s?f:(s+1)/2;
		ans += mn*mn;
		f -= mn;s -= mn;
		ans += f;
		for(;f>0;f--)ans += f+mn;
		v[i] = s;
	}for(;v[n-1]>0;v[n-1]--)ans += last-v[n-1]+1ll;
	return ans;
}
/*

5 4
1 5 6 8 9
2 3 4 7

6 4
1 2 6 7 8 9
3 4 5 10

*/
#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...