#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <queue>
#include <stdint.h>
#include <vector>
#include <cassert>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <limits.h>
using namespace std;
class DisjointSetUnion {
protected:
vector<int> parent;
vector<int> compSize;
const int n;
int connectedComponents;
public:
int getConnectedComponents() const {
return connectedComponents;
}
public:
DisjointSetUnion(int sz) : n(sz), connectedComponents(sz) {
parent.resize(sz), compSize.resize(sz);
for (int i = 0; i < n; i++) {
parent[i] = i, compSize[i] = 1;
}
}
int find_head(int x) const {
int cur = x;
while (cur != parent[cur]) {
cur = parent[cur];
}
return cur;
}
void join(int x, int y) {
x = find_head(x);
y = find_head(y);
if (x == y) {
return;
}
if (compSize[x] > compSize[y]) {
swap(x, y);
//ensures that compSize[x1] <= compSize[y1]
}
parent[x] = y;
compSize[y] += compSize[x];
connectedComponents--;
}
bool comp(int x, int y) {
return (find_head(x) == find_head(y));
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<int> v(N);
for (int i = 0; i < N; i++) {
cin >> v[i];
}
sort(v.begin(),v.end()); v.resize(unique(v.begin(),v.end()) - v.begin());
DisjointSetUnion dsu(N);
int MX = 0;
for (int i = 0; i < N; i++) {
MX = max(MX, v[i]);
}
MX++;
int lb[MX + 1]; for (int i = 0; i <= MX; i++) lb[i] = -1;
for (int i = 0; i < N; i++) {
lb[v[i]] = i;
}
for (int i = MX - 1; i >= 1; i--) {
if (lb[i] != -1) {
continue;
}
lb[i] = lb[i + 1];
}
int64_t tot = 0;
vector<pair<int,int>> edges[MX + 1];
for (int i = 0; i < N; i++) {
if (i != N - 1) {
edges[v[i + 1] % v[i]].emplace_back(i, i + 1);
}
for (int mx = v[i]; mx < MX; mx += v[i]) {
if (lb[mx] == -1) continue;
edges[v[lb[mx]] % v[i]].emplace_back(lb[mx], i);
}
}
for (int i = 0; i <= MX; i++) {
for (auto& e: edges[i]) {
if (dsu.comp(e.first, e.second)) {
continue;
}
dsu.join(e.first, e.second);
tot += i;
}
}
cout << tot;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
274164 KB |
Output is correct |
2 |
Correct |
313 ms |
302760 KB |
Output is correct |
3 |
Correct |
195 ms |
273484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1604 ms |
690196 KB |
Output is correct |
3 |
Correct |
198 ms |
274992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
274404 KB |
Output is correct |
2 |
Correct |
199 ms |
273804 KB |
Output is correct |
3 |
Correct |
193 ms |
274424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
39040 KB |
Output is correct |
2 |
Correct |
191 ms |
67416 KB |
Output is correct |
3 |
Correct |
127 ms |
52164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
30036 KB |
Output is correct |
2 |
Correct |
1014 ms |
263192 KB |
Output is correct |
3 |
Correct |
65 ms |
25548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
51932 KB |
Output is correct |
2 |
Correct |
243 ms |
84956 KB |
Output is correct |
3 |
Correct |
102 ms |
48316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
9812 KB |
Output is correct |
2 |
Correct |
278 ms |
87448 KB |
Output is correct |
3 |
Correct |
112 ms |
50080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
289084 KB |
Output is correct |
2 |
Correct |
1812 ms |
634252 KB |
Output is correct |
3 |
Correct |
367 ms |
291744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
293612 KB |
Output is correct |
2 |
Correct |
3697 ms |
748428 KB |
Output is correct |
3 |
Correct |
513 ms |
349324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
276976 KB |
Output is correct |
2 |
Correct |
3984 ms |
648848 KB |
Output is correct |
3 |
Correct |
117 ms |
54032 KB |
Output is correct |