UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196172#2485. 树苗tkswls100449ms4776kbC++112.6kb2023-10-19 10:44:352023-10-19 12:10:20

answer

#include <bits/stdc++.h>
using namespace std;
int t, x, a[100005], b[100005],  name[200005], ccnt, n, c[100005], l, r, ll, rr, ans, cnt;
vector<int> son[100005];
inline void build() {
	b[n / 2] = 1;
	if (n == 2) {
		b[2] = 2;
		return;
	}
	b[n / 2 + 1] = n;
	b[n / 2 - 1] = n - 1;
	l = 2, r = n - 2;
	ll = n / 2 - 1, rr = n / 2 + 1;
	int flg = 1;
	for (int i = 2; i <= r; i++) {
//		cout << i << '|' << flg << endl;
		if (flg) {
			b[++rr] = i;
			l = i + 1;
			if (r > l) {
				b[++rr] = r;
				r--;
			}
		} else {
			b[--ll] = i;
			l = i + 1;
			if (r > l) {
				b[--ll] = r;
				r--;
			}
		}
		flg = !flg;
	}
}
inline void color(int p, int q) {
//	cout << p << " " << q << "@@\n";
	name[p] = q;
	for (int i = 0; i < son[p].size(); i++) {
		if (name[son[p][i]]) continue;
		color(son[p][i], q);
	}
}
inline void solve() {
	int ct = 0;
	for (int i = 1; i <= n; i++) {
		if (b[i] == a[i]) ct++;
//		cout << b[i] << ' ';
		name[i] = 0, son[i].clear();
	}
//	cout << endl;
	for (int i = 1; i <= n; i++) {
		if (a[i] != b[i]) {
			son[a[i]].push_back(b[i]);
			son[b[i]].push_back(a[i]);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (a[i] != b[i] && !name[ a[i]]) {
			color(a[i], ++ct);
		}
	}
	ans = min(ans, n - ct);
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t >> x;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		build();
		if (x == 0) {
			long long ans = 0;
			for (int i = 1; i < n; i++) {
				ans += abs(b[i + 1] - b[i]);
			}
			cout << ans << "\n";
		} else {
			ans = 0x3f3f3f3f;
			cnt = 0;
			for (int i = 1; i <= n; i++) c[i] = a[i];
			if (c[1] != n / 2) {
				for (int i = 2; i <= n; i++) if (c[i] == n / 2) c[i] = c[1];
				c[1] = n / 2;
				cnt++;
			}
			if (c[n] != n / 2 + 1) {
				for (int i = 1; i <= n - 1; i++) if (c[i] == n / 2 + 1) c[i] = c[n];
				c[n] = n / 2 + 1;
				cnt++;
			}
			for (int i = 2; i < n; i += 2) {
				if (c[i] < n / 2) cnt++;
			}
			ans = min(ans, cnt);
			cnt = 0;
			for (int i = 1; i <= n; i++) c[i] = a[i];
			if (c[1] != n / 2 + 1) {
				for (int i = 2; i <= n; i++) if (c[i] == n / 2 + 1) c[i] = c[1];
				c[1] = n / 2 + 1;
				cnt++;
			}
			if (c[n] != n / 2 ) {
				for (int i = 1; i <= n - 1; i++) if (c[i] == n / 2 ) c[i] = c[n];
				c[n] = n / 2;
				cnt++;
			}
			for (int i = 3; i < n; i += 2) {
				if (c[i] < n / 2) cnt++;
			}
			ans = min(ans, cnt);
			cout << ans << "\n";
		}
	}
}
//1 0
//44
//22 39 6 35 15 27 21 37 9 33 1 25 17 40 12 29 7 44 2 24 14 30 13 31 10 26 4 32 3 41 11 34 19 38 18 42 8 28 23 43 20 36 16 5

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 3612kb

input:

10 1
8
6 3 2 1 7 5 8 4
8
8 3 4 2 6 5 1 7
8
2 6 4 7 5 3 1 8
8
6 4 8 7 1 3 2 5
8
3 1 8 5 6 4 7 2
8
2 1...

output:

2
3
3
2
2
2
3
2
3
2

result:

ok 10 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 3612kb

input:

10 1
16
1 2 10 14 5 15 16 4 12 8 7 6 9 13 11 3
16
13 5 11 1 7 8 15 9 6 14 3 16 12 10 4 2
16
13 1 12 ...

output:

5
5
5
5
6
5
4
5
5
5

result:

ok 10 lines

Test #3:

score: 10
Accepted
time: 0ms
memory: 3628kb

input:

10 0
1500
543 588 100 834 103 24 1404 1021 199 395 1307 992 31 1284 182 1382 1199 399 780 690 1094 4...

output:

1124999
1124999
1124999
1124999
1124999
1124999
1124999
1124999
1124999
1124999

result:

ok 10 lines

Test #4:

score: 10
Accepted
time: 0ms
memory: 3632kb

input:

10 1
1500
1289 1455 1108 966 1151 1194 129 1356 500 194 1313 1311 672 977 1161 989 1365 1454 1367 17...

output:

376
367
374
368
362
366
369
372
364
374

result:

ok 10 lines

Test #5:

score: 10
Accepted
time: 68ms
memory: 4388kb

input:

10 0
100000
13984 27116 76045 29312 31905 23594 52094 94604 88051 98481 70620 77017 87926 61572 6058...

output:

4999999999
4999999999
4999999999
4999999999
4999999999
4999999999
4999999999
4999999999
4999999999
4...

result:

ok 10 lines

Test #6:

score: 10
Accepted
time: 75ms
memory: 4776kb

input:

10 1
100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3...

output:

25002
25002
25002
25002
25002
25002
25002
25002
25002
25002

result:

ok 10 lines

Test #7:

score: 10
Accepted
time: 72ms
memory: 4772kb

input:

10 1
100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3...

output:

25002
25002
25002
25002
25002
25002
25002
25002
25002
25002

result:

ok 10 lines

Test #8:

score: 10
Accepted
time: 79ms
memory: 4772kb

input:

10 1
99808
54440 45174 32316 85631 23195 13230 12272 67712 67047 99679 80872 57285 7545 45417 49656 ...

output:

24854
24856
24934
24920
24814
24936
24939
24980
24900
24903

result:

ok 10 lines

Test #9:

score: 10
Accepted
time: 76ms
memory: 4776kb

input:

10 1
99916
22417 7937 97265 21568 58953 17520 5801 38081 76267 65401 68942 45736 54077 60159 56344 1...

output:

24944
24861
24854
24859
24945
24864
24933
24913
24939
24914

result:

ok 10 lines

Test #10:

score: 10
Accepted
time: 79ms
memory: 4772kb

input:

10 1
100000
28447 33141 26374 48851 21216 33527 12868 62675 20537 9699 86178 50390 81761 8818 32921 ...

output:

25001
24880
24818
24978
24963
24912
24842
24978
24873
24991

result:

ok 10 lines

Extra Test:

score: 0
Extra Test Passed