QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#62667#4598. DarnassusqinjianbinAC ✓2184ms397300kbC++171.1kb2022-11-20 10:57:512022-11-20 10:57:51

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-20 10:57:51]
  • 评测
  • 测评结果:AC
  • 用时:2184ms
  • 内存:397300kb
  • [2022-11-20 10:57:51]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;

typedef long long ll;
const int N = 5e4 + 10;
int p[N], a[N], f[N], n;
struct Edge { int u, v, w; };
vector<Edge> e;

int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

bool cmp(Edge x, Edge y)
{
	return x.w < y.w;
}

int main()
{
	int T; scanf("%d", &T);
	while(T--)
	{
		e.clear();
		scanf("%d", &n);
		_for(i, 1, n) scanf("%d", &p[i]), a[p[i]] = i, f[i] = i;

		int t = sqrt(n);
		_for(i, 1, n)
		{
			_for(j, i + 1, n)
			{
				if(j - i > t) break;
				ll cur = 1LL * abs(i - j) * abs(p[i] - p[j]);
				if(cur <= n - 1) e.push_back({i, j, cur});
			}
			
			_for(j, p[i] + 1, n)
			{
				if(j - p[i] > t) break;
				ll cur = 1LL * abs(i - a[j]) * abs(p[i] - j);
				if(cur <= n - 1) e.push_back({i, a[j], cur});
			}
		}

		ll ans = 0;
		sort(e.begin(), e.end(), cmp);
		for(auto [u, v, w]: e)
		{
			int fu = find(u), fv = find(v);
			if(fu != fv)
			{
				f[fu] = fv;
				ans += w;
			}
		}
		printf("%lld\n", ans);
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2184ms
memory: 397300kb

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