UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211057#3801. Thief Mastersthree_zero3010018348ms294812kbC++111.4kb2024-08-09 09:46:472024-08-09 12:39:45

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
long long read(){
	long long x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int N=5010;
int n,m,k,INF=0x3f3f3f3f;
int w[N][N],minv[N][N];
int maxv[N][N],que[N];
int a[N],b[N],c[N],d[N];
void get_max(int a[],int f[],int m){
    int hh=0,tt=-1;
    for (register int i=1;i<=m;++i){
        while(hh<=tt&&i-que[hh]>=k) hh++;
		while(hh<=tt&&a[i]>=a[que[tt]]) tt--;
		que[++tt]=i;
		f[i]=a[que[hh]];
	}
}
void get_min(int a[], int f[], int m){
    int hh =0,tt=-1;
	for(register int i=1;i<=m;++i){
		while(hh<=tt&&i-que[hh]>=k)hh++;
		while(hh<=tt&&a[i]<=a[que[tt]])tt--;
		que[++tt]=i;
 		f[i]=a[que[hh]];
    }
}
int main(){	
	n=read();
	m=read();
	k=read();
    for (register int i=1;i<=n;++i){
    	for(register int j=1;j<=m;++j){
    		w[i][j]=read();
		}      
	}
    for (register int i=1;i<=n;++i){
        get_min(w[i],minv[i],m);
        get_max(w[i],maxv[i],m);
    }
    for (register int i=k;i<=m;++i){
        for (register int j=1;j<=n;++j){
            a[j]=maxv[j][i];
            b[j]=minv[j][i];
        }
        get_max(a,c,n);
		get_min(b,d,n);
        for(register int j=k;j<=n;++j)INF=min(INF,c[j]-d[j]);
    }
   	cout<<INF;
    return 0;
}

Details

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

Test #1:

score: 10
Accepted
time: 4028ms
memory: 294808kb

input:

5000 5000 1000
112860841 963292630 950228017 843100821 628111020 568250408 901169419 322571542 98853...

output:

999988492

result:

ok single line: '999988492'

Test #2:

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

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 #3:

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

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 #4:

score: 10
Accepted
time: 2857ms
memory: 294812kb

input:

5000 5000 1000
8974 8293 3530 8179 6181 2983 6500 3525 2603 1983 6720 5938 9247 2494 4568 9909 3852 ...

output:

9999

result:

ok single line: '9999'

Test #5:

score: 20
Accepted
time: 2889ms
memory: 294808kb

input:

5000 5000 1000
3206 5640 1070 5846 5931 3274 6116 1741 543 6399 1626 115 92 3037 464 100 3700 2694 5...

output:

9999

result:

ok single line: '9999'

Test #6:

score: 10
Accepted
time: 2589ms
memory: 294808kb

input:

5000 5000 1000
9041 1397 3723 7115 4298 5210 8033 5608 6711 6539 7140 5331 4282 2599 59 9363 5817 25...

output:

9999

result:

ok single line: '9999'

Test #7:

score: 10
Accepted
time: 3134ms
memory: 294808kb

input:

5000 5000 1000
19636 14616 3910 15971 9484 16390 2221 13667 5771 1364 2375 15202 16870 7107 13189 16...

output:

21813

result:

ok single line: '21813'

Test #8:

score: 20
Accepted
time: 2851ms
memory: 294812kb

input:

5000 5000 1000
3628 1018 3668 18160 14975 18599 9087 10641 19468 16400 19072 16984 6583 7925 19853 1...

output:

21811

result:

ok single line: '21811'

Extra Test:

score: 0
Extra Test Passed