QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#294274#5529. Most Annoying Constructive Problemzhoukangyang#TL 896ms4004kbC++145.8kb2023-12-30 11:10:142023-12-30 11:10:16

Judging History

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

  • [2023-12-30 11:10:16]
  • 评测
  • 测评结果:TL
  • 用时:896ms
  • 内存:4004kb
  • [2023-12-30 11:10:14]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long 
#define i128 __int128
#define pb emplace_back
using namespace std; 
const int N = 1007;
int n, k;
int p[N];
bool h[N][N];
int vis[N];
vi V[N];
int cnt;
void Gen() {
	cnt = 0;
	L(i, 1, n) {
		L(j, i + 1, n) {
			h[i][j] = p[i] > p[j];
		}
	}
	L(i, 1, n) {
		L(j, i + 1, n) {
			h[i][j] ^= h[i][j - 1];
		}
	}
	R(i, n, 1) {
		L(j, i + 1, n) {
			h[i][j] ^= h[i + 1][j];
		}
	}
	L(i, 1, n) {
		L(j, i + 1, n) {
			cnt += h[i][j];
		}
	}
}
void upm(int l, int r) {
	L(j, l, r) 
		R(i, j - 1, 1) 
			cnt -= h[i][j], 
			h[i][j] = h[i + 1][j] ^ h[i][j - 1] ^ h[i + 1][j - 1] ^ (p[i] > p[j]),
			cnt += h[i][j];
	R(i, r, l) 
		L(j, i + 1, n) 
			cnt -= h[i][j], 
			h[i][j] = h[i + 1][j] ^ h[i][j - 1] ^ h[i + 1][j - 1] ^ (p[i] > p[j]), 
			cnt += h[i][j];
}
mt19937_64 orz;
const int mx = 1e9;
inline int rad(int l, int r) {
	return orz() % (r - l + 1) + l;
}
inline bool update(int x, int nw) {
	if(x < k && nw >= x) {
		return 1;
	}
	if(x > k && nw <= x) {
		return 1;
	}
	return orz() % mx < exp(-(double) abs(nw - k) / n * 10) * mx;
}
void solve(int tn, int tm) {
	n = tn, k = tm;
	if(k < n * (n - 1) / 4) {
		L(i, 1, n) {
			p[i] = i;
		}
	} else {
		L(d, 1, n / 2) {
			p[d * 2 - 1] = d * 2 + 1;
			p[d * 2] = d * 2 - 1;
			if(1 < d && d < n / 2) {
				--p[d * 2];
			}
			if(d == n / 2) 
				--p[d * 2], --p[d * 2 - 1];
		}
		if(n % 2)p[n] = n;
	}
	Gen();
	if((cnt - k) & 1) { 
		swap(p[1], p[2]);
		Gen();
	}
	while(true){
		int lst = cnt;
		if(orz() % 2 == 0) {
			int d = rad(1, n - 2);
			swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);	
			upm(d, d + 2);
			if(!update(lst, cnt)) {
				swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
				upm(d, d + 2);
//				Gen();	
			} 	
		}else {
			int d = rad(1, n - 2);
			swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
			upm(d, d + 2);
			if(!update(lst, cnt)) {
				swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);	
				upm(d, d + 2);
			} 
		}
		if(cnt == k) {
			Gen();
			cout << "YES\n";
			L(i, 1, n) cout << p[i] << ' ';
			cout << '\n';
			break;
		}
		if(orz()%(n*20)==0){
			solve(tn,tm);
			return;
		}
	}
} 
void mini_solve(int tn, int tm) {
	n = tn, k = tm;
	if(k < n * (n - 1) / 4) {
		L(i, 1, n) {
			p[i] = i;
		}
	} else {
		L(d, 1, n / 2) {
			p[d * 2 - 1] = d * 2 + 1;
			p[d * 2] = d * 2 - 1;
			if(1 < d && d < n / 2) {
				--p[d * 2];
			}
			if(d == n / 2) 
				--p[d * 2], --p[d * 2 - 1];
		}
		if(n % 2)p[n] = n;
	}
	Gen();
	int Lun = (n + 3) / 3;
	int g = sqrt(k) * 4;
	g = min(g, n - 2);
	L(d, 1, Lun){
		int lst = cnt;
		if(orz() % 2 == 0) {
			int d = rad(1, g);
			if(orz() & 1) d = n - 1 - d;
			swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);	
			upm(d, d + 2);
			if(!update(lst, cnt)) {
				swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
				upm(d, d + 2);
			} 	
		}else {
			int d = rad(1, g);
			if(orz() & 1) d = n - 1 - d;
			swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
			upm(d, d + 2);
			if(!update(lst, cnt)) {
				swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);	
				upm(d, d + 2);
			} 
		}
		if(cnt == k) {
			Gen();
			cout << cnt << ' ' << k << endl;
			cout << "YES\n";
			L(i, 1, n) cout << p[i] << ' ';
			cout << '\n';
			return;
		}
	}
	mini_solve(tn, tm);
} 
bitset < N > dm[N], shan[N];
int ts;
void islv(int tn, int tm) {
	n = tn;
	k = tm;
	auto dfs = [&] (auto self, int x, int cur) {
		if(cur < 0)return 0;
		int con = n * (n - 1) / 2 - (x - 1) * (x - 2) / 2 - max(n - x - 1, 0) / 2;
		if(cur > con)return 0;
		if(x == n + 1) {
			cout << "YES\n";
			L(i, 1, n) cout << p[i] << ' ';
			cout << '\n';
			Gen();
			return 1;
		}
		dm[x] = dm[x - 1];
		bitset < N > add;
		R(i, x, max(1, x - 4)) {
			if(i < x) {
				int pos = 0;
				R(j, x - 1, 1) {
					if(p[j] == i) {
						pos = j;
						break;
					}
				}
				add.set(pos);
				++p[pos];
				dm[x] ^= shan[pos];
			} 
			p[x] = i;
			int cmll = cur - dm[x].count();
			if(self(self, x + 1, cmll)) return 1;
		}
		for(int d = add._Find_first(); d <= n; d = add._Find_next(d))
			--p[d];
		return 0;
	};
	if(dfs(dfs, 1, k)) {
		
	} else {
		cout << "NO\n";
	}
}
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0); 
	L(i, 1, 1000) {
		shan[i] = shan[i - 1];
		shan[i].set(i);
	}
	int t;
	cin >> t; 
	while(t--) {
		int n, m;
		cin >> n >> m;
		int mxm = n * (n - 1) / 2 - n / 2;
		if(m - mxm <= n || n <= 5) {
			islv(n, m);
		} else if(m < n) {
			mini_solve(n, m);
		} else {
			solve(n, m);
		}
	}
	return 0;
	
	cin >> n;
	L(i, 1, n) {
		p[i] = i;
	}
	do {
		Gen();
		int cnt = 0;
		L(i, 1, n) {
			L(j, i + 1, n) {
				cnt += h[i][j];
			}
		}
		if(n*(n-1)/2-n/2-cnt<=3){
			L(i, 1, n) {
				L(j, 1, n) {
					if(j <= i) cout << " ";
					else cout << (p[i] > p[j]); // h[i][j];
				}
				cout << endl;
			} 
			L(i,1,n)cout<<p[i]<<' ';
			cout<<":"<<cnt<<endl;
			cout<<endl; 
		} 
		vis[cnt] = 1;
	} while(next_permutation(p + 1, p + n + 1));
//	int gm=0;
//	L(i, 0, n * (n - 1) / 2) {
//		if(!vis[i])++gm;
////		cout<<vis[i];
//		if(vis[i]) {
//			L(d, 1, n) {
//				p[d] = V[i][d - 1];
//			}
//			Gen();
//			L(i, 1, n) {
//				L(j, 1, n) {
//					if(j <= i) cout << " ";
//					else cout << (p[i] > p[j]); // h[i][j];
//				}
//				cout << endl;
//			} 
//			for(auto&p : V[i]){
//				cout << p << ' ';
//			}
//			cout << endl;
//			cout << i << endl;
//			cout << endl;
//		}
//	}
//	cout<<gm<<endl;
	return 0;
}

詳細信息

Test #1:

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

input:

4
1 0
3 3
4 1
6 15

output:

YES
1 
YES
3 2 1 
YES
1 3 4 2 
NO

result:

ok Correct (4 test cases)

Test #2:

score: 0
Accepted
time: 126ms
memory: 4004kb

input:

5951
27 255
28 371
31 320
33 201
32 199
31 108
15 78
27 32
24 268
20 4
14 82
29 202
33 85
26 104
31 380
27 330
20 140
29 192
21 63
17 89
20 41
32 444
20 76
27 114
33 330
26 249
33 301
24 87
25 72
14 49
25 281
21 58
15 48
16 19
14 0
22 175
11 7
30 43
31 259
22 112
12 30
30 111
33 268
30 287
19 150
15...

output:

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

result:

ok Correct (5951 test cases)

Test #3:

score: 0
Accepted
time: 210ms
memory: 3744kb

input:

3201
37 408
37 275
34 107
35 521
36 552
37 582
35 81
36 53
34 16
33 40
34 70
33 305
36 367
35 55
35 15
35 416
33 92
36 590
33 206
35 241
34 404
36 154
35 582
33 359
36 25
37 412
38 162
36 579
34 194
35 92
36 429
36 458
37 662
35 71
34 78
35 90
36 304
33 448
36 98
36 605
34 31
34 421
35 509
37 90
36 ...

output:

YES
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 24 22 25 26 28 23 32 33 30 27 34 29 36 31 37 35 
YES
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 30 28 31 32 34 29 36 33 37 35 
YES
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 27 29 30 31 26 2...

result:

ok Correct (3201 test cases)

Test #4:

score: 0
Accepted
time: 278ms
memory: 3864kb

input:

2587
39 606
40 291
40 10
40 382
38 71
40 558
38 179
41 178
40 479
39 596
40 356
41 303
39 415
38 528
39 204
39 432
38 484
38 365
38 690
38 431
40 733
38 364
40 746
40 409
40 240
41 342
39 278
38 79
39 573
39 118
38 653
40 600
40 252
38 628
40 611
41 242
40 650
39 195
38 114
39 551
39 620
40 15
38 51...

output:

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

result:

ok Correct (2587 test cases)

Test #5:

score: 0
Accepted
time: 318ms
memory: 3808kb

input:

2277
41 527
41 310
41 697
41 108
42 610
41 320
42 390
43 482
43 266
42 2
42 599
43 95
43 204
41 392
42 29
41 57
43 201
42 465
43 166
43 169
42 471
41 539
41 358
41 100
43 60
42 475
42 604
41 451
41 246
42 61
42 113
42 36
43 437
41 532
43 582
43 573
41 312
42 638
42 590
42 632
42 621
43 464
41 654
43...

output:

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

result:

ok Correct (2277 test cases)

Test #6:

score: 0
Accepted
time: 448ms
memory: 3832kb

input:

2095
43 903
43 836
43 288
43 362
44 461
44 748
43 360
45 160
45 111
44 696
43 518
43 441
43 636
43 853
44 594
43 23
43 540
44 167
44 735
43 317
43 127
44 795
43 347
43 445
44 388
43 880
44 177
43 744
43 131
44 208
43 521
43 342
45 128
44 908
43 843
45 36
43 404
43 311
43 185
44 727
44 599
43 375
43 ...

output:

NO
YES
1 2 3 4 5 6 7 8 9 12 10 14 15 16 11 18 13 20 17 22 19 24 21 26 23 28 25 30 27 32 29 34 31 36 33 38 35 40 37 42 39 43 41 
YES
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 33 34 36 35 38 39 40 42 37 43 41 
YES
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...

result:

ok Correct (2095 test cases)

Test #7:

score: 0
Accepted
time: 428ms
memory: 3840kb

input:

1932
45 489
46 403
45 883
46 531
45 906
45 54
45 320
45 410
45 425
45 719
45 342
46 628
46 508
45 950
45 825
46 309
46 687
46 164
46 284
46 634
45 87
45 836
46 719
46 313
45 687
45 602
45 773
46 629
46 301
46 219
45 59
46 670
46 58
45 538
46 33
45 229
45 919
45 805
46 107
45 971
46 335
45 555
45 552...

output:

YES
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 32 31 34 36 37 38 33 40 35 42 39 43 41 44 45 
YES
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 33 34 36 35 37 39 40 41 43 38 45 42 46 44 
YES
1 2 3 4 5 6 7 8 9 10 11 12 15 1...

result:

ok Correct (1932 test cases)

Test #8:

score: 0
Accepted
time: 896ms
memory: 3812kb

input:

1585
50 173
50 325
51 176
50 136
51 211
50 766
51 203
51 107
51 145
50 990
50 1054
50 542
50 135
51 234
51 302
50 346
50 1051
50 733
51 27
50 559
50 967
50 38
50 861
50 832
50 83
50 551
51 33
51 332
50 977
50 466
51 146
50 912
51 312
50 256
50 891
50 1076
51 319
50 710
50 269
50 689
51 143
50 259
50...

output:

YES
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 33 34 35 36 37 38 39 40 41 43 45 46 49 42 48 44 47 50 
YES
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 33 34 35 36 37 38 39 41 43 44 40 46 47 42 45 49 50 48 
YES
1 2 ...

result:

ok Correct (1585 test cases)

Test #9:

score: 0
Accepted
time: 765ms
memory: 3824kb

input:

1422
53 964
53 573
53 593
53 923
53 901
53 1244
53 29
54 2
53 544
53 89
53 229
53 304
53 1035
53 1002
53 184
53 951
53 1123
53 1167
53 32
53 1342
53 1037
53 1280
53 943
53 850
53 309
53 925
53 587
53 238
53 714
53 872
53 52
54 24
53 233
53 908
53 672
53 1286
53 910
54 21
53 407
53 106
53 555
53 764
...

output:

YES
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 30 28 32 33 34 29 36 31 38 39 40 35 42 37 44 45 46 41 48 43 50 51 52 47 53 49 
YES
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 33 34 35 36 37 38 39 42 40 43 44 46 41 48 49 50 45 52 ...

result:

ok Correct (1422 test cases)

Test #10:

score: -100
Time Limit Exceeded

input:

400
100 221
100 321
100 334
100 1
100 17
100 166
100 312
100 186
100 343
100 39
100 206
100 288
100 134
100 127
100 159
100 307
100 105
100 160
100 213
100 319
100 337
100 264
100 181
100 115
100 267
100 308
100 171
100 192
100 140
100 309
100 189
100 20
100 350
100 130
100 376
100 246
100 165
100 3...

output:


result: