답안 #949304

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949304 2024-03-19T05:36:12 Z Baytoro Sirni (COCI17_sirni) C++17
140 / 140
1786 ms 748156 KB
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define mp make_pair
#define ll long long
const ll INF=1e18,N=1e7+5;
ll par[N];
vector< pair<int,int> > g[N];
int f(ll x){
	if(par[x]==x) return x;
	return par[x]=f(par[x]);
}
bool add(ll a, ll b){
	a=f(a),b=f(b);
	if(a==b) return 0;
	if(rand()%2) swap(a,b);
	par[a]=b;
	return 1;
}
void solve(){
	int n;cin>>n;
	vector<ll> p(n);
	ll mx=0;
	for(int i=0;i<n;i++){
		cin>>p[i];
		mx=max(mx,p[i]);
	}
	sort(all(p));
	p.erase(unique(all(p)),p.end());
	n=p.size();
	vector<int> nxt(mx+1,-1);
	for(int i=0;i<n;i++){
		nxt[p[i]]=i;
		par[i]=i;
	}
	for(int i=mx-1;i>=0;i--){
		if(nxt[i]==-1) nxt[i]=nxt[i+1];
	}
	vector<pair<int,pair<int,int> > > a;
	for(int i=0;i<n-1;i++){
		if(i<n-1 && p[i]==p[i+1]) continue;
		g[p[i+1]%p[i]].pb(make_pair(i+1,i));
		//a.pb(mp(p[i+1]%p[i],mp(p[i],p[i+1])));
		for(int j=p[i]*2;j<=mx;j+=p[i]){
			//a.pb(mp(nxt[j]%p[i],mp(p[i],nxt[j])));
			g[p[nxt[j]]%p[i]].pb(make_pair(nxt[j],i));
		}
	}
	sort(all(a));
	ll ans=0;
	for(int i=0;i<=mx;i++){
		for(auto it: g[i]){
			//cout<<it.fr<<' '<<it.sc<<' '<<f(it.fr)<<' '<<f(it.sc)<<' ';
			if(add(it.fr,it.sc)){
				ans+=i;
				//cout<<"<-";
			}
			//cout<<endl;
		}
	}
	cout<<ans<<endl;
}
int main(){
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 274448 KB Output is correct
2 Correct 197 ms 303696 KB Output is correct
3 Correct 132 ms 274444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 235344 KB Output is correct
2 Correct 1082 ms 669116 KB Output is correct
3 Correct 120 ms 275280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 274536 KB Output is correct
2 Correct 118 ms 274176 KB Output is correct
3 Correct 119 ms 274440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 250260 KB Output is correct
2 Correct 174 ms 278772 KB Output is correct
3 Correct 134 ms 261048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 241076 KB Output is correct
2 Correct 129 ms 262664 KB Output is correct
3 Correct 111 ms 248272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 262428 KB Output is correct
2 Correct 203 ms 296140 KB Output is correct
3 Correct 128 ms 259356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 239972 KB Output is correct
2 Correct 203 ms 296596 KB Output is correct
3 Correct 129 ms 261112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 288792 KB Output is correct
2 Correct 1132 ms 634120 KB Output is correct
3 Correct 220 ms 291548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 293240 KB Output is correct
2 Correct 1786 ms 748156 KB Output is correct
3 Correct 323 ms 349520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 276968 KB Output is correct
2 Correct 1640 ms 635108 KB Output is correct
3 Correct 153 ms 263028 KB Output is correct