# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
891791 |
2023-12-24T04:14:43 Z |
cuman |
Sirni (COCI17_sirni) |
C++14 |
|
1403 ms |
786432 KB |
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (i64 i = (l); i <= (r); i++)
struct DSU {
vector<i64> e;
void initSz(i64 n) {
e = vector<i64>(n, -1);
}
i64 fr(i64 x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
bool unite(i64 x, i64 y) {
x = fr(x), y = fr(y);
if (x==y) return false;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y];
e[y] = x;
return true;
}
};
void sub1(vector<i64> A) {
sort(all(A));
A.erase(unique(all(A)), A.end());
i64 N = A.size();
vector<array<i64,3>> edges;
rep(i, 0, N-1) {
rep(j, i+1, N-1) {
edges.push_back({A[j] % A[i], i, j});
}
}
DSU dsu;
sort(all(edges));
dsu.initSz(N);
long long res = 0;
for (i64 i = 0; i < edges.size(); i++) {
i64 w = edges[i][0];
i64 u = edges[i][1];
i64 v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
void sub2(vector<i64> A) {
sort(all(A));
A.erase(unique(all(A)), A.end());
i64 N = A.size();
vector<array<i64,3>> edges;
i64 MaxA = *max_element(all(A));
for (i64 i = 0; i < N; i++) {
for (i64 mul = A[i]; mul <= MaxA; mul += A[i]) {
i64 j = lower_bound(all(A), mul) - A.begin();
if (j == i) j++;
if (j < N) {
edges.push_back({A[j] % A[i], i, j});
}
}
}
DSU dsu;
sort(all(edges));
dsu.initSz(N);
long long res = 0;
for (i64 i = 0; i < edges.size(); i++) {
i64 w = edges[i][0];
i64 u = edges[i][1];
i64 v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
signed main() {
i64 N; cin >> N;
vector<i64> A(N);
for (i64& i : A) cin >> i;
if (N <= 2e3) {
sub1(A);
}
else {
sub2(A);
}
return 0;
}
Compilation message
sirni.cpp: In function 'void sub1(std::vector<long long int>)':
sirni.cpp:42:23: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (i64 i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2(std::vector<long long int>)':
sirni.cpp:77:23: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (i64 i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
14024 KB |
Output is correct |
2 |
Correct |
50 ms |
13000 KB |
Output is correct |
3 |
Correct |
54 ms |
13764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
14792 KB |
Output is correct |
2 |
Correct |
34 ms |
7884 KB |
Output is correct |
3 |
Correct |
57 ms |
14024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13256 KB |
Output is correct |
2 |
Correct |
43 ms |
14276 KB |
Output is correct |
3 |
Correct |
54 ms |
13768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
53304 KB |
Output is correct |
2 |
Correct |
1027 ms |
101288 KB |
Output is correct |
3 |
Correct |
483 ms |
100980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
8140 KB |
Output is correct |
2 |
Correct |
447 ms |
102072 KB |
Output is correct |
3 |
Correct |
281 ms |
51760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
604 ms |
102396 KB |
Output is correct |
2 |
Correct |
1403 ms |
199416 KB |
Output is correct |
3 |
Correct |
427 ms |
51372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
14524 KB |
Output is correct |
2 |
Correct |
1364 ms |
199060 KB |
Output is correct |
3 |
Correct |
430 ms |
52392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
52504 KB |
Output is correct |
2 |
Runtime error |
840 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
53260 KB |
Output is correct |
2 |
Runtime error |
753 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
8648 KB |
Output is correct |
2 |
Runtime error |
1012 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |