Submission #636448

# Submission time Handle Problem Language Result Execution time Memory
636448 2022-08-29T10:00:05 Z dozer Sirni (COCI17_sirni) C++14
140 / 140
4459 ms 244768 KB
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int , int>
#define st first
#define nd second
#define N 10000005


int root[N], sz[N], arr[N];
vector<int> ind[N];
set<int> roots;

int find(int node)
{
	if (root[node] == node) return node;
	return root[node] = find(root[node]);
}


void uni(int a, int b)
{
	a = find(a), b = find(b);
	if (a == b) return;
	if (sz[a] > sz[b]) swap(a, b);
	roots.erase(a);
	root[a] = b;
	sz[b] += sz[a];
}


int32_t main()
{
	fastio();

	
	int n;
	cin>>n;
	int maks = 0;
	vector<int> v;
	for (int i = 1; i <= n; i++)
	{
		cin>>arr[i];
		if (ind[arr[i]].size() == 0) v.pb(arr[i]);
		ind[arr[i]].pb(i);
		root[i] = i;
		sz[i] = 1;
		roots.insert(i);
		maks = max(maks, arr[i]);
	}

	for (int i = 1; i <= n; i++)
		uni(i, ind[arr[i]].front());
	
	int rem = 0;
	int ans = 0;
	while(roots.size() > 1)
	{
		for (auto curr : v)
		{
			int i = ind[curr].front();
			for (int j = rem; j <= maks; j += curr)
			{
				if (ind[j].size() == 0) continue;
				int k = ind[j].front();
				if (find(i) != find(k)) uni(i, k), ans += rem;
			}
		}
		rem++;
	}

	cout<<ans<<endl;
	cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# Verdict Execution time Memory Grader output
1 Correct 929 ms 235248 KB Output is correct
2 Correct 993 ms 235312 KB Output is correct
3 Correct 703 ms 235256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 235196 KB Output is correct
2 Correct 706 ms 235244 KB Output is correct
3 Correct 237 ms 235260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 955 ms 235340 KB Output is correct
2 Correct 369 ms 235292 KB Output is correct
3 Correct 688 ms 235256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 455 ms 244440 KB Output is correct
2 Correct 602 ms 243764 KB Output is correct
3 Correct 271 ms 243804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 236428 KB Output is correct
2 Correct 412 ms 241924 KB Output is correct
3 Correct 406 ms 243564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 606 ms 244380 KB Output is correct
2 Correct 410 ms 244324 KB Output is correct
3 Correct 277 ms 244296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 238644 KB Output is correct
2 Correct 486 ms 243632 KB Output is correct
3 Correct 260 ms 243920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1442 ms 244548 KB Output is correct
2 Correct 1027 ms 244520 KB Output is correct
3 Correct 1365 ms 244512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 565 ms 244564 KB Output is correct
2 Correct 2427 ms 243944 KB Output is correct
3 Correct 317 ms 244492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1457 ms 237056 KB Output is correct
2 Correct 4459 ms 243592 KB Output is correct
3 Correct 269 ms 244768 KB Output is correct