Submission #307565

# Submission time Handle Problem Language Result Execution time Memory
307565 2020-09-28T15:46:33 Z luciocf Sirni (COCI17_sirni) C++14
84 / 140
5000 ms 666268 KB
#include <bits/stdc++.h>
 
#define ff first
#define ss second
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
const int maxn = 1e5+10;
const int maxv = 1e7+1;
 
struct DSU
{
	int pai[maxn], peso[maxn];
 
	void init(int n)
	{
		for (int i = 1; i <= n; i++)
			pai[i] = i, peso[i] = 1;
	}
 
	int Find(int x)
	{
		if (pai[x] == x) return x;
		return pai[x] = Find(pai[x]);
	}
 
	void Join(int x, int y)
	{
		x = Find(x), y = Find(y);
		if (x == y) return;
 
		if (peso[x] < peso[y]) swap(x, y);
 
		pai[y] = x, peso[x] += peso[y];
	}
} dsu;
 
int a[maxn];
int prim[maxv];

pair<int, pii> V[140000000];
 
int main(void)
{
	int n;
	scanf("%d", &n);
 
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
 
	sort(a+1, a+n+1);
 		
 	int ptr = 2;
	for (int i = a[1]; i <= a[n]; i++)
	{
		while (ptr <= n && a[ptr] < i)
			ptr++;
 
		prim[i] = ptr;
	}
 
 	int m = 0;
	dsu.init(n);
 
	int aux = 0;
 
	for (int i = 2; i <= n; i++)
		V[++aux] = {a[i]-a[i-1], {i-1, i}};
 
	for (int i = 1; i <= n; i++)
	{
		int j;
		for (j = i; j <= n && a[i] == a[j]; j++)
			if (j != i)
				dsu.Join(i, j), m++;
 
		i = j-1;
			
		for (int v = 2*a[i]; v <= a[n]; v += a[i])
			V[++aux] = {a[prim[v]]-v, {i, prim[v]}};
	}
 
 	sort(V+1, V+aux+1);
 
	ll ans = 0;
 
	for (int i = 1; i <= aux; i++)
	{
		if (m == n-1) break;
		
		auto pp = V[i];
 
		int u = pp.ss.ff, v = pp.ss.ss, w = pp.ff;
 
		if (dsu.Find(u) != dsu.Find(v))
		{
			ans += 1ll*w;
			dsu.Join(u, v), ++m;
		}
	}
 
	printf("%lld\n", ans);
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
sirni.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |   scanf("%d", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 28 ms 39424 KB Output is correct
2 Correct 446 ms 71928 KB Output is correct
3 Correct 31 ms 39552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Execution timed out 5077 ms 592156 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 39544 KB Output is correct
2 Correct 28 ms 39424 KB Output is correct
3 Correct 30 ms 39552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 17912 KB Output is correct
2 Correct 744 ms 54520 KB Output is correct
3 Correct 371 ms 31352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 6528 KB Output is correct
2 Correct 379 ms 32884 KB Output is correct
3 Correct 192 ms 16248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 415 ms 32760 KB Output is correct
2 Correct 1032 ms 74616 KB Output is correct
3 Correct 341 ms 28156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 6016 KB Output is correct
2 Correct 1060 ms 77688 KB Output is correct
3 Correct 354 ms 29560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 56312 KB Output is correct
2 Execution timed out 5087 ms 516676 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 366 ms 62328 KB Output is correct
2 Execution timed out 5090 ms 666268 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 62 ms 41848 KB Output is correct
2 Execution timed out 5099 ms 508056 KB Time limit exceeded
3 Halted 0 ms 0 KB -