UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#165404#1445. 理想的正方形lyp1219100948ms13096kbC++2.1kb2022-11-11 08:22:382022-11-11 08:22:40

answer

#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int N=1010,M=110;

int a,b,n;
int w[N][N],cmx[N],cmn[N];
int p1[N][N],h1[N],t1[N];
int p2[N][N],h2[N],t2[N];
int p3[N],h3,t3;
int p4[N],h4,t4;
int ans=1e9;

void p1add(int i,int j)
{
	int &h=h1[i],&t=t1[i];
	while(h<=t&&w[i][p1[i][t]]<=w[i][j])	t--;
	p1[i][++t]=j;
}
void p2add(int i,int j)
{
	int &h=h2[i],&t=t2[i];
	while(h<=t&&w[i][p2[i][t]]>=w[i][j])	t--;
	p2[i][++t]=j;
}

int p1ask(int i,int j)
{
	int &h=h1[i],&t=t1[i];
	while(h<=t&&p1[i][h]<(j-n+1))	h++;
	return w[i][p1[i][h]];
}
int p2ask(int i,int j)
{
	int &h=h2[i],&t=t2[i];
	while(h<=t&&p2[i][h]<(j-n+1))	h++;
	return w[i][p2[i][h]];
}

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",&w[i][j]);
//	for(int i=n;i<=a;i++)
//	{
//		for(int j=i-n+1;j<=i;j++)
//		{
//			p1[j][1]=0;h1[j]=1;t1[j]=0;
//			p2[j][1]=0;h2[j]=1;t2[j]=0;
//			for(int k=1;k<=n-1;k++)
//			{
//				p1add(j,k);
//				p2add(j,k);
//			}
//		}
//		for(int j=n;j<=b;j++)
//		{
//			for(int k=i-n+1;k<=i;k++)
//			{
//				p1add(k,j);p2add(k,j);
//			}
//			int mn=1e9,mx=-1e9;
//			for(int k=i-n+1;k<=i;k++)
//			{
//				mn=min(mn,p2ask(k,j));
//				mx=max(mx,p1ask(k,j));
//			}
//			ans=min(ans,mx-mn);
////			cout<<i<<" "<<j<<" "<<mx<<" "<<mn<<endl;
//		}
//	}
	for(int j=1;j<=b;j++)
	{
		for(int i=1;i<=a;i++)
		{
			cmx[i]=-1e9;cmn[i]=1e9;
			p1add(i,j);p2add(i,j);
		}
		if(j<n)	continue;
		h3=h4=1;t3=t4=0;
		for(int i=1;i<n;i++)
		{
			cmx[i]=p1ask(i,j);cmn[i]=p2ask(i,j);
			while(h3<=t3&&cmx[p3[t3]]<=cmx[i])	t3--;
			p3[++t3]=i;
			while(h4<=t4&&cmn[p4[t4]]>=cmn[i])	t4--;
			p4[++t4]=i;
		}
		for(int i=n;i<=a;i++)
		{
			cmx[i]=p1ask(i,j);cmn[i]=p2ask(i,j);
			while(h3<=t3&&cmx[p3[t3]]<=cmx[i])	t3--;
			p3[++t3]=i;
			while(h4<=t4&&cmn[p4[t4]]>=cmn[i])	t4--;
			p4[++t4]=i;
			
			while(h3<=t3&&p3[h3]<(i-n+1))	h3++;
			while(h4<=t4&&p4[h4]<(i-n+1))	h4++;
			ans=min(ans,cmx[p3[h3]]-cmn[p4[h4]]);
		}
	}
	cout<<ans;
	return 0;
}

Details

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

Test #1:

score: 10
Accepted
time: 193ms
memory: 13088kb

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: 194ms
memory: 13092kb

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: 1308kb

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: 3ms
memory: 2420kb

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: 176ms
memory: 13092kb

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: 40ms
memory: 7160kb

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: 79ms
memory: 7164kb

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: 160ms
memory: 13096kb

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: 31ms
memory: 7160kb

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: 7160kb

input:

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

output:

998801667

result:

ok single line: '998801667'