# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
46362 |
2018-04-19T18:37:21 Z |
evpipis |
Sirni (COCI17_sirni) |
C++11 |
|
5000 ms |
95312 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef pair<int, int> ii;
typedef long long ll;
const int len = 1e5+2, inf = 1e9;
int cnt[100*len], nex[100*len], p[len], r[len];
int u[100*len], v[100*len], edcnt = 0;
int cnt_sz[100*len];
vector<int> *vec[100*len];
int fin(int i){
if (p[i] == i) return i;
return p[i] = fin(p[i]);
}
void join(int i, int j){
if (r[i] > r[j])
p[j] = i;
else{
p[i] = j;
if (r[i] == r[j]) r[j]++;
}
}
void read(int &res){
char c;
while (c = getchar(), c < '0' || c > '9'){}
res = c-'0';
while (c = getchar(), c >= '0' && c <= '9')
res = 10*res+c-'0';
}
int main(){
int n, m = 0, k = 0;
read(n);
for (int i = 0; i < n; i++){
int temp;
read(temp);
m = max(m, temp);
if (!cnt[temp]) cnt[temp] = ++k;
}
nex[m+1] = inf;
for (int i = m; i >= 1; i--){
nex[i] = nex[i+1];
if (cnt[i+1]) nex[i] = i+1;
}
for (int i = 1; i <= k; i++){
r[i] = 0;
p[i] = i;
}
for (int i = 1; i <= m; i++) {
if (cnt[i]) {
if (nex[i] < 2*i){
cnt_sz[nex[i]-i]++;
}
for (int j = 2*i; j <= m; j+=i){
if (cnt[j]){
cnt_sz[0]++;
}
else if (nex[j] < j+i){
cnt_sz[nex[j]-j]++;
}
}
}
}
for (int i = 0; i <= m; i++){
if (cnt_sz[i]) {
vec[i] = new vector<int>();
vec[i]->reserve(cnt_sz[i]);
}
}
vec[0] = new vector<int>();
for (int i = 1; i <= m; i++)
if (cnt[i]){
if (nex[i] < 2*i){
u[edcnt] = cnt[i];
v[edcnt] = cnt[nex[i]];
vec[nex[i]-i]->pb(edcnt);
edcnt++;
//vec[nex[j][1]-j].pb(mp(cnt[i], cnt[nex[j][1]]));
}
for (int j = i; j <= m; j+=i){
if (cnt[j]){
u[edcnt] = cnt[i];
v[edcnt] = cnt[j];
vec[0]->pb(edcnt);
edcnt++;
}
else if (nex[j] < j+i){
u[edcnt] = cnt[i];
v[edcnt] = cnt[nex[j]];
vec[nex[j]-j]->pb(edcnt);
edcnt++;
//vec[nex[j][0]-j].pb(mp(cnt[i], cnt[nex[j][0]]));
}
}
}
ll ans = 0;
for (int i = 0; i <= m; i++){
if (k == 1) break;
if (vec[i] == NULL) continue;
for (auto it : *vec[i]){
if (k == 1) break;
int x = fin(u[it]), y = fin(v[it]);
if (x != y)
join(x, y), ans += i, k--;
}
}
printf("%lld\n", ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
44412 KB |
Output is correct |
2 |
Correct |
274 ms |
45136 KB |
Output is correct |
3 |
Correct |
98 ms |
45136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
45136 KB |
Output is correct |
2 |
Correct |
1050 ms |
45136 KB |
Output is correct |
3 |
Correct |
106 ms |
45136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
45136 KB |
Output is correct |
2 |
Correct |
93 ms |
45136 KB |
Output is correct |
3 |
Correct |
99 ms |
45136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
45136 KB |
Output is correct |
2 |
Correct |
350 ms |
55768 KB |
Output is correct |
3 |
Correct |
129 ms |
55768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
55768 KB |
Output is correct |
2 |
Correct |
424 ms |
55768 KB |
Output is correct |
3 |
Correct |
66 ms |
55768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
55768 KB |
Output is correct |
2 |
Correct |
468 ms |
68700 KB |
Output is correct |
3 |
Correct |
127 ms |
68700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
68700 KB |
Output is correct |
2 |
Correct |
368 ms |
68700 KB |
Output is correct |
3 |
Correct |
101 ms |
68700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
893 ms |
95312 KB |
Output is correct |
2 |
Execution timed out |
5020 ms |
95312 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
895 ms |
95312 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
95312 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
250 ms |
95312 KB |
Output is correct |
2 |
Execution timed out |
5016 ms |
95312 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |