UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#205046#3655. T2Tony210027647ms9384kbC++113.5kb2024-06-22 12:18:102024-06-22 22:51:55

answer

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 8005, inf = 1e9;
int n, p[N], rp[N], ans;
int f[N][4], g[N][4];
struct tree1{
	int a[N * 2];
	void init(){
		for (int i = 1; i <= n * 2; i++) a[i] = inf;
	}
	void add(int x, int k){
		for (; x <= n * 2; x += x & -x) a[x] = min(k, a[x]);
	}
	int ask(int x){
		int res = inf;
		for (; x; x -= x & -x) res = min(a[x], res);
		return res;
	}
}T1;
struct tree2{
	int a[N * 2];
	void init(){
		for (int i = 1; i <= n * 2; i++) a[i] = inf;
	}
	void add(int x, int k){
		for (; x; x -= x & -x) a[x] = min(k, a[x]);
	}
	int ask(int x){
		int res = inf;
		for (; x <= n * 2; x += x & -x) res = min(a[x], res);
		return res;
	}
}T2;
vector<pair<int, int>> ad[8][N];
void add(int l, int r, int y){
	ad[0][r].emplace_back(y - l + n, -y);
	ad[1][y + r - l].emplace_back(y - l + n, -l);
	ad[2][l].emplace_back(y + r, -y);
	ad[3][y + r - l].emplace_back(y + r, r);
	ad[4][l].emplace_back(y - l + n, y + r - l);
	ad[5][y].emplace_back(y - l + n, r);
	ad[6][r].emplace_back(y + r, y + r - l);
	ad[7][y].emplace_back(y + r, -l);
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout); 
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> p[i];
		rp[p[i]] = i;
	}
	ans = 1;
	while (1){
		for (int i = 0; i < 8; i++)
			for (int j = 1; j <= n; j++)
				ad[i][j].clear();
		memset(g, 0x3f, sizeof(g));
		for (int i = 1; i <= n; i++){
			if (f[i][0] <= min(n - i, n - p[i])) add(i, i + f[i][0], p[i]);
			if (f[i][1] <= min(i - 1, n - p[i])) add(i - f[i][1], i, p[i]);
			if (f[i][2] <= min(i - 1, p[i] - 1)) add(i - f[i][2], i, p[i] - f[i][2]);
			if (f[i][3] <= min(n - i, p[i] - 1)) add(i, i + f[i][3], p[i] - f[i][3]);
		}
		T1.init();
		for (int i = 1; i <= n; i++){
			g[i][2] = min(T1.ask(p[i] - i + n) + p[i], g[i][2]);
			for (pair<int, int> p : ad[0][i]) T1.add(p.first, p.second);
		}
		T2.init();
		for (int i = 1; i <= n; i++){
			g[rp[i]][2] = min(T2.ask(i - rp[i] + n) + rp[i], g[rp[i]][2]);
			for (pair<int, int> p : ad[1][i]) T2.add(p.first, p.second);
		}
		T1.init();
		for (int i = n; i >= 1; i--){
			g[i][3] = min(T1.ask(i + p[i]) + p[i], g[i][3]);
			for (pair<int, int> p : ad[2][i]) T1.add(p.first, p.second);
		}
		T2.init();
		for (int i = 1; i <= n; i++){
			g[rp[i]][3] = min(T2.ask(rp[i] + i) - rp[i], g[rp[i]][3]);
			for (pair<int, int> p : ad[3][i]) T2.add(p.first, p.second);
		}
		T2.init();
		for (int i = n; i >= 1; i--){
			g[i][0] = min(T2.ask(p[i] - i + n) - p[i], g[i][0]);
			for (pair<int, int> p : ad[4][i]) T2.add(p.first, p.second);
		}
		T1.init();
		for (int i = n; i >= 1; i--){
			g[rp[i]][0] = min(T1.ask(i - rp[i] + n) - rp[i], g[rp[i]][0]);
			for (pair<int, int> p : ad[5][i]) T1.add(p.first, p.second);
		}
		T2.init();
		for (int i = 1; i <= n; i++){
			g[i][1] = min(T2.ask(i + p[i]) - p[i], g[i][1]);
			for (pair<int, int> p : ad[6][i]) T2.add(p.first, p.second);
		}
		T1.init();
		for (int i = n; i >= 1; i--){
			g[rp[i]][1] = min(T1.ask(rp[i] + i) + rp[i], g[rp[i]][1]);
			for (pair<int, int> p : ad[7][i]) T1.add(p.first, p.second);
		}
		bool flg = 0;
		for (int i = 1; i <= n; i++){
			if (g[i][0] <= min(n - i, n - p[i])) flg = 1;
			if (g[i][1] <= min(i - 1, n - p[i])) flg = 1;
			if (g[i][2] <= min(i - 1, p[i] - 1)) flg = 1;
			if (g[i][3] <= min(n - i, p[i] - 1)) flg = 1;
		}
		if (flg) ans++;
		else break;
		memcpy(f, g, sizeof(f));
	}
	cout << n - ans;
	return 0;
}

详细

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

Subtask #1:

score: 10
Accepted

Test #1:

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

input:

6
2 1 6 5 3 4

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

5
4 3 1 5 2

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

5
3 1 2 5 4

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

6
6 5 1 2 3 4

output:

0

result:

ok 1 number(s): "0"

Test #5:

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

input:

5
4 1 5 2 3

output:

2

result:

ok 1 number(s): "2"

Subtask #2:

score: 10
Accepted

Test #6:

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

input:

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

output:

7

result:

ok 1 number(s): "7"

Test #7:

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

input:

20
2 3 12 6 15 10 9 18 19 13 14 8 11 20 7 17 1 4 5 16

output:

9

result:

ok 1 number(s): "9"

Test #8:

score: 0
Accepted
time: 2ms
memory: 3028kb

input:

20
12 5 2 14 11 7 9 20 19 16 15 4 13 8 18 6 1 17 3 10

output:

11

result:

ok 1 number(s): "11"

Test #9:

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

input:

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

output:

9

result:

ok 1 number(s): "9"

Test #10:

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

input:

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

output:

9

result:

ok 1 number(s): "9"

Subtask #3:

score: 20
Accepted

Test #11:

score: 20
Accepted
time: 3ms
memory: 3088kb

input:

99
19 64 83 38 43 12 73 93 22 23 11 18 66 96 32 53 60 72 99 41 42 92 29 86 74 3 20 31 8 84 55 77 80 ...

output:

73

result:

ok 1 number(s): "73"

Test #12:

score: 0
Accepted
time: 3ms
memory: 3088kb

input:

100
30 19 85 8 69 70 31 13 99 29 47 98 55 62 10 63 6 80 12 36 39 48 35 24 33 93 96 72 45 95 94 43 38...

output:

76

result:

ok 1 number(s): "76"

Test #13:

score: 0
Accepted
time: 3ms
memory: 3088kb

input:

100
12 53 60 29 43 61 50 22 11 65 27 19 74 62 16 66 44 81 86 13 34 51 78 100 26 99 41 90 64 85 47 97...

output:

78

result:

ok 1 number(s): "78"

Test #14:

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

input:

100
96 32 72 75 48 80 87 36 100 17 88 2 19 61 65 86 89 64 39 66 29 49 24 63 67 8 94 52 11 76 46 69 9...

output:

75

result:

ok 1 number(s): "75"

Test #15:

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

input:

100
35 97 20 56 19 26 37 22 50 61 47 88 73 17 1 71 95 84 29 32 9 46 52 63 24 14 69 77 21 6 40 16 54 ...

output:

77

result:

ok 1 number(s): "77"

Subtask #4:

score: 30
Accepted

Test #16:

score: 30
Accepted
time: 15ms
memory: 3432kb

input:

500
118 354 357 104 493 101 245 34 198 96 342 275 215 117 451 163 409 182 309 373 99 160 356 189 100...

output:

437

result:

ok 1 number(s): "437"

Test #17:

score: 0
Accepted
time: 15ms
memory: 3432kb

input:

500
205 124 370 182 15 93 99 255 145 435 190 497 43 47 369 31 108 297 142 167 294 5 162 127 240 153 ...

output:

437

result:

ok 1 number(s): "437"

Test #18:

score: 0
Accepted
time: 15ms
memory: 3432kb

input:

500
153 118 203 14 316 310 19 321 13 481 180 74 164 337 420 199 56 158 140 101 165 128 194 21 187 49...

output:

442

result:

ok 1 number(s): "442"

Test #19:

score: 0
Accepted
time: 12ms
memory: 3436kb

input:

500
52 422 94 45 208 7 81 100 234 25 162 79 372 393 454 98 475 253 332 310 27 344 136 455 445 80 444...

output:

440

result:

ok 1 number(s): "440"

Test #20:

score: 0
Accepted
time: 15ms
memory: 3432kb

input:

500
303 214 103 215 112 146 101 297 400 195 217 191 193 54 32 435 228 410 216 441 41 55 63 139 174 1...

output:

441

result:

ok 1 number(s): "441"

Test #21:

score: 0
Accepted
time: 15ms
memory: 3432kb

input:

500
495 21 488 155 57 184 36 66 242 259 50 115 90 349 205 3 258 371 285 414 459 404 73 141 339 93 30...

output:

439

result:

ok 1 number(s): "439"

Test #22:

score: 0
Accepted
time: 15ms
memory: 3428kb

input:

500
135 113 145 438 14 20 129 223 209 477 148 104 149 318 35 52 83 425 187 220 285 237 274 295 31 10...

output:

442

result:

ok 1 number(s): "442"

Test #23:

score: 0
Accepted
time: 11ms
memory: 3428kb

input:

500
20 446 100 109 39 287 331 60 495 183 159 357 197 32 25 19 140 431 75 369 164 83 128 301 131 246 ...

output:

443

result:

ok 1 number(s): "443"

Test #24:

score: 0
Accepted
time: 15ms
memory: 3428kb

input:

500
130 284 4 186 126 145 228 402 222 450 462 248 500 185 299 33 112 26 238 224 387 393 242 259 234 ...

output:

441

result:

ok 1 number(s): "441"

Test #25:

score: 0
Accepted
time: 11ms
memory: 3436kb

input:

500
24 137 89 133 413 26 304 371 135 109 176 299 227 170 389 139 232 254 399 308 255 110 370 167 168...

output:

442

result:

ok 1 number(s): "442"

Subtask #5:

score: 30
Accepted

Test #26:

score: 30
Accepted
time: 1172ms
memory: 9356kb

input:

8000
787 4651 6606 593 821 2848 3555 1511 5871 2179 1774 592 6961 5073 894 2035 696 3844 2666 4100 5...

output:

7749

result:

ok 1 number(s): "7749"

Test #27:

score: 0
Accepted
time: 1159ms
memory: 9332kb

input:

8000
5149 5252 2272 2173 3330 1300 6212 1141 1403 2960 1665 2369 7091 3426 2738 362 1431 4227 2228 4...

output:

7755

result:

ok 1 number(s): "7755"

Test #28:

score: 0
Accepted
time: 1202ms
memory: 9368kb

input:

8000
9 114 1202 3743 5250 6703 2630 3396 1034 2448 2308 2711 384 4713 958 335 203 3094 465 2207 674 ...

output:

7744

result:

ok 1 number(s): "7744"

Test #29:

score: 0
Accepted
time: 1227ms
memory: 9376kb

input:

8000
1421 7624 7669 6514 1151 6689 3827 1348 511 2364 4690 768 2152 142 2135 5458 2343 2381 6583 734...

output:

7747

result:

ok 1 number(s): "7747"

Test #30:

score: 0
Accepted
time: 1188ms
memory: 9324kb

input:

8000
5049 5683 91 1836 5935 7841 3184 7289 1734 5365 5908 7139 2290 330 5057 2325 5289 5417 3575 543...

output:

7755

result:

ok 1 number(s): "7755"

Test #31:

score: 0
Accepted
time: 1378ms
memory: 9312kb

input:

8000
3652 607 4899 6529 300 4907 4232 297 2046 466 2567 2115 6313 5968 4120 7083 1689 5800 5896 5189...

output:

7752

result:

ok 1 number(s): "7752"

Test #32:

score: 0
Accepted
time: 1170ms
memory: 9332kb

input:

8000
2514 1186 5802 1388 6131 2711 6852 4996 2458 1475 4272 869 4965 3424 771 3658 7399 3185 2603 16...

output:

7747

result:

ok 1 number(s): "7747"

Test #33:

score: 0
Accepted
time: 1185ms
memory: 9364kb

input:

8000
4951 3333 2775 904 7597 1924 5072 4111 5475 2034 5883 2039 453 753 1103 1401 6859 4748 1690 478...

output:

7751

result:

ok 1 number(s): "7751"

Test #34:

score: 0
Accepted
time: 1161ms
memory: 9372kb

input:

8000
2310 3209 3885 5341 6414 4604 5782 2224 251 6265 5694 1424 3974 2947 2048 1345 5597 4679 7754 3...

output:

7753

result:

ok 1 number(s): "7753"

Test #35:

score: 0
Accepted
time: 1194ms
memory: 9356kb

input:

8000
3011 3842 1539 6033 3876 169 5589 7927 2133 2930 5998 6020 4590 527 4528 4750 922 6790 4875 199...

output:

7751

result:

ok 1 number(s): "7751"

Test #36:

score: 0
Accepted
time: 1171ms
memory: 9320kb

input:

8000
1914 5262 351 3996 1645 3466 7681 2627 1197 688 7327 1273 7934 2319 273 6227 406 105 388 4803 4...

output:

7754

result:

ok 1 number(s): "7754"

Test #37:

score: 0
Accepted
time: 1190ms
memory: 9360kb

input:

8000
3840 379 6303 5325 1098 2804 403 7163 2351 1932 5585 2902 1253 4770 2649 7944 4093 5584 3865 19...

output:

7747

result:

ok 1 number(s): "7747"

Test #38:

score: 0
Accepted
time: 1187ms
memory: 9320kb

input:

8000
3565 3779 674 1825 4552 1339 3699 359 173 322 4881 4807 5451 6463 168 7756 6125 6430 6654 6734 ...

output:

7747

result:

ok 1 number(s): "7747"

Test #39:

score: 0
Accepted
time: 1205ms
memory: 9372kb

input:

8000
3274 2378 513 3214 6839 973 6575 1317 4377 7976 453 1544 1190 6382 890 4626 355 3620 3772 816 6...

output:

7745

result:

ok 1 number(s): "7745"

Test #40:

score: 0
Accepted
time: 1194ms
memory: 9360kb

input:

8000
5799 4146 5604 2893 3516 4809 2881 7328 7555 6588 3898 1607 2916 3614 659 3749 1920 1891 3857 4...

output:

7743

result:

ok 1 number(s): "7743"

Test #41:

score: 0
Accepted
time: 1185ms
memory: 9336kb

input:

8000
5621 4555 2934 976 443 669 4440 5978 7497 6857 560 2442 4617 3454 3301 6564 3865 7804 949 7103 ...

output:

7751

result:

ok 1 number(s): "7751"

Test #42:

score: 0
Accepted
time: 1161ms
memory: 9316kb

input:

8000
593 6633 7267 7492 1947 6357 352 3499 35 2617 2903 1473 794 2395 6387 5210 93 1495 480 3477 775...

output:

7754

result:

ok 1 number(s): "7754"

Test #43:

score: 0
Accepted
time: 1196ms
memory: 9352kb

input:

8000
5610 2887 1565 1945 1688 2050 7055 4781 3967 5240 5911 1170 5153 1818 2541 5675 4630 1211 3472 ...

output:

7742

result:

ok 1 number(s): "7742"

Test #44:

score: 0
Accepted
time: 1181ms
memory: 9332kb

input:

8000
2104 5291 3925 1104 4928 3310 6922 4675 3363 4652 6547 3425 6256 365 3219 2283 7826 3328 1277 5...

output:

7743

result:

ok 1 number(s): "7743"

Test #45:

score: 0
Accepted
time: 1227ms
memory: 9384kb

input:

8000
5901 1536 7568 4214 550 7756 3564 1459 2528 7726 1576 7556 6241 6352 2086 614 6635 7345 312 616...

output:

7750

result:

ok 1 number(s): "7750"

Test #46:

score: 0
Accepted
time: 1193ms
memory: 9352kb

input:

8000
2465 787 2200 7684 2811 5146 1942 4344 2753 565 5215 310 7911 983 5299 6050 129 375 31 7616 837...

output:

7744

result:

ok 1 number(s): "7744"

Test #47:

score: 0
Accepted
time: 1188ms
memory: 9356kb

input:

8000
1171 7368 4415 4073 1003 959 4291 1700 2885 889 70 6022 4889 1384 904 6565 2511 3649 7881 809 3...

output:

7754

result:

ok 1 number(s): "7754"

Test #48:

score: 0
Accepted
time: 1181ms
memory: 9380kb

input:

8000
886 876 3787 1706 1594 5039 7255 7312 4783 5354 2917 1026 3870 2179 1300 1276 780 3522 7620 569...

output:

7748

result:

ok 1 number(s): "7748"