QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65076#4598. Darnassuszzxzzx123AC ✓594ms332568kbC++1.2kb2022-11-27 12:31:462022-11-27 12:31:49

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-27 12:31:49]
  • 评测
  • 测评结果:AC
  • 用时:594ms
  • 内存:332568kb
  • [2022-11-27 12:31:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define x first
#define y second
const int N=5e4+40;
vector<pii>g[N];
int a[N],pos[N];
int f[N];
int find(int x){
	if(x==f[x])	return x;
	return f[x]=find(f[x]);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			g[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			pos[a[i]]=i;
		}
		int d=sqrt(n-1);
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n&&(j-i)<=d;j++){
				ll val=abs(a[i]-a[j]);
				ll v=val*abs(j-i);
				if(v<=n-1){
					g[v].push_back({i,j});
				}
			}
			for(int j=a[i]+1;j<=n&&(j-a[i])<=d;j++){
				ll val=j-a[i];
				ll v=val*abs(pos[j]-i);
				if(v<=n-1){
					g[v].push_back({i,pos[j]});
				}
			}
		}
		for(int i=1;i<=n;i++){
			f[i]=i;
		}
		ll ans=0;
		//直接桶不用排序
		int sum=n-1;
		for(int i=1;i<=n-1;i++){
			for(int j=0;j<g[i].size();j++){
				auto a=g[i][j];
				int u=a.x,v=a.y;
				int c=find(u),d=find(v);
				if(c!=d){
					f[d]=c;
					ans+=i;
					sum--;
				}else {
					continue;
				}
			}
			if(!sum)	break;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 594ms
memory: 332568kb

input:

5
50000
37763 49860 5765 19637 25267 12594 48787 14155 47404 5524 39461 31259 25877 30705 2684 7916 11685 39034 33744 43465 9933 2403 35987 7114 26874 22167 9016 15866 8799 1644 12470 30017 38907 46034 9522 31054 8591 20733 49457 46805 12191 40590 14403 5658 6721 2458 15939 12811 27626 33040 47408 5...

output:

99246830
99608266
99997
49998
98832945

result:

ok 5 lines