#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,pair<int,int>>> edges;
for (int i = 0; i < N; i++) {
if (i != N - 1) {
edges.push_back({v[i + 1] % v[i], {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.push_back({v[ind] % v[i], {ind, i}});
}
}
sort(edges.begin(), edges.end());
for (auto& e: edges) {
if (dsu.comp(e.second.first, e.second.second)) continue;
dsu.join(e.second.first, e.second.second);
tot += e.first;
}
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 |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
374 ms |
49684 KB |
Output is correct |
3 |
Correct |
5 ms |
916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Runtime error |
1298 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
854 ms |
35176 KB |
Output is correct |
2 |
Correct |
2758 ms |
108264 KB |
Output is correct |
3 |
Correct |
1562 ms |
58052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
4988 KB |
Output is correct |
2 |
Execution timed out |
5035 ms |
396620 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1874 ms |
59812 KB |
Output is correct |
2 |
Correct |
4280 ms |
108692 KB |
Output is correct |
3 |
Correct |
1024 ms |
34980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
10296 KB |
Output is correct |
2 |
Correct |
3928 ms |
205448 KB |
Output is correct |
3 |
Correct |
1241 ms |
58572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
885 ms |
35488 KB |
Output is correct |
2 |
Execution timed out |
5107 ms |
405060 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1018 ms |
35528 KB |
Output is correct |
2 |
Execution timed out |
5103 ms |
441468 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
5544 KB |
Output is correct |
2 |
Execution timed out |
5108 ms |
403008 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |