#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
inline void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
/* online submission */
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
/**************************COCI 2016-2017 R6 P4*************************/
template <int SZ> struct UF {
int N; // Vertices 0...N-1
int par[SZ], compsz[SZ]; // parent of i, size of component
UF() {
FOR(i, 0, SZ) par[i] = i, compsz[i] = 1;
}
int find(int x) { // path compression
if (par[x] != x) par[x] = find(par[x]);
return par[x];
}
bool unite(int x, int y) { // unite by rank. return TRUE if united. FALSE if already same component
x = find(x), y = find(y);
if (x == y) return false;
if (compsz[x] < compsz[y]) swap(x, y); // now x is the larger component
compsz[x] += compsz[y], par[y] = x;
return true;
}
};
const int MAXP = 9.5*1000000;
int N, M;
v<int> P;
int nxt[MAXP];
v<pii> edges;
v<pii> edgeSorted[MAXP];
UF<100000> uf;
int main() {
io();
// input
cin >> N;
P.resize(N);
FOR(i, 0, N) cin >> P[i];
sort(P.begin(), P.end());
P.erase(unique(P.begin(), P.end()), P.end());
N = P.size();
M = P.back();
// assert(M < MAXP);
// compute nxt
for (int i = M, j = P.size()-1; i >= P.front(); i--) {
if (j < 0) {
cout << "WTF\n";
return 0;
}
if (j>=0 && i == P[j])
nxt[i] = j--;
else
nxt[i] = nxt[i + 1];
}
// compute edges
FOR(i, 0, N) {
edges.push_back({ i, nxt[P[i] + 1] });
for (int k = P[i] + P[i]; k <= M; k += P[i]) {
if (k > M) {
cout << "WTF\n";
return 0;
}
edges.push_back({ i, nxt[k] });
}
}
// init count sort
for (auto &e : edges) {
if (P[e.second] % P[e.first] > M) {
cout << "WTF\n";
return 0;
}
edgeSorted[P[e.second] % P[e.first]].push_back(e);
}
// mst -Run time error somwhere here
int num = 0;
ll mstcost = 0;
FORE(c, 0, M) {
if (c > MAXP) {
cout << "WTF\n";
return 0;
}
for (pii e : edgeSorted[c]) {
if (uf.unite(e.first, e.second)) {
mstcost += (ll)(c);
num++;
}
if (num == N - 1)goto oof;
}
}
oof:
cout << mstcost << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
433 ms |
448356 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
448540 KB |
Output is correct |
2 |
Runtime error |
511 ms |
448540 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
483 ms |
448560 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
471780 KB |
Output is correct |
2 |
Correct |
399 ms |
527160 KB |
Output is correct |
3 |
Correct |
366 ms |
527160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
527160 KB |
Output is correct |
2 |
Correct |
416 ms |
527160 KB |
Output is correct |
3 |
Correct |
310 ms |
527160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
527160 KB |
Output is correct |
2 |
Correct |
482 ms |
554952 KB |
Output is correct |
3 |
Correct |
320 ms |
554952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
554952 KB |
Output is correct |
2 |
Correct |
503 ms |
558104 KB |
Output is correct |
3 |
Correct |
359 ms |
558104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
508 ms |
558104 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
493 ms |
558104 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
521 ms |
558104 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |