QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#734703#9538. Closest Derangementfhq_treapAC ✓233ms23300kbC++142.4kb2024-11-11 14:21:342024-11-11 14:21:35

Judging History

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

  • [2024-11-11 14:21:35]
  • 评测
  • 测评结果:AC
  • 用时:233ms
  • 内存:23300kb
  • [2024-11-11 14:21:34]
  • 提交

answer

//人生错乱多如此
#include<bits/stdc++.h>
#define ll long long 
#define N 200005

int n,k;
int a[N];
int pre[N];
int st[N][20];
int _lg[N];
#define inf 1e9

inline int minlen(int x,int y){
	// std::cerr<<x<<" "<<y<<"\n";
	if(x > y)return inf;
	if(x == y)return st[x][0];
	int _L = _lg[y - x + 1];
	return std::min(st[x][_L],st[y - (1ll << _L) + 1][_L]);
}

struct P{int l,op;};

inline int val(int u,P X){
	if(u < X.l){return (u % 2 == 0) ? u - 1 : u + 1;}
	if(u > X.l + 2){return (u % 2 == 0) ? u + 1 : u - 1;}
	if(X.op){if(u == X.l)return X.l + 2;else return u - 1;}
	if(!X.op){if(u == X.l + 2)return X.l;else return u + 1;}
}

bool operator < (P A,P B){
	bool flg = 0;
	if(A.l > B.l)std::swap(A,B),flg = 1;
	int L = std::min(A.l,B.l),R = std::max(B.l + 2,A.l + 2);
	int ind = minlen(A.l + 2,B.l);
	for(int i = A.l;i <= A.l + 2;++i){
		if(val(i,A) != val(i,B)){ind = std::min(ind,st[i][0]);}
	}
	for(int i = B.l;i <= B.l + 2;++i){
		if(val(i,A) != val(i,B)){ind = std::min(ind,st[i][0]);}
	}	
	// if(B.op == 0){ind = std::min(ind,st[B.l + 2][0]);}else{ind = std::min(ind,st[B.l + 2][0]);}
	// if(A.op == 0){ind = std::min(ind,st[A.l + 1][0]);}else{ind = std::min(ind,st[A.l][0]);}
	int ta = val(a[ind],A),tb = val(a[ind],B);
	// std::cout<<A.l<<" "<<A.op<<" "<<B.l<<" "<<B.op<<" "<<ind<<" "<<ta<<" "<<tb<<"\n";
	return (ta < tb) ^ flg;
}

#define inf 1e9

