Submission #24253

# Submission time Handle Problem Language Result Execution time Memory
24253 2017-06-03T10:39:02 Z Bruteforceman Sirni (COCI17_sirni) C++11
84 / 140
1519 ms 786432 KB
#include "bits/stdc++.h"
using namespace std;

struct edge
{
	int u, v, cost;
	edge () {}
	edge (int u, int v, int cost) : u(u), v(v), cost(cost) {}
	bool operator < (edge e) const {
		return cost < e.cost;
	}
};

vector <int> node;
vector <edge> e;
const int maxn = 10000000;
int nxt[maxn + 5];

int par[100010];
int sub[100010];

int root(int x) {
	if(x == par[x]) return x;
	else return par[x] = root(par[x]);
}
void join(int x, int y) {
	x = root(x);
	y = root(y);
	if(sub[x] > sub[y]) swap(x, y);
	if(x != y) {
		par[x] = y;
		sub[y] += sub[x];
	}
}

int main(int argc, char const *argv[])
{
	set <int> s;
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		par[i] = i;
		sub[i] = 1;

		int p;
		scanf("%d", &p);
		s.insert(p);
	}
	node = vector <int> (s.begin(), s.end());

	memset(nxt, -1, sizeof nxt);
	nxt[maxn + 1] = n;

	for(size_t i = 0; i < node.size(); i++) {
		nxt[node[i]] = i;
	}
	for(int i = maxn; i >= 0; i--) {
		if(nxt[i] == -1){
			nxt[i] = nxt[i + 1];
		}
	}

	for(size_t i = 0; i < node.size(); i++) {
		for(int j = 0; j <= maxn; j += node[i]) {
			if(nxt[j] < (int) node.size()) {
				e.push_back(edge(i, nxt[j], node[nxt[j]] - j));
			}
		}
		if(nxt[node[i] + 1] < (int) node.size()) {
			e.push_back(edge(i, nxt[node[i] + 1], node[nxt[node[i] + 1]] - node[i]));
		}
	}
	sort(e.begin(), e.end());

	long long ans = 0;
	for(size_t i = 0; i < e.size(); i++) {
		int u = e[i].u;
		int v = e[i].v;
		int cost = e[i].cost;
		if(root(u) != root(v)) {
			join(u, v);
			ans += cost;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

sirni.cpp: In function 'int main(int, const char**)':
sirni.cpp:40:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
sirni.cpp:46:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &p);
                  ^
# Verdict Execution time Memory Grader output
1 Correct 26 ms 42184 KB Output is correct
2 Correct 429 ms 115724 KB Output is correct
3 Correct 26 ms 42580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 42380 KB Output is correct
2 Memory limit exceeded 603 ms 786432 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 42188 KB Output is correct
2 Correct 23 ms 42000 KB Output is correct
3 Correct 33 ms 42384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 439 ms 83656 KB Output is correct
2 Correct 1173 ms 193864 KB Output is correct
3 Correct 556 ms 119592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 47276 KB Output is correct
2 Correct 756 ms 116140 KB Output is correct
3 Correct 519 ms 82804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 743 ms 120484 KB Output is correct
2 Correct 1379 ms 194068 KB Output is correct
3 Correct 526 ms 120404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 349 ms 52932 KB Output is correct
2 Correct 1519 ms 193148 KB Output is correct
3 Correct 496 ms 119900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 336 ms 83872 KB Output is correct
2 Memory limit exceeded 959 ms 786432 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 343 ms 83872 KB Output is correct
2 Memory limit exceeded 1036 ms 786432 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 69 ms 47576 KB Output is correct
2 Memory limit exceeded 1393 ms 786432 KB Memory limit exceeded
3 Halted 0 ms 0 KB -