QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#246125#7630. Yet Another Maximize Permutation Subarraysucup-team1447#AC ✓1444ms63816kbC++142.5kb2023-11-10 16:22:512023-11-10 16:22:52

Judging History

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

  • [2023-11-10 16:22:52]
  • 评测
  • 测评结果:AC
  • 用时:1444ms
  • 内存:63816kb
  • [2023-11-10 16:22:51]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int long long
#define ull unsigned long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

int n,p[maxn],pos[maxn];
bool vis[maxn];
int vis2[maxn];

int sum[maxn];
int tmp[maxn][5];

int Q(int o,int l,int r){
	return tmp[r][o]-tmp[l-1][o];
}

int res;
pii ans;

map<int,int>mp;
vector<pii>buc;

void adde(int u,int v,int w){
	mp[max(u,v)]+=(-w);
}

void del(int x){
	if(vis[x-1]) adde(p[x],p[x-1],-1);
	if(vis[x+1]) adde(p[x],p[x+1],-1);
	vis[x]=0;
}
void ins(int x){
	vis[x]=1;
	if(vis[x-1]) adde(p[x],p[x-1],1);
	if(vis[x+1]) adde(p[x],p[x+1],1);
}
void work(int x,int y){
	if(x==y)return;
	mp.clear();
	del(x),del(y);
	swap(p[x],p[y]);
	ins(x),ins(y);
	swap(p[x],p[y]);
	int now=0,lst=1,res1=0;
	
	auto calc = [&](int p,int l,int r){
		if(p>0 || l>r)return ;
		res1+=Q(-p,l,r);
	};
	
	for(auto [x,y]:mp){
		calc(now,lst,x-1);
		now+=y;
		lst=x;
	}
	calc(now,lst,n);
	if(res1>res) res=res1,ans=mkp(x,y);
}

void work()
{
	n=read();
	For(i,1,n)p[i]=read(),pos[p[i]]=i;
	
	For(i,1,n) sum[i]=1; sum[1]=0;
	For(i,1,n-1) sum[max(p[i],p[i+1])]--;
	For(i,1,n) {
		sum[i]+=sum[i-1];
		For(j,0,4)tmp[i][j]=tmp[i-1][j];
		if(sum[i]<=4) tmp[i][sum[i]]++;
	}
	
	For(i,0,n+1)vis2[i]=0;
	vis[0]=vis[n+1]=0;
	For(i,1,n)vis[i]=1;
	buc.clear();
	
	res=Q(0,1,n),ans=mkp(1,1);
	
	For(i,1,n)if(i!=pos[1])buc.pb(mkp(i,pos[1]));
	int l=pos[1],r=pos[1],pl=pos[1],pr=pos[1];
	vis2[1]=1;
	For(i,2,n){
		if(pos[i]<l)l=pos[i];
		if(pos[i]>r)r=pos[i];
		vis2[pos[i]]=1;
		while(pl>1 && vis2[pl-1]) --pl;
		while(pr<n && vis2[pr+1]) ++pr;
		for(int x:{pl-1,pr+1}) for(int y:{l,r}) if(x>=1 && x<=n && x!=y) buc.pb(mkp(x,y));
	}
//	cerr<<"buc "<<buc.size()<<"\n";
	for(auto [x,y]:buc)work(x,y);
//	cerr<<"res "<<res<<"\n";
	cout<<ans.fi<<" "<<ans.se<<"\n";
}

signed main()
{
//	freopen("data.in","r",stdin);
	int T=read();
	while(T--)work();
	return 0;
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 13928kb

input:

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

output:

1 1
2 1
4 1
3 1
1 1
9 4
4 2
5 1

result:

ok 8 cases

Test #2:

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

input:

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

output:

3 8
7 17
8 12
9 12
1 1
7 6
5 9
2 5
3 1
6 4

result:

ok 10 cases

Test #3:

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

input:

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

output:

13 1
3 4
9 3
15 6
1 1
5 8
1 1
1 1
4 2
5 1

result:

ok 10 cases

Test #4:

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

input:

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

output:

1 4
3 4
2 1
5 16
1 1
8 16
4 1
3 7
2 4

result:

ok 9 cases

Test #5:

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

input:

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

output:

40 39
28 9
65 61
11 2
134 3
92 149
66 1
32 12
51 63

result:

ok 9 cases

Test #6:

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

input:

8
151
150 151 149 148 129 131 130 125 126 124 128 127 123 119 115 117 122 121 116 120 118 111 112 113 110 109 108 107 105 106 103 104 102 100 98 101 99 97 96 95 80 79 72 73 71 68 69 70 56 51 47 50 48 49 46 33 35 27 34 31 30 29 32 26 28 25 24 23 22 21 20 17 19 18 16 11 12 7 5 6 1 4 3 2 10 8 9 14 13 1...

output:

63 58
2 12
9 3
32 1
35 85
12 4
70 163
86 170

result:

ok 8 cases

Test #7:

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

input:

7
144
144 143 142 141 139 137 138 136 135 133 131 130 129 127 125 120 119 116 117 109 108 107 105 104 103 80 99 100 98 95 92 91 89 88 85 81 101 79 73 72 64 63 61 59 60 58 57 56 52 51 50 45 43 44 42 41 36 34 32 33 31 30 29 27 26 21 22 20 19 13 12 11 9 5 4 3 2 1 6 7 8 10 16 17 15 14 18 24 23 25 28 35 ...

output:

37 26
19 22
53 141
96 40
69 75
29 101
16 19

result:

ok 7 cases

Test #8:

score: 0
Accepted
time: 7ms
memory: 14008kb

input:

8
487
487 486 485 484 481 480 479 477 475 472 473 469 465 464 463 458 453 452 451 450 448 449 446 435 434 433 432 431 430 429 428 427 426 419 418 414 413 408 406 405 396 390 391 389 384 386 385 382 381 380 379 376 374 375 373 372 370 371 369 368 366 367 360 359 357 356 355 354 353 351 349 350 345 34...

output:

365 413
975 126
935 805
490 73
324 794
381 498
609 59
17 1097

result:

ok 8 cases

Test #9:

score: 0
Accepted
time: 9ms
memory: 14008kb

input:

8
403
402 400 399 398 394 393 395 396 391 390 389 388 387 386 385 384 383 376 373 372 371 370 366 368 369 367 364 360 361 359 355 356 352 351 350 349 341 340 338 339 336 335 327 326 325 324 319 318 317 315 314 313 311 308 310 309 307 306 305 304 299 302 300 301 296 293 290 288 289 287 286 285 284 28...

output:

218 245
282 228
1464 2
482 565
288 214
596 100
747 224
1465 199

result:

ok 8 cases

Test #10:

score: 0
Accepted
time: 7ms
memory: 11900kb

input:

9
464
427 67 114 302 21 32 268 339 309 366 432 286 88 10 347 290 353 248 249 261 360 9 284 191 405 17 464 110 341 349 188 49 33 235 451 100 81 95 410 457 355 62 381 266 37 351 189 402 107 263 184 246 350 94 186 438 93 233 279 441 158 140 379 417 55 156 439 342 278 320 13 435 142 265 270 36 173 218 7...

output:

382 388
736 1473
849 698
515 568
63 208
397 530
343 399
725 433
200 19

result:

ok 9 cases

Test #11:

score: 0
Accepted
time: 61ms
memory: 12700kb

input:

7
2236
2234 2235 2236 2222 2221 2220 2219 2212 2213 2208 2209 2206 2205 2207 2197 2196 2194 2193 2192 2195 2191 2189 2188 2190 2180 2181 2175 2176 2177 2179 2178 2171 2168 2166 2165 2170 2167 2164 2169 2155 2154 2153 2152 2151 2129 2125 2126 2123 2120 2122 2121 2119 2117 2114 2115 2107 2106 2105 210...

output:

1500 1939
9639 14094
15053 16385
709 105
11301 14867
1907 12653
8769 1596

result:

ok 7 cases

Test #12:

score: 0
Accepted
time: 79ms
memory: 14728kb

input:

9
14132
14128 14132 14131 14129 14130 14126 14117 14115 14114 14111 14110 14112 14108 14105 14104 14103 14102 14101 14100 14091 14093 14092 14090 14088 14082 14083 14081 14077 14075 14073 14072 14074 14071 14068 14067 14060 14059 14058 14057 14056 14055 14053 14052 14051 14043 14042 14038 14034 1403...

output:

2905 2533
14623 12073
5335 6106
999 703
1216 4140
1306 9473
54 87
5749 2160
1432 1647

result:

ok 9 cases

Test #13:

score: 0
Accepted
time: 62ms
memory: 15032kb

input:

9
11668
122 3358 10900 8761 11606 6985 11355 6706 11563 3705 7295 627 11650 6491 8905 8408 1648 3401 4976 9571 8524 2075 10416 9513 3700 3565 4160 5098 8140 909 3951 11517 7913 9256 10863 3141 5284 5805 784 4403 3751 8015 10058 6313 3547 8244 726 719 9645 9538 8839 10802 681 4118 9712 8591 4722 6306...

output:

207 216
16302 356
11146 10574
2840 1503
5422 5651
313 472
9101 9799
6035 535
1766 2792

result:

ok 9 cases

Test #14:

score: 0
Accepted
time: 456ms
memory: 31668kb

input:

7
193626
193626 193625 193622 193621 193624 193623 193620 193617 193618 193619 193605 193606 193587 193578 193575 193574 193555 193563 193558 193561 193566 193554 193571 193570 193556 193567 193557 193560 193562 193559 193568 193553 193569 193565 193564 193573 193572 193550 193549 193547 193548 1935...

output:

82765 139831
58002 116795
14664 6379
36835 35586
10542 30649
24113 38850
1448 490

result:

ok 7 cases

Test #15:

score: 0
Accepted
time: 626ms
memory: 24856kb

input:

10
55747
55745 55744 55737 55734 55731 55729 55726 55724 55723 55725 55721 55720 55719 55718 55715 55713 55706 55705 55702 55701 55697 55695 55694 55692 55690 55687 55686 55685 55683 55681 55682 55680 55678 55669 55667 55666 55662 55654 55653 55649 55648 55645 55644 55643 55642 55641 55628 55627 556...

output:

28220 25048
49747 85833
41617 167132
5147 11329
1530 331
53264 5963
30379 3056
126366 133195
31910 6947
90122 92794

result:

ok 10 cases

Test #16:

score: 0
Accepted
time: 593ms
memory: 24808kb

input:

8
161886
161886 161884 161883 161881 161880 161877 161874 161873 161871 161872 161869 161868 161866 161865 161862 161858 161856 161855 161852 161851 161845 161846 161841 161839 161837 161836 161834 161833 161830 161828 161826 161820 161819 161821 161818 161817 161815 161810 161809 161807 161803 1618...

output:

145753 14047
48602 66627
15190 48450
35562 48992
54249 54651
41987 33403
78930 82807
5621 39152

result:

ok 8 cases

Test #17:

score: 0
Accepted
time: 1444ms
memory: 63816kb

input:

8
477214
477214 477209 477208 477207 477206 477204 477205 477199 477198 477200 477196 477192 477193 477189 477190 477188 477185 477184 477182 477181 477180 477174 477173 477172 477171 477170 477163 477164 477162 477157 477158 477156 477155 477154 477151 477145 477146 477134 477135 477133 477132 4771...

output:

198310 69720
10152 151
127265 139375
101073 402556
175992 198298
129760 142366
14234 5990
191453 368817

result:

ok 8 cases

Test #18:

score: 0
Accepted
time: 951ms
memory: 42876kb

input:

7
104640
80979 94896 93379 84998 101575 69108 32621 13413 19543 63751 74406 100342 91227 41571 7722 15382 88496 71052 98146 91567 98194 76721 20313 26614 79982 69719 85888 82373 104512 10114 34784 77042 99044 54249 83799 63420 72841 67390 96124 80151 102095 44828 82307 74818 58750 94133 63179 74514 ...

output:

37997 40810
108910 46196
85667 108875
27356 11560
25500 36269
218547 334416
85710 31523

result:

ok 7 cases

Test #19:

score: 0
Accepted
time: 1003ms
memory: 41512kb

input:

7
3719
3719 3718 3715 3712 3710 3711 3701 3700 3698 3696 3692 3690 3687 3686 3683 3682 3681 3678 3677 3676 3675 3673 3674 3670 3669 3664 3663 3661 3660 3658 3659 3655 3654 3651 3650 3643 3641 3635 3634 3633 3631 3632 3630 3628 3629 3627 3626 3624 3622 3618 3617 3614 3611 3610 3606 3605 3604 3599 359...

output:

2315 3633
38218 40408
11209 21572
70030 115100
207417 216701
161659 150862
264533 58611

result:

ok 7 cases

Extra Test:

score: 0
Extra Test Passed