답안 #307567

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307567 2020-09-28T15:53:47 Z luciocf Sirni (COCI17_sirni) C++14
112 / 140
5000 ms 413420 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];
 
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 tot = 0;
	for (int i = 1; i <= n; i++)
	{
		if (a[i] == a[i+1]) continue;
		
		tot += (a[n]/a[i]);
	}

 	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);
 
	vector<pair<int, pii>> V;
	V.reserve(tot+1);
 
	for (int i = 2; i <= n; i++)
		V.push_back({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])
		{
			j = v;
			for (; j <= a[n] && prim[j] == prim[v]; j += a[i])
				;
			j -= a[i];

			v = j;
			V.push_back({a[prim[v]]-v, {i, prim[v]}});
		}
	}
 
 	sort(V.begin(), V.end());
 
	ll ans = 0;
 
	for (auto pp: V)
	{
		if (m == n-1) break;
 
		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:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   47 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
sirni.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |   scanf("%d", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 39292 KB Output is correct
2 Correct 94 ms 41592 KB Output is correct
3 Correct 37 ms 39548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 288 ms 40184 KB Output is correct
3 Correct 35 ms 39544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 39416 KB Output is correct
2 Correct 33 ms 39384 KB Output is correct
3 Correct 35 ms 39544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 17400 KB Output is correct
2 Correct 709 ms 50828 KB Output is correct
3 Correct 277 ms 23032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 6136 KB Output is correct
2 Correct 378 ms 31376 KB Output is correct
3 Correct 197 ms 15864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 32504 KB Output is correct
2 Correct 907 ms 63608 KB Output is correct
3 Correct 258 ms 21496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 5752 KB Output is correct
2 Correct 856 ms 61176 KB Output is correct
3 Correct 243 ms 20728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 297 ms 54904 KB Output is correct
2 Correct 4823 ms 308600 KB Output is correct
3 Correct 345 ms 58796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 54776 KB Output is correct
2 Execution timed out 5062 ms 413420 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 41848 KB Output is correct
2 Execution timed out 5068 ms 411800 KB Time limit exceeded
3 Halted 0 ms 0 KB -