Submission #633630

# Submission time Handle Problem Language Result Execution time Memory
633630 2022-08-22T21:50:44 Z inksamurai Sirni (COCI17_sirni) C++17
140 / 140
3849 ms 612660 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3PGDklf ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

struct dsu{
	int _n;
	vi si,par,leb;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		leb=vi(_n,-1);
		rep(i,n){
			si[i]=1;
			par[i]=i;
		}	
	}
	int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
	bool same(int u,int v){return parent(u)==parent(v);}
	void merge(int u,int v){
		u=parent(u),v=parent(v);
		if(!same(u,v)){
			if(si[u]<si[v]) swap(u,v);
			leb[u]=max(leb[u],leb[v]);
			_n-=1;
			si[u]+=si[v];
			par[v]=u;
		}
	}
	int size(int v=-1){return v==-1?_n:si[parent(v)];}
};

const int _n=10000002;

int nxt[_n],usd[_n];

vector<pii> es[_n];

signed main(){
_3PGDklf;
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()),a.end());
	if(a[0]==1){
		print(0);
		return 0;
	}
	n=sz(a);
	rep(i,n){
		usd[a[i]]=i+1;
	}
	per(i,_n-1){
		if(usd[i]){
			nxt[i]=usd[i];
		}else{
			nxt[i]=nxt[i+1];
		}
	}
	for(auto&i:a){
		for(int j=0;j<_n;){
			int nj=j;
			if(j==i){
				nj++;
			}
			{
				int id=nxt[nj];
				if(id){
					id-=1;
					int w=min(a[id]%i,i%a[id]);
					es[w].emplace_back(usd[i]-1,id);
					j=max(j+i,(a[id]/i+1)*i);
				}else{
					break;
				}
			}
		}
	}
	dsu uf(n);
	int ans=0;
	for(int i=0;i<_n;i++){
		for(auto&p:es[i]){
			int u=p.fi,v=p.se;
			if(!uf.same(u,v)){
				ans+=i;
				uf.merge(u,v);
			}
		}
		if(uf.size()==1){
			break;
		}
	}
	assert(uf.size()==1);
	print(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 148 ms 278032 KB Output is correct
2 Correct 159 ms 278752 KB Output is correct
3 Correct 149 ms 278476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 274556 KB Output is correct
2 Correct 147 ms 275412 KB Output is correct
3 Correct 146 ms 278376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 278328 KB Output is correct
2 Correct 152 ms 276956 KB Output is correct
3 Correct 147 ms 278344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 215 ms 289680 KB Output is correct
2 Correct 353 ms 316596 KB Output is correct
3 Correct 258 ms 294140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 280140 KB Output is correct
2 Correct 286 ms 299760 KB Output is correct
3 Correct 215 ms 288244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 300860 KB Output is correct
2 Correct 389 ms 329508 KB Output is correct
3 Correct 253 ms 293348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 279240 KB Output is correct
2 Correct 373 ms 325172 KB Output is correct
3 Correct 220 ms 292044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 328232 KB Output is correct
2 Correct 1735 ms 522888 KB Output is correct
3 Correct 330 ms 331080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 327704 KB Output is correct
2 Correct 3491 ms 612660 KB Output is correct
3 Correct 374 ms 334608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 311068 KB Output is correct
2 Correct 3849 ms 602116 KB Output is correct
3 Correct 251 ms 296444 KB Output is correct