#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>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
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); set<int> s;
for (int i = 0; i < N; i++) {
cin >> v[i]; s.insert(v[i]);
}
v.clear();
for (int i: s) v.push_back(i);
DisjointSetUnion dsu(N);
int MX = 0; map<int,int> myMap;
for (int i = 0; i < N; i++) {
MX = max(MX, v[i]); myMap[v[i]] = i;
}
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]].push_back({i, i + 1});
}
for (int mx = 1; mx * v[i] <= MX; mx++) {
auto it = s.lower_bound(mx * v[i]);
if (it == s.end()) continue;
int ind = myMap[*it];
edges[v[ind] % v[i]].push_back({ind, 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;
}
Compilation message
sirni.cpp:25: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
25 | #pragma GCC optimization ("O3")
|
sirni.cpp:26: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
26 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
235084 KB |
Output is correct |
2 |
Correct |
354 ms |
269116 KB |
Output is correct |
3 |
Correct |
173 ms |
234664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Runtime error |
2585 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
235204 KB |
Output is correct |
2 |
Correct |
162 ms |
234768 KB |
Output is correct |
3 |
Correct |
183 ms |
235412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
767 ms |
44548 KB |
Output is correct |
2 |
Correct |
2359 ms |
74156 KB |
Output is correct |
3 |
Correct |
1229 ms |
70392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
27424 KB |
Output is correct |
2 |
Execution timed out |
5110 ms |
294836 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1880 ms |
56968 KB |
Output is correct |
2 |
Correct |
3693 ms |
97908 KB |
Output is correct |
3 |
Correct |
933 ms |
53312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
13360 KB |
Output is correct |
2 |
Correct |
3154 ms |
122064 KB |
Output is correct |
3 |
Correct |
1022 ms |
59216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
953 ms |
259476 KB |
Output is correct |
2 |
Execution timed out |
5109 ms |
503312 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
971 ms |
263844 KB |
Output is correct |
2 |
Execution timed out |
5114 ms |
507672 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
239708 KB |
Output is correct |
2 |
Execution timed out |
5121 ms |
448156 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |