Submission #715241

# Submission time Handle Problem Language Result Execution time Memory
715241 2023-03-26T09:01:37 Z Fidan Sirni (COCI17_sirni) C++17
42 / 140
354 ms 786432 KB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define repn(i, a, b) for(ll i=ll(b)-1; i>=a; i--)
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define bg begin()
#define ed end()
#define pb push_back
struct DSU {
	vector<ll> e;
	DSU(ll n){
		e=vector<ll> (n, -1);
	}
	ll get(ll x){
		return e[x]<0 ? x: e[x]=get(e[x]);
	}
	bool same_set(ll a, ll b){
		return get(a)==get(b);
	}
	ll size(ll x){
		return -e[get(x)];
	}
	bool unite(ll a, ll b){
		a=get(a), b=get(b);
		if(a==b) return false;
		if(e[a]>e[b]) swap(a, b);
		e[a]+=e[b], e[b]=a;
		return true;
	}
};
void solve(){
	ll n;
	cin>>n;
	vector<ll> q(n);
	rep(i, 0, n){
		cin>>q[i];
	}
	sort(all(q));
	vector<ll> p;
	p.pb(q[0]);
	rep(i, 1, n){
		if(q[i]==q[i-1]) continue;
		p.pb(q[i]);
	}
	n=sz(p);
    DSU dsu(n);
	vector<vector<ll>> v(n, vector<ll>(n));
	set<pair<ll, pair<ll, ll>>> s;
	rep(i, 0, n){
		rep(j, i+1, n){
			v[i][j]=min(p[i]%p[j], p[j]%p[i]);
			v[j][i]=min(p[i]%p[j], p[j]%p[i]);
			s.insert({v[i][j], {i, j}});
		}
	}
	ll sum=0, c=n-1;
	while(c>0){
		auto a=(*s.begin());
		s.erase(s.begin());
		ll a1=a.ff;
		auto b=a.ss;
		ll b1=b.ff, b2=b.ss;
		if(dsu.same_set(b1, b2)) continue;
		c--;
		sum+=a1;
		dsu.unite(b1, b2);
	}
	cout<<sum;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll t=1;
	//~ cin>>t;
	while(t--){
		solve();
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 270 ms 34192 KB Output is correct
2 Correct 214 ms 30744 KB Output is correct
3 Correct 274 ms 38464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 34776 KB Output is correct
2 Correct 104 ms 19396 KB Output is correct
3 Correct 336 ms 39220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 39576 KB Output is correct
2 Correct 224 ms 31612 KB Output is correct
3 Correct 354 ms 39404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 284 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 280 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 283 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 278 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 310 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 289 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 288 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -