제출 #668490

#제출 시각아이디문제언어결과실행 시간메모리
668490DoxenoSirni (COCI17_sirni)C++17
98 / 140
5110 ms399704 KiB
#include <algorithm> #include <bits/stdc++.h> #include <functional> #include <numeric> using namespace std; vector<int> dad, sz; inline int find(int a){ if(dad[a] != a)dad[a] = find(dad[a]); return dad[a]; } bool uni (int a, int b){ a = find(a); b = find(b); if(a == b)return 0; if(sz[a] < sz[b])swap(a,b); dad[b] = a; sz[a] += sz[b]; return 1; } int main(){ int N; cin >> N; set<int> s; int a; for(int i = 0; i < N; ++i){ cin >> a; s.insert(a); } N = s.size(); vector<int> v; for(auto x: s)v.emplace_back(x); long long ans = 0; vector<array<int,3>> eg; for(int i = 0; i < N; ++i){ for(int j = v[i]; j <= v.back(); j += v[i]){ int k = lower_bound(v.begin() + i + 1, v.end(), j) -v.begin(); if(k >= N || v[k] - j >= v[i])continue; eg.push_back({v[k] - j, k, i}); } } dad.resize(N); sz.assign(N, 1); iota(dad.begin(), dad.end(), 0); sort(eg.begin(), eg.end()); for(auto [w, i, j]: eg){ if(uni(i, j))ans += w; } cout << ans << endl; return 0; }
#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...
#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...