# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
216964 | davitmarg | 전선 연결 (IOI17_wiring) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 100005;
vector<pair<int, int>> a;
vector<pair<LL, pair<int,int>>> e;
pair<int, int> last[2];
int n, p[N];
int get(int v)
{
if (v == p[v])
return v;
return p[v] = get(p[v]);
}
int dsu(int a, int b)
{
a = get(a);
b = get(b);
if (a == b)
return;
p[b] = a;
}
LL min_total_length(vector<int> r, vector<int> b)
{
for (int i = 0; i < r.size(); i++)
a.push_back(MP(r[i], i + 1));
for (int i = 0; i < b.size(); i++)
a.push_back(MP(b[i], r.size() + i + 1));
n = r.size() + b.size();
sort(all(a));
last[0] = last[1] = MP(0,0);
for (int i = 0; i < a.size(); i++)
{
int col = a[i].second<=r.size();
int pos = a[i].first;
last[col] = a[i];
if (last[!col].second)
e.push_back(MP(last[col].first - last[!col].first, MP(last[col].second, last[!col].second)));
}
last[0] = last[1] = MP(0, 0);
reverse(all(a));
for (int i = 0; i < a.size(); i++)
{
int col = a[i].second <= r.size();
int pos = a[i].first;
last[col] = a[i];
if (last[!col].second)
e.push_back(MP(last[col].first - last[!col].first, MP(last[col].second, last[!col].second)));
}
sort(all(e));
for (int i = 1; i <= n; i++)
p[i] = i;
LL ans = 0;
for (int i = 0; i < e.size(); i++)
{
int a = e[i].second.first;
int b = e[i].second.second;
LL d = e[i].first;
if (get(a) != get(b))
{
ans += d;
dsu(a, b);
}
}
return ans;
}
/*
3 3
4
0 0
0 1
0 2
1 1
0 0
2 3
2
0 0
0 1
3 3
4
0 0
0 1
0 2
1 1
3 3
5
0 0
1 0
1 2
2 1
2 2
0 0
*/