Submission #633629

# Submission time Handle Problem Language Result Execution time Memory
633629 2022-08-22T21:41:54 Z inksamurai Sirni (COCI17_sirni) C++17
98 / 140
5000 ms 473132 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];

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];
		}
	}
	using T=pair<int,pii>;
	vec(T) es;
	for(auto&i:a){
		if(!usd[i]) continue;
		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.emplace_back(w,pii(usd[i]-1,id));
					j=max(j+i,(a[id]/i+1)*i);
				}else{
					break;
				}
				// nj=a[id]+1;
			}
		}
	}
	sort(es.begin(), es.end());
	dsu uf(n);
	int ans=0;
	for(auto&tp:es){
		int u=tp.se.fi,v=tp.se.se;
		if(!uf.same(u,v)){
			ans+=tp.fi;
			uf.merge(u,v);
		}
	}
	assert(uf.size()==1);
	print(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 51 ms 43140 KB Output is correct
2 Correct 92 ms 44576 KB Output is correct
3 Correct 46 ms 43584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 39692 KB Output is correct
2 Correct 55 ms 40756 KB Output is correct
3 Correct 47 ms 43564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 43440 KB Output is correct
2 Correct 45 ms 41984 KB Output is correct
3 Correct 44 ms 43556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 258 ms 68520 KB Output is correct
2 Correct 758 ms 93192 KB Output is correct
3 Correct 320 ms 68472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 46608 KB Output is correct
2 Correct 446 ms 92612 KB Output is correct
3 Correct 238 ms 66636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 518 ms 93200 KB Output is correct
2 Correct 956 ms 142352 KB Output is correct
3 Correct 308 ms 68520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 46664 KB Output is correct
2 Correct 937 ms 142416 KB Output is correct
3 Correct 285 ms 68524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 356 ms 103816 KB Output is correct
2 Execution timed out 5071 ms 473132 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 357 ms 103744 KB Output is correct
2 Execution timed out 5079 ms 472188 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 96 ms 76720 KB Output is correct
2 Execution timed out 5093 ms 465040 KB Time limit exceeded
3 Halted 0 ms 0 KB -