이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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++)
const i64 MaxN = 1e5 + 2;
const i64 MaxA = 1e6 + 5;
i64 N, A[MaxN];
struct DSU {
i64 e[MaxN];
void initSz(i64 n) {
memset(e, -1, n * sizeof(i64));
}
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;
}
};
DSU dsu;
void sub1() {
sort(A, A + N);
N = unique(A, A + N) - A;
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});
}
}
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() {
sort(A, A + N);
N = unique(A, A + N) - A;
vector<array<i64,3>> edges;
for (i64 i = 0; i < N; i++) {
for (i64 mul = A[i]; mul <= MaxA; mul += A[i]) {
i64 j = lower_bound(A, A + N, mul) - A;
if (j < N && A[j] == A[i]) j++;
if (j < N) {
edges.push_back({A[j] % A[i], i, j});
}
}
}
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() {
cin >> N;
for (i64 i = 0; i < N; i++) {
cin >> A[i];
}
if (N <= 2e3) {
sub1();
}
else {
sub2();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sirni.cpp: In function 'void sub1()':
sirni.cpp:45: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]
45 | for (i64 i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2()':
sirni.cpp:75: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]
75 | for (i64 i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |