UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#148825#1445. 理想的正方形dxh100811ms20992kbC++1.5kb2022-07-17 20:41:462022-07-17 22:08:49

answer

#include<bits/stdc++.h>

using namespace std;

int a,b,n,c[1007][1007];
int hmax[1007][1007],hmin[1007][1007],lmax[1007][1007],lmin[1007][1007];
int head1,head2,tail1,tail2,q1[1007],q2[1007],C1[1007],C2[1007];

int main()
{
	scanf("%d%d%d",&a,&b,&n);
	for(int i=1;i<=a;i++){
		for(int j=1;j<=b;j++) scanf("%d",&c[i][j]);
	}
	for(int i=1;i<=a;i++){
		head1=head2=tail1=tail2=C1[1]=C2[1]=1;
		q1[1]=q2[1]=c[i][1];
		for(int j=2;j<=b;j++){
			while(head1<=tail1&&q1[tail1]<=c[i][j]) tail1--;
			while(head2<=tail2&&q2[tail2]>=c[i][j]) tail2--;
			q1[++tail1]=q2[++tail2]=c[i][j];C1[tail1]=C2[tail2]=j;
			if(head1<=tail1&&C1[head1]<j-n+1) head1++;
			if(head2<=tail2&&C2[head2]<j-n+1) head2++;
			if(j>=n){
				hmax[i][j-n+1] = q1[head1];
				hmin[i][j-n+1] = q2[head2];
			}
		}
	}
	for(int j=1;j<=b-n+1;j++){
		head1=head2=tail1=tail2=C1[1]=C2[1]=1;
		q1[1]=hmax[1][j];q2[1]=hmin[1][j];
		for(int i=2;i<=a;i++){
			while(head1<=tail1&&q1[tail1]<=hmax[i][j]) tail1--;
			while(head2<=tail2&&q2[tail2]>=hmin[i][j]) tail2--;
			q1[++tail1]=hmax[i][j];q2[++tail2]=hmin[i][j];
			C1[tail1]=C2[tail2]=i;
			if(head1<=tail1&&C1[head1]<i-n+1) head1++;
			if(head2<=tail2&&C2[head2]<i-n+1) head2++;
			if(i>=n){
				lmax[i-n+1][j] = q1[head1];
				lmin[i-n+1][j] = q2[head2];
			}
		}
	}
	int ans=2147483647;
	for(int i=1;i<=a-n+1;i++){
		for(int j=1;j<=b-n+1;j++){
			ans = min(ans,lmax[i][j]-lmin[i][j]);
		}
	}
	printf("%d",ans);
	return 0;
}

详细

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

Test #1:

score: 10
Accepted
time: 211ms
memory: 20988kb

input:

1000 1000 100
804544523 340648618 718292412 235345736 704741136 942776831 741228920 463473302 677289...

output:

998893495

result:

ok single line: '998893495'

Test #2:

score: 10
Accepted
time: 142ms
memory: 20988kb

input:

1000 1000 100
629053044 957239666 456746076 940194386 201529807 592062148 244394389 825620014 223976...

output:

998862873

result:

ok single line: '998862873'

Test #3:

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

input:

5 4 2
1 2 5 6
0 17 16 0
16 17 0 1
2 10 2 1
1 2 3 2

output:

2

result:

ok single line: '2'

Test #4:

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

input:

100 100 10
2 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 1200001 ...

output:

908999

result:

ok single line: '908999'

Test #5:

score: 10
Accepted
time: 124ms
memory: 20988kb

input:

1000 1000 20
1 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 120000...

output:

1901899

result:

ok single line: '1901899'

Test #6:

score: 10
Accepted
time: 31ms
memory: 19360kb

input:

500 500 50
79289095 232165705 955620938 481434262 465576217 112035388 50089892 459799006 181906335 3...

output:

995944328

result:

ok single line: '995944328'

Test #7:

score: 10
Accepted
time: 70ms
memory: 19244kb

input:

500 1000 80
499163842 331022295 940054497 684192083 248823911 842132608 629298697 398526298 98438040...

output:

998299092

result:

ok single line: '998299092'

Test #8:

score: 10
Accepted
time: 124ms
memory: 20992kb

input:

1000 1000 80
102 134 429 251 299 109 264 669 727 296 112 550 270 270 544 660 131 546 968 219 113 678...

output:

998

result:

ok single line: '998'

Test #9:

score: 10
Accepted
time: 37ms
memory: 19168kb

input:

500 500 100
65532583 920409544 753795976 989349545 818384831 778207112 425141881 853270293 542112588...

output:

999172505

result:

ok single line: '999172505'

Test #10:

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

input:

500 1000 100
456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...

output:

998801667

result:

ok single line: '998801667'