답안 #307574

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307574 2020-09-28T16:16:06 Z luciocf Sirni (COCI17_sirni) C++14
140 / 140
2454 ms 574664 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 fq[maxn];

vector<pii> V[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);

	for (int i = 2; i <= n; i++)
		V[a[i]-a[i-1]].push_back({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[a[prim[v]]-v].push_back({i, prim[v]});
		}
	}
 
	ll ans = 0;
 
	for (int w = 0; w < maxv; w++)
	{
		if (m == n-1) break;

		for (auto pp: V[w])
		{
			int u = pp.ff, v = pp.ss;
	 
			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:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
sirni.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |   scanf("%d", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 274296 KB Output is correct
2 Correct 224 ms 276540 KB Output is correct
3 Correct 182 ms 274424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 235256 KB Output is correct
2 Correct 439 ms 275064 KB Output is correct
3 Correct 180 ms 274552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 274468 KB Output is correct
2 Correct 188 ms 274296 KB Output is correct
3 Correct 180 ms 274528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 222 ms 249780 KB Output is correct
2 Correct 324 ms 276232 KB Output is correct
3 Correct 227 ms 254740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 240888 KB Output is correct
2 Correct 287 ms 262892 KB Output is correct
3 Correct 212 ms 248764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 276 ms 261756 KB Output is correct
2 Correct 353 ms 290860 KB Output is correct
3 Correct 221 ms 252856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 239612 KB Output is correct
2 Correct 350 ms 285732 KB Output is correct
3 Correct 226 ms 253040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 287352 KB Output is correct
2 Correct 1277 ms 483392 KB Output is correct
3 Correct 301 ms 289780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 287364 KB Output is correct
2 Correct 2316 ms 574664 KB Output is correct
3 Correct 322 ms 294260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 276712 KB Output is correct
2 Correct 2454 ms 570340 KB Output is correct
3 Correct 227 ms 255196 KB Output is correct