# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
544432 |
2022-04-01T22:16:03 Z |
Olympia |
Sirni (COCI17_sirni) |
C++17 |
|
5000 ms |
655628 KB |
#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);
if (N <= 1e3) {
vector<pair<int, pair<int, int>>> vec;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
vec.emplace_back(min(v[i] % v[j], v[j] % v[i]), make_pair(i, j));
}
}
sort(vec.begin(), vec.end());
int64_t ans = 0;
for (int i = 0; i < vec.size(); i++) {
if (dsu.comp(vec[i].second.first, vec[i].second.second)) {
continue;
}
dsu.join(vec[i].second.first, vec[i].second.second);
ans += vec[i].first;
}
cout << ans;
return 0;
}
//sort(v.begin(), v.end());
int MX = 0;
map<int, int> myMap;
for (int i = 0; i < N; i++) {
MX = max(MX, v[i]);
myMap[v[i]] = i;
}
vector<vector<int>> indices(MX + 1);
for (int i = 0; i < N; i++) {
for (int j = 0; j <= MX; j += v[i]) {
indices[j].push_back(i);
}
}
int64_t tot = 0;
for (int mx = 0; mx < 100; mx++) {
for (int i = 0; i < N; i++) {
if (v[i] - mx < 0) {
continue;
}
for (int ind: indices[v[i] - mx]) {
if (v[i] % v[ind] == mx) {
if (dsu.comp(i, ind)) {
continue;
}
dsu.join(i, ind);
tot += mx;
if (dsu.getConnectedComponents() == 1) {
cout << tot << '\n';
exit(0);
}
}
}
}
}
}
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")
|
sirni.cpp: In function 'int main()':
sirni.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
6596 KB |
Output is correct |
2 |
Correct |
63 ms |
6600 KB |
Output is correct |
3 |
Correct |
49 ms |
6676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
6596 KB |
Output is correct |
2 |
Correct |
69 ms |
6600 KB |
Output is correct |
3 |
Correct |
57 ms |
6676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
6592 KB |
Output is correct |
2 |
Correct |
42 ms |
6600 KB |
Output is correct |
3 |
Correct |
51 ms |
6596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
51176 KB |
Output is correct |
2 |
Correct |
598 ms |
72772 KB |
Output is correct |
3 |
Correct |
430 ms |
70752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
30016 KB |
Output is correct |
2 |
Correct |
3846 ms |
201124 KB |
Output is correct |
3 |
Correct |
243 ms |
37424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
451 ms |
60296 KB |
Output is correct |
2 |
Correct |
765 ms |
90492 KB |
Output is correct |
3 |
Correct |
302 ms |
61068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
13960 KB |
Output is correct |
2 |
Correct |
1031 ms |
100624 KB |
Output is correct |
3 |
Correct |
339 ms |
65780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
463 ms |
282328 KB |
Output is correct |
2 |
Correct |
4344 ms |
642728 KB |
Output is correct |
3 |
Correct |
483 ms |
294680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
294192 KB |
Output is correct |
2 |
Execution timed out |
5095 ms |
655628 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
194 ms |
243076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |