UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213818#2406. Thief Masterserican1001901ms32620kbC++113.1kb2024-11-13 20:26:402024-11-13 23:06:24

answer

// Problem: P1807 最长路
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1807
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// Challenger: Erica N
// ----
// 
#include<bits/stdc++.h>

using namespace std;
#define rd read()
#define ull unsigned long long
#define int long long 
#define pb push_back
#define itn int
#define ps second 
#define pf first


#define rd read()
int read()
{
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9')
  {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    xx = xx * 10 + (ch - '0'), ch = getchar();
  return xx * ff;
}
#define zerol = 1
#ifdef zerol
#define cdbg(x...) do { cerr << #x << " -> "; err(x); } while (0)
void err() {
	cerr << endl;
}
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
	for (auto v: a) cerr << v << ' ';
	err(x...);
}
template<typename T, typename... A>
void err(T a, A... x) {
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif
const int N=1e3+5;
const ull P=137;
const int INF=1e18+7;
/*

策略

一维的情况我们可以使用单调队列,二维我们先竖向吧所有扩散后再一维
*/	



int a[N][N];
int b[N][N];
int anb[N][N];
int anm[N][N];
list<int> q;
int n,m,r,s;

void getMx(){
	for(int i=1;i<=m;i++){
		while(q.size())q.pop_back();
		for(int j=n;j;j--){
			while(q.size()&&a[q.back()][i]<a[j][i])q.pop_back();
			q.push_back(j);
			while(q.size()&&q.front()>=j+r)q.pop_front();
			b[j][i]=a[q.front()][i];
			
		}
	}
	
	for(int i=1;i<=n;i++){
		while(q.size())q.pop_back();
		for(int j=m;j;j--){
			while(q.size()&&b[i][q.back()]<b[i][j])q.pop_back();
			q.push_back(j);
			while(q.size()&&q.front()>=j+s)q.pop_front();
			anb[i][j]=b[i][q.front()];
		}
	}
}

void getMn(){
	// cdbg(n,m,r,s);
	
	for(int i=1;i<=m;i++){
		while(q.size())q.pop_back();
		for(int j=n;j;j--){
			while(q.size()&&a[q.back()][i]>a[j][i])q.pop_back();
			q.push_back(j);
			while(q.size()&&q.front()>=j+r)q.pop_front();
			b[j][i]=a[q.front()][i];
			
		}
	}
	
	// for(int i=1;i<=n;i++){
		// for(int j=1;j<=m;j++){
			// cerr<<b[i][j]<<' ';
		// }
// 		
		// cerr<<endl;
	// }
	
	
	for(int i=1;i<=n;i++){
		while(q.size())q.pop_back();
		for(int j=m;j;j--){
			while(q.size()&&b[i][q.back()]>b[i][j])q.pop_back();
			q.push_back(j);
			while(q.size()&&q.front()>=j+s)q.pop_front();
			anm[i][j]=b[i][q.front()];
		}
	}
}

signed main(){
	 n=rd,m=rd;
	 s=r=rd;
	 // r++;
	 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			a[i][j]=rd;
		}
	}
	
	
	
	getMx();
	// memset(b,0x3f3f,sizeof b);
	getMn();
	
	int ans=INF;
	
	for(int i=1;i<=n-r+1;i++){
		for(int j=1;j<=m-s+1;j++){
			ans=min(ans,anb[i][j]-anm[i][j]);
			
		}
		
		
	}
	
	cout<<ans<<endl;
		 
		 
	// for(int i=1;i<=n;i++){
		// for(int j=1;j<=m;j++){
			// cerr<<anb[i][j]<<' ';
		// }
// 		
		// cerr<<endl;
	// }
// 	
// 	
	// for(int i=1;i<=n;i++){
		// for(int j=1;j<=m;j++){
			// cerr<<anm[i][j]<<' ';
		// }
// 		
		// cerr<<endl;
	// }
	
	
}

详细

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

Test #1:

score: 10
Accepted
time: 284ms
memory: 32620kb

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: 0ms
memory: 1276kb

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: 6ms
memory: 3116kb

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: 341ms
memory: 32620kb

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

score: 10
Accepted
time: 115ms
memory: 16904kb

input:

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

output:

995944328

result:

ok single line: '995944328'

Test #6:

score: 10
Accepted
time: 227ms
memory: 16920kb

input:

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

output:

998299092

result:

ok single line: '998299092'

Test #7:

score: 10
Accepted
time: 411ms
memory: 32620kb

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

score: 10
Accepted
time: 139ms
memory: 16900kb

input:

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

output:

999172505

result:

ok single line: '999172505'

Test #9:

score: 10
Accepted
time: 118ms
memory: 16920kb

input:

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

output:

998801667

result:

ok single line: '998801667'

Test #10:

score: 10
Accepted
time: 260ms
memory: 32620kb

input:

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

output:

998862873

result:

ok single line: '998862873'

Extra Test:

score: 0
Extra Test Passed