int main(){
	int _ = 1;	
	scanf("%d",&_);
	while(_ -- ){
		scanf("%d%d",&n,&k);
		for(int i = 1;i <= n;++i){scanf("%d",&a[i]);pre[a[i]] = i;st[a[i]][0] = i;}
		_lg[1] = 0;for(int i = 2;i <= n;++i){_lg[i] = _lg[i / 2] + 1;}
		for(int lev = 1;lev < 20;++lev)
		for(int i = 1;i + (1ll << lev) <= n;++i)st[i][lev] = inf;
		for(int lev = 1;lev < 20;++lev){
			for(int i = 1;i + (1ll << lev) - 1 <= n;++i){
				st[i][lev] = std::min(st[i][lev - 1],st[i + (1ll << (lev - 1))][lev - 1]);
			}
		}
		if(n % 2 == 0){
			if(k == 1){for(int i = 1;i <= n;++i){std::cout<<((a[i] % 2 == 0) ? a[i] - 1 : a[i] + 1)<<" ";}puts("");}
			else {puts("-1");}
		}else{
			using std::vector;
			vector<P>ALL;
			for(int i = 1;i + 2 <= n;i += 2){
				P A;A.l = i;A.op = 1;
				ALL.push_back(A);
				A.op = 0;
				ALL.push_back(A);
			}
			std::sort(ALL.begin(),ALL.end());
			// for(auto X : ALL){std::cout<<X.l<<" "<<X.op<<"\n";}
			if(k > ALL.size()){puts("-1");}
			else{for(int i = 1;i <= n;++i){std::cout<<val(a[i],ALL[k - 1])<<" ";}puts("");}
		}
	}
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6028kb

input:

2
2 2
2 1
3 2
1 2 3

output:

-1
3 1 2 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 5804kb

input:

50
6 1
4 2 6 3 5 1
8 1
6 2 8 7 3 4 1 5
3 298728530
2 3 1
4 610615077
2 4 3 1
4 2
4 2 3 1
7 152065238
4 1 3 5 7 6 2
6 844435075
2 6 4 5 1 3
4 544008000
3 2 4 1
9 379783780
5 9 3 8 4 2 1 7 6
5 139426002
2 4 5 3 1
2 1
1 2
2 1
1 2
3 1
3 1 2
4 2
2 4 1 3
4 1
3 2 4 1
5 4
2 4 1 5 3
3 1
1 2 3
6 805720317
5 6...

output:

3 1 5 4 6 2 
5 1 7 8 4 3 2 6 
-1
-1
-1
-1
-1
-1
-1
-1
2 1 
2 1 
1 2 3 
-1
4 1 3 2 
3 5 2 4 1 
2 3 1 
-1
-1
-1
2 1 
-1
-1
2 4 3 1 
-1
-1
-1
-1
-1
5 3 7 8 6 4 1 2 
1 3 2 
-1
3 7 9 5 8 1 4 2 6 
-1
1 2 
-1
-1
-1
8 5 3 2 4 7 1 6 
-1
-1
5 1 6 7 4 3 2 
-1
5 2 4 1 6 3 7 
1 2 
-1
2 1 
-1
4 1 3 2 5 
-1

result:

ok 50 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 5756kb

input:

100
7 3
6 4 3 5 1 2 7
7 2
7 6 5 1 3 4 2
3 579339385
3 1 2
3 244980876
1 2 3
2 1
2 1
6 184906010
1 4 2 6 5 3
6 908625780
3 2 6 4 5 1
4 223847761
2 3 1 4
7 4078777
1 3 4 2 6 5 7
5 955859204
1 3 5 4 2
5 3
4 5 3 2 1
7 6
1 7 4 3 6 2 5
6 2
4 2 3 1 6 5
2 1
1 2
7 6
5 4 6 7 2 1 3
2 2
2 1
3 2
2 3 1
6 38861221...

output:

7 3 5 4 2 1 6 
6 5 7 2 4 3 1 
-1
-1
1 2 
-1
-1
-1
-1
-1
5 4 1 3 2 
3 6 5 2 7 1 4 
-1
2 1 
7 3 5 6 1 2 4 
-1
3 1 2 
-1
5 2 4 3 1 6 
-1
2 3 1 
-1
-1
4 1 2 3 
-1
-1
2 1 3 
-1
-1
-1
-1
-1
1 2 
-1
-1
-1
2 5 3 4 1 
1 2 3 
6 5 4 1 3 2 
-1
5 7 4 3 1 2 6 
2 4 1 5 3 
-1
-1
2 4 3 6 1 5 
-1
2 4 6 1 3 5 
1 5 4 2...

result:

ok 100 lines

Test #4:

score: 0
Accepted
time: 1ms
memory: 5704kb

input:

100
8 2
8 6 4 1 7 5 3 2
3 1
2 1 3
4 408738161
1 3 4 2
9 392326366
4 3 2 1 8 7 6 5 9
7 6
2 5 1 3 7 4 6
3 662976110
1 2 3
4 584704458
3 1 2 4
2 170402059
2 1
8 195525462
8 4 2 1 7 6 5 3
5 241372504
4 5 2 3 1
2 1
1 2
7 2
6 3 7 4 1 2 5
6 1
6 3 5 1 2 4
7 5
3 2 7 4 1 6 5
8 2
5 3 4 6 1 2 7 8
4 1
2 4 3 1
2 ...

output:

-1
1 3 2 
-1
-1
3 4 2 1 6 5 7 
-1
-1
-1
-1
-1
2 1 
7 1 6 5 2 3 4 
5 4 6 2 1 3 
4 1 6 5 2 7 3 
-1
1 3 4 2 
-1
-1
2 1 3 
-1
1 3 6 5 7 4 2 
-1
-1
5 3 1 4 2 
-1
-1
-1
-1
-1
3 5 2 4 6 1 
3 1 2 
-1
5 4 9 3 7 1 2 8 6 
-1
-1
-1
-1
-1
-1
-1
2 1 3 
2 1 
-1
-1
1 6 5 2 4 3 7 8 
-1
8 2 3 5 6 9 4 7 1 
2 3 1 
-1
-...

result:

ok 100 lines

Test #5:

score: 0
Accepted
time: 1ms
memory: 5680kb

input:

1000
4 2
2 1 4 3
3 1
3 2 1
7 110856137
6 5 1 7 4 2 3
7 253418683
2 6 7 4 3 1 5
2 2
1 2
7 763879058
7 5 3 1 6 4 2
8 518300421
1 8 4 2 3 5 7 6
3 241647870
2 1 3
4 400296427
2 1 4 3
3 477979797
1 3 2
6 2
4 1 3 6 5 2
8 2
3 7 5 4 2 8 1 6
8 1
3 2 1 5 7 8 6 4
3 2
2 3 1
9 3
1 6 7 2 5 4 3 8 9
5 1
3 2 5 1 4
4...

output:

-1
1 3 2 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
4 1 2 6 8 7 5 3 
3 1 2 
2 5 9 1 6 3 4 7 8 
1 3 4 2 5 
-1
-1
4 1 2 5 3 
-1
3 8 2 1 5 9 7 6 4 
-1
-1
4 3 2 7 1 5 6 
-1
-1
-1
-1
-1
2 1 3 4 6 5 
2 1 
-1
-1
-1
2 1 3 
-1
2 1 
1 5 4 6 8 7 3 2 
-1
-1
3 2 4 5 1 
3 2 4 1 
-1
3 1 2 
3 2 1 4 
-1
8 1 3 2 9 7 5 4 6 
7 4 6...

result:

ok 1000 lines

Test #6:

score: 0
Accepted
time: 1ms
memory: 3952kb

input:

1000
6 2
6 4 2 3 1 5
9 7
6 5 3 9 1 7 2 8 4
8 290062773
5 2 6 7 8 3 4 1
6 764373478
6 2 1 5 4 3
2 2
2 1
4 280972480
1 4 2 3
3 931925743
2 3 1
5 698025972
2 5 3 1 4
3 260868128
3 2 1
4 15623583
3 4 2 1
5 2
2 5 1 4 3
7 5
3 1 5 4 2 6 7
10 2
1 5 4 3 8 2 9 7 10 6
7 3
5 2 4 6 7 1 3
10 1
5 9 2 8 7 1 4 6 10 ...

output:

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

result:

ok 1000 lines

Test #7:

score: 0
Accepted
time: 146ms
memory: 8236kb

input:

100
3412 1
258 1465 305 1663 2883 340 3112 1078 1464 1315 1418 2018 141 2901 1121 1382 938 1208 3289 3016 2358 1270 1795 3335 1566 1284 2091 1582 3137 3276 2873 1838 2049 3274 438 266 2827 1337 440 1063 2895 2953 1433 84 253 1547 3360 3125 530 2708 2985 181 2559 3308 1660 1609 287 2737 1894 871 3048...

output:

257 1466 306 1664 2884 339 3111 1077 1463 1316 1417 2017 142 2902 1122 1381 937 1207 3290 3015 2357 1269 1796 3336 1565 1283 2092 1581 3138 3275 2874 1837 2050 3273 437 265 2828 1338 439 1064 2896 2954 1434 83 254 1548 3359 3126 529 2707 2986 182 2560 3307 1659 1610 288 2738 1893 872 3047 403 3361 8...

result:

ok 100 lines

Test #8:

score: 0
Accepted
time: 52ms
memory: 22036kb

input:

131
20 1
15 20 9 10 2 3 19 5 12 11 18 17 16 4 1 14 6 13 7 8
5 2
5 3 4 2 1
6 1
3 5 4 1 6 2
41 19
6 5 1 38 41 20 10 4 21 30 22 29 24 36 16 2 8 12 27 23 3 39 33 26 25 32 18 19 37 40 15 31 11 34 14 9 28 35 7 17 13
9 6
5 8 4 7 6 1 9 3 2
4 1
1 2 4 3
41 5
33 4 8 13 7 9 23 11 36 26 25 5 39 41 2 15 31 28 30 ...

output:

16 19 10 9 1 4 20 6 11 12 17 18 15 3 2 13 5 14 8 7 
4 1 5 3 2 
4 6 3 2 5 1 
5 6 2 39 40 19 9 3 22 31 21 30 23 37 15 1 7 11 28 24 4 38 32 25 26 33 17 20 36 41 16 29 12 35 13 10 27 34 8 18 14 
6 9 3 5 7 2 8 4 1 
2 1 3 4 
32 3 7 12 8 11 22 10 37 27 24 6 38 40 1 14 30 29 31 41 5 2 23 18 20 33 26 4 16 13...

result:

ok 131 lines

Test #9:

score: 0
Accepted
time: 5ms
memory: 6040kb

input:

1000
33 13
2 18 17 8 25 30 1 29 19 6 11 13 9 14 10 22 4 26 23 31 27 3 28 15 7 24 20 21 33 32 16 5 12
40 43
24 27 31 4 26 8 33 3 25 34 28 30 7 22 38 17 14 39 11 23 20 13 10 37 15 40 5 21 32 1 29 36 2 9 35 18 19 6 16 12
41 81
2 14 3 11 1 27 33 16 40 32 31 18 39 38 26 29 20 34 30 25 12 6 8 19 17 21 4 2...

output:

1 17 18 7 26 31 2 30 20 5 12 14 10 13 9 21 3 25 24 29 28 4 27 16 8 23 19 22 32 33 15 6 11 
-1
-1
-1
19 9 7 6 12 5 13 15 8 3 18 14 1 4 11 10 17 16 2 
-1
39 19 40 13 12 49 10 43 42 22 32 14 3 5 26 28 9 23 2 7 11 16 46 24 18 1 27 30 25 29 6 41 48 20 47 37 8 44 35 31 45 17 33 15 34 21 38 4 36 
-1
-1
-1
...

result:

ok 1000 lines

Test #10:

score: 0
Accepted
time: 6ms
memory: 5696kb

input:

1000
47 88
38 47 17 13 27 3 1 25 11 23 9 5 41 40 31 20 16 46 6 2 30 19 28 32 10 26 43 36 15 45 29 33 21 24 39 34 7 44 22 42 8 14 18 12 35 37 4
30 33
5 29 3 21 2 27 25 4 19 23 17 6 7 8 14 18 20 1 26 24 28 12 13 22 16 15 9 11 30 10
62 85
17 50 62 16 15 28 20 27 57 31 11 53 58 61 14 39 24 3 33 34 38 49...

output:

-1
-1
-1
2 17 65 8 80 6 81 15 26 16 83 59 68 91 33 40 61 78 74 12 92 75 70 64 7 21 24 23 38 62 29 45 84 34 20 39 85 9 86 30 41 57 56 27 13 52 79 88 82 72 55 47 42 89 90 11 5 31 73 1 77 51 67 76 19 60 18 25 32 93 35 4 49 28 48 69 87 22 46 43 71 66 37 63 50 10 54 53 58 36 44 3 14 
-1
2 4 3 1 
-1
-1
-1...

result:

ok 1000 lines

Test #11:

score: 0
Accepted
time: 90ms
memory: 4000kb

input:

10000
15 15
14 2 5 7 9 15 4 12 10 6 1 3 13 11 8
84 4
56 16 12 74 14 71 40 21 13 52 78 2 4 46 29 3 36 70 32 22 75 58 63 27 45 6 26 47 64 33 44 31 15 28 24 1 67 30 83 68 76 73 82 39 54 38 35 41 42 61 53 11 66 34 51 37 81 25 62 17 18 5 57 60 10 84 8 48 9 19 69 80 79 7 23 59 49 43 20 55 72 77 65 50
9 17...

output:

-1
-1
-1
-1
52 55 34 79 54 22 91 82 94 16 86 41 9 38 31 78 75 72 15 46 6 45 17 35 56 70 58 73 3 40 62 57 39 84 26 65 59 53 13 87 80 92 69 88 76 19 21 90 89 4 11 42 95 44 93 51 30 48 32 36 37 5 7 63 33 50 1 49 2 28 8 24 74 77 27 47 83 43 12 14 29 20 68 85 67 25 81 10 61 60 18 71 64 66 23 
-1
67 80 22...

result:

ok 10000 lines

Test #12:

score: 0
Accepted
time: 117ms
memory: 7804kb

input:

1000
999 1602
532 982 788 569 395 942 891 814 393 246 562 806 668 631 296 787 663 782 641 448 971 98 170 14 361 249 46 133 484 402 41 608 885 825 790 724 614 304 411 644 779 567 290 931 449 47 242 121 708 433 16 358 536 933 784 773 180 851 80 459 896 254 956 604 970 540 215 541 537 639 669 939 943 3...

output:

-1
338 79 456 38 709 479 273 451 252 617 233 464 503 511 64 342 42 541 627 679 241 275 299 25 160 75 494 182 67 172 18 316 231 680 558 578 690 715 112 107 449 741 434 188 610 422 448 559 81 223 203 676 63 333 163 705 637 232 144 698 518 26 362 738 493 153 589 548 122 602 80 263 381 329 83 565 177 58...

result:

ok 1000 lines

Test #13:

score: 0
Accepted
time: 117ms
memory: 8268kb

input:

100
186 134
128 34 170 56 136 10 62 50 48 144 1 66 91 99 153 149 77 87 156 8 29 36 57 183 69 90 28 51 115 76 21 101 49 65 71 173 73 25 154 70 116 89 24 103 22 167 138 68 88 119 31 46 166 125 126 181 78 178 104 9 130 60 11 177 127 44 47 4 30 45 171 185 3 38 92 162 17 63 114 118 23 106 81 184 37 27 14...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1732 762 2848 1365 107 1567 1202 2362 970 573 1051 2142 1037 1479 1003 24 439 2026 2352 304 2689 2579 500 1658 1232 6 1402 2824 2841 967 1044 411 2343 1322 1254 3015 1135 780 3125 1937 2494 232 1175 1807 1454 2582 853 3104 1475 1843 2059 2005 383 2929 ...

result:

ok 100 lines

Test #14:

score: 0
Accepted
time: 203ms
memory: 15780kb

input:

10
32077 42391
17986 10802 3107 8912 23279 16553 26516 8313 5924 9342 3829 29290 420 18508 29472 24529 26449 5893 28485 983 8073 2722 21478 23941 22613 19864 12364 25856 27509 7951 21218 17202 13121 17591 11609 13300 15719 31876 26033 24795 25899 6433 15759 2051 29655 12031 17813 16091 28725 13894 1...

output:

-1
-1
14839 5132 2966 10074 468 13948 15989 2478 9665 9212 14639 13705 2031 13708 11921 1614 3620 20187 5082 6026 15300 2595 9727 11867 4001 10527 1418 3619 5085 3980 19407 3520 18446 11589 15261 2454 10763 9889 18895 19896 11432 2793 5 3503 19524 17879 7552 12847 12146 15744 15301 16217 18281 20746...

result:

ok 10 lines

Test #15:

score: 0
Accepted
time: 233ms
memory: 23300kb

input:

5
200000 357261
49805 123255 26648 123787 16881 172698 147556 44746 40569 173176 122155 103751 174194 42928 65635 130130 149126 189179 107060 107100 24399 154028 105771 158652 109927 78981 3086 159721 126652 130391 194285 50663 143730 191701 193671 180950 28088 11584 174201 191920 25968 31954 116771...

output:

-1
143838 18024 41263 111462 34584 179044 118745 153971 5814 99626 107956 133071 46127 136547 100260 54744 167298 121071 166176 199011 132209 109000 47621 171257 168076 199027 190697 127624 194291 169706 63927 104217 38796 198715 49650 136501 132599 122404 169308 8320 55450 132159 83408 184311 18244...

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed