답안 #66534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
66534 2018-08-11T10:46:46 Z KLPP Sirni (COCI17_sirni) C++14
126 / 140
5000 ms 41464 KB
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<stdio.h>

using namespace std;
#define MAXP 10000000

int pnt[MAXP+5];
int parent[1000000];
int size[1000000];
int root(int x){
	if(parent[x]==x)return x;
	parent[x]=root(parent[x]);
	return parent[x];
}
bool component(int a, int b){
	a=root(a);
	b=root(b);
	if(root(a)==root(b))return true;
	return false;
}
void merge(int a, int b){
	a=root(a);
	b=root(b);
	if(a==b)return;
	if(size[a]<size[b]){
		size[b]+=size[a];
		parent[a]=b;
		return;
	}
	size[a]+=size[b];
	parent[b]=a;
}
int main(){
	int n;
	scanf("%d",&n);
	int arr[n];
	for(int i=0;i<n;i++)scanf("%d",&arr[i]);
	sort(arr,arr+n);
	/*if(n<=1000){
	vector<pair<int,pair<int,int> > >Edges;
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			Edges.push_back(pair<int,pair<int,int> >(arr[j]%arr[i],pair<int,int>(i,j)));
		}
	}
	sort(Edges.begin(),Edges.end());
	begin(n);
	long long int ans=0;
	int CC=n;
	for(int i=0;i<Edges.size() && CC>1;i++){
		int x,y;
		x=Edges[i].second.first;
		y=Edges[i].second.second;
		if(!component(x,y)){
			merge(x,y);
			CC--;
			ans+=Edges[i].first;
		}
		//cout<<x<<" "<<y<<endl;
	}cout<<ans<<endl;
	return 0;
	}*/
	queue<int> array;
	array.push(arr[0]);
	for(int i=1;i<n;i++){
		if(arr[i]!=arr[i-1]){
			array.push(arr[i]);
		}
	}
	n=array.size();
	for(int i=0;i<n;i++){
		arr[i]=array.front();
		array.pop();
	}
	for(int i=0;i<MAXP+5;i++)pnt[i]=-1;
	for(int i=0;i<n;i++){
		pnt[arr[i]-1]=i;
	}
	long long int ans=0;
	int CC=n;
	for(int i=0;i<n;i++){
		parent[i]=i;
		size[i]=1;
	}
	int round=0;
	while(CC>1){
		//Plog(n)
		for(int i=0;i<n;i++){
			if(arr[i]>round){
				for(int j=arr[i]+round-1;j<MAXP;j+=arr[i]){
					if(pnt[j]!=-1 && !component(i,pnt[j])){
						merge(i,pnt[j]);
						CC--;
						ans+=round;
					}
				}
			}
		}
		round++;
	}printf("%lld\n",ans);

	return 0;
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
sirni.cpp:40:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0;i<n;i++)scanf("%d",&arr[i]);
                      ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 39504 KB Output is correct
2 Correct 1354 ms 39656 KB Output is correct
3 Correct 572 ms 39832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 704 ms 39832 KB Output is correct
2 Correct 886 ms 39832 KB Output is correct
3 Correct 136 ms 39832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 685 ms 39832 KB Output is correct
2 Correct 194 ms 39832 KB Output is correct
3 Correct 548 ms 39832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1709 ms 41264 KB Output is correct
2 Correct 2988 ms 41264 KB Output is correct
3 Correct 488 ms 41264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 603 ms 41264 KB Output is correct
2 Correct 3559 ms 41264 KB Output is correct
3 Correct 2239 ms 41304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3033 ms 41316 KB Output is correct
2 Correct 1632 ms 41316 KB Output is correct
3 Correct 441 ms 41324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1741 ms 41324 KB Output is correct
2 Correct 2391 ms 41324 KB Output is correct
3 Correct 420 ms 41324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1720 ms 41340 KB Output is correct
2 Correct 1147 ms 41428 KB Output is correct
3 Correct 1440 ms 41464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 41464 KB Output is correct
2 Correct 2758 ms 41464 KB Output is correct
3 Correct 143 ms 41464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1211 ms 41464 KB Output is correct
2 Execution timed out 5028 ms 41464 KB Time limit exceeded
3 Halted 0 ms 0 KB -