Submission #863243

# Submission time Handle Problem Language Result Execution time Memory
863243 2023-10-19T21:14:25 Z blacktulip Sirni (COCI17_sirni) C++17
42 / 140
2216 ms 786432 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long lo;

#define fi first
#define se second
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
#define _ << " " <<

const lo inf = 1000000000;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,a[li],k,flag,t,fa[li],vis[10000005];
long long cev;
string s;
vector<pair<int,pair<int,int>>> v;
pair<int,int> vis2[10000005];
set<pair<int,int>> st;

inline int dsu(int x){
	if(x==fa[x])return x;
	return fa[x]=dsu(fa[x]);
}

int main(void){
	//~ fio();
	cin>>n;
	FOR{
		fa[i]=i;
		cin>>a[i];	
	}
	sort(a+1,a+n+1);
	if(a[1]==1){cout<<"0\n";return 0;}
	FOR st.insert({a[i],i});
	FOR{
		if(a[i]==a[i-1]){
			v.pb({0,{i-1,i}});
			continue;
		}
		if(vis[a[i]]){v.pb({0,{i,vis[a[i]]}});}
		int mn=1000000000;
		int ind=-1;
		for(int j=a[i];j<=10000000;j+=a[i]){
			if(!vis[j])vis[j]=i;
			else{if(vis2[j].fi!=0)v.pb({vis2[j].fi-j,{i,vis2[j].se}});continue;}
			auto it=st.lower_bound({j,-inf});
			if(j==a[i])it=st.upper_bound({j,inf});
			if(it!=st.end()){
				v.pb({it->fi-j,{it->se,i}});
				vis2[j]=*it;
				if(it->fi-j<mn){mn=it->fi-j;ind=it->se;}
			}
		}
		//~ cout<<i<<" ()() "<<endl;
		if(~ind)v.pb({mn,{ind,i}});
	}
	int last=0,ind=0,cnt=1;
	for(int i=1;i<=n;i++){
		while(cnt<a[i]){
			//~ cout<<vis[cnt]<<" () "<<a[i]<<endl;
			//~ if(vis[cnt] && vis[cnt]!=i){last=cnt;ind=vis[cnt];}
			if(vis[cnt]){last=cnt;ind=vis[cnt];}
			cnt++;
		}
		if(ind)v.pb({a[i]-last,{ind,i}});
	}
	sort(v.begin(),v.end());
	for(auto go:v){
		int co=go.fi;
		int x=go.se.fi;
		int y=go.se.se;
		//~ cout<<x<<" () "<<y<<" () "<<co<<endl;
		if(dsu(x)==dsu(y))continue;
		
		fa[dsu(x)]=dsu(y);
		cev+=co;
	}
	cout<<cev<<endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 100444 KB Output is correct
2 Correct 437 ms 171172 KB Output is correct
3 Correct 36 ms 119752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 43940 KB Output is correct
2 Runtime error 593 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 101588 KB Output is correct
2 Correct 19 ms 91996 KB Output is correct
3 Correct 27 ms 109708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 908 ms 83148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 164 ms 55716 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1344 ms 106284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 745 ms 53940 KB Output is correct
2 Correct 2216 ms 155696 KB Output is correct
3 Correct 1292 ms 106816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 442 ms 152264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 493 ms 151216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 89 ms 126756 KB Output isn't correct
2 Halted 0 ms 0 KB -