#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));
}
};
vector<int> v;
int get (pair<int,int> p1) {
return min(v[p1.first] % v[p1.second], v[p1.second] % v[p1.first]);
}
bool comp (pair<int,int> p1, pair<int,int> p2) {
return get(p1) < get(p2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
v.resize(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;
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;
for (int i = 0; i < N; i++) {
if (i != N - 1) {
if (get({i, i + 1}) <= 10000) {
edges.emplace_back(i, i + 1);
}
}
for (int mx = 1; mx * v[i] < MX; mx++) {
if (lb[mx * v[i]] == -1) {
continue;
}
if (get({lb[mx * v[i]], i}) <= 10000) {
edges.emplace_back(lb[mx * v[i]], i);
}
}
}
sort(edges.begin(), edges.end(), comp);
for (auto &e: edges) {
if (dsu.comp(e.first, e.second)) {
continue;
}
dsu.join(e.first, e.second);
tot += min(v[e.first] % v[e.second], v[e.second] % v[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 |
Incorrect |
57 ms |
39472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
596 KB |
Output is correct |
2 |
Runtime error |
1564 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
39496 KB |
Output is correct |
2 |
Correct |
53 ms |
39404 KB |
Output is correct |
3 |
Correct |
49 ms |
39632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
637 ms |
26344 KB |
Output is correct |
2 |
Correct |
2573 ms |
75124 KB |
Output is correct |
3 |
Correct |
1985 ms |
41924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
7248 KB |
Output is correct |
2 |
Execution timed out |
5073 ms |
268552 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1345 ms |
42716 KB |
Output is correct |
2 |
Correct |
3625 ms |
75496 KB |
Output is correct |
3 |
Correct |
1076 ms |
26328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
7356 KB |
Output is correct |
2 |
Correct |
4957 ms |
140296 KB |
Output is correct |
3 |
Correct |
1361 ms |
42272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
852 ms |
61780 KB |
Output is correct |
2 |
Execution timed out |
5112 ms |
570648 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1127 ms |
61836 KB |
Output is correct |
2 |
Execution timed out |
5118 ms |
570024 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
42808 KB |
Output is correct |
2 |
Execution timed out |
5118 ms |
569364 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |