#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse4")
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int32_t> p, r;
DSU(int32_t n) {
p.assign(n,0);
r.assign(n,1);
for (int i=0;i<n;i++)p[i]=i;
}
int get(int32_t i) {
if (i != p[i]) p[i]=get(p[i]);
return p[i];
}
bool unite(int32_t a,int32_t b) {
a=get(a);
b=get(b);
if (a==b)return false;
if (r[a]<r[b])swap(a,b);
p[b]=a;
r[a]+=r[b];
return true;
}
};
signed test(int n, vector<int> &b) {
int lr=b.back();
n=b.size();
vector<int32_t> clos(lr+1,-1);
for (int i=0;i<n;i++) {
clos[b[i]]=i;
}
for (int32_t i=lr-1;i>=0;i--) {
if (clos[i] == -1) {
clos[i]=clos[i+1];
}
}
// cout<<"here\n";
typedef array<int32_t,2> pii;
vector<vector<pii>> edg(lr+1);
for (int i=0;i<n-1;i++) {
int x=b[i];
// cout<<x<<"\n";
// cout<<clos[x+1]<<"\n";
// cout<<"PRINT\n";
vector<int> tmp;
edg[(b[clos[x+1]]%x)].push_back({i, clos[x+1]});
tmp.push_back(clos[x+1]);
// cout<<"after\n";
for (int32_t j=2*x;j<=b.back();j+=x) {
// cout<<j<<" "<<clos[j]<<"\n";
if (clos[j] != -1) {
if (tmp.back() != clos[j]) {
edg[(b[clos[j]]%x)].push_back({clos[j], i});
tmp.push_back(clos[j]);
}
}
}
if (tmp.size() == 0 || tmp.back() != n-1) {
edg[(b.back()%x)].push_back({n-1, i});
}
}
// cout<<"here\n";
DSU d(b.size());
int cost=0;
int steps=0;
bool term=false;
for (int c=0;c<=b.back();c++) {
for (auto &x:edg[c]) {
if (d.unite(x[0], x[1])) {
cost+=c;
steps++;
if (steps == n-1) {
term=true;
break;
}
}
}
if (term)break;
}
return cost;
}
bool brute() {
int len=(rand() % 10)+10;
vector<int> b;
set<int> chose;
for (int i=0;i<len;i++) {
int j=(rand()%100)+1;
while (chose.find(j) != chose.end()) {
j=(rand()%100)+1;
}
chose.insert(j);
b.push_back(j);
}
cout<<"gen numbers\n";
sort(b.begin(),b.end());
typedef array<int,3> pii;
vector<pii> g;
for (int i=0;i<len;i++) {
for (int j=i+1;j<len;j++) {
g.push_back({b[j] % b[i], j, i});
}
}
sort(g.begin(),g.end());
DSU d(len);
int cost=0;
for (auto x:g) {
if (d.unite(x[1], x[2])) {
// cout<<b[x[1]]<<" "<<b[x[2]]<<"\n";
cost += x[0];
}
}
cout<<"mst calc\n";
// cout<<"\n";
int cost2 = test(len, b);
cout<<"mst fast\n";
// cout<<"\n";
if (cost != cost2) {
cout<<cost<<" "<<cost2<<"\n";
}
// for (int x:b) {
// cout<<x<<" ";
// }
// cout<<"\n";
cout<<"finished\n";
cout.flush();
return true;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<int> a(n);
for (int &x:a)cin>>x;
if (n == 1) {
cout<<0<<"\n";
return 0;
}
sort(a.begin(),a.end());
vector<int> b;
for (int x:a) {
if (b.size()==0||b.back()!=x)b.push_back(x);
}
cout<<test(n,b)<<"\n";
// srand(time(NULL));
// brute();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
274340 KB |
Output is correct |
2 |
Correct |
103 ms |
275884 KB |
Output is correct |
3 |
Correct |
77 ms |
273628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
286 ms |
274556 KB |
Output is correct |
3 |
Correct |
78 ms |
274348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
274348 KB |
Output is correct |
2 |
Correct |
76 ms |
273744 KB |
Output is correct |
3 |
Correct |
77 ms |
274584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
41152 KB |
Output is correct |
2 |
Correct |
110 ms |
70024 KB |
Output is correct |
3 |
Correct |
58 ms |
45076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30040 KB |
Output is correct |
2 |
Correct |
73 ms |
50400 KB |
Output is correct |
3 |
Correct |
46 ms |
26808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
52376 KB |
Output is correct |
2 |
Correct |
117 ms |
84128 KB |
Output is correct |
3 |
Correct |
58 ms |
44420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
10488 KB |
Output is correct |
2 |
Correct |
109 ms |
75104 KB |
Output is correct |
3 |
Correct |
69 ms |
43532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
291000 KB |
Output is correct |
2 |
Correct |
783 ms |
487160 KB |
Output is correct |
3 |
Correct |
168 ms |
293408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
291124 KB |
Output is correct |
2 |
Correct |
1336 ms |
577476 KB |
Output is correct |
3 |
Correct |
179 ms |
296660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
277660 KB |
Output is correct |
2 |
Correct |
1350 ms |
570372 KB |
Output is correct |
3 |
Correct |
62 ms |
45064 KB |
Output is correct |