1 条题解
-
0
#define LL long long using namespace std; const int N = 1010; int M,N,H; int a[N][N]; int row_max[N][N], row_min[N][N]; int mat_max[N][N], mat_min[N][N]; deque<int> dq; int main(){ cin>>M>>N>>H; for(int i=1;i<=M;i++){ for(int j=1;j<=N;j++){ cin>>a[i][j]; } } for(int i=1;i<=M;i++){ dq.clear(); for(int j=1;j<=N;j++){ while(!dq.empty() && j-dq.front()>=3) dq.pop_front(); while(!dq.empty() && a[i][dq.back()]<=a[i][j]) dq.pop_back(); dq.push_back(j); if(j>=3) row_max[i][j-1] = a[i][dq.front()]; } dq.clear(); for(int j=1;j<=N;j++){ while(!dq.empty() && j-dq.front()>=3) dq.pop_front(); while(!dq.empty() && a[i][dq.back()]>=a[i][j]) dq.pop_back(); dq.push_back(j); if(j>=3) row_min[i][j-1] = a[i][dq.front()]; } } for(int j=2;j<=N-1;j++){ dq.clear(); for(int i=1;i<=M;i++){ while(!dq.empty() && i-dq.front()>=3) dq.pop_front(); while(!dq.empty() && row_max[dq.back()][j]<=row_max[i][j]) dq.pop_back(); dq.push_back(i); if(i>=3) mat_max[i-1][j] = row_max[dq.front()][j]; } dq.clear(); for(int i=1;i<=M;i++){ while(!dq.empty() && i-dq.front()>=3) dq.pop_front(); while(!dq.empty() && row_min[dq.back()][j]>=row_min[i][j]) dq.pop_back(); dq.push_back(i); if(i>=3) mat_min[i-1][j] = row_min[dq.front()][j]; } } LL pre[N][N]={0}; for(int i=1;i<=M;i++){ for(int j=1;j<=N;j++){ pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + a[i][j]; } } LL ans=0; for(int i=2;i<=M-1;i++){ for(int j=2;j<=N-1;j++){ if(mat_max[i][j] - mat_min[i][j] <= H){ LL sum = pre[i+1][j+1] - pre[i-2][j+1] - pre[i+1][j-2] + pre[i-2][j-2]; ans = max(ans, sum); } } } cout<<ans<<endl; return 0; } //:) //114514找豆包写的
信息
- ID
- 3428
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 24
- 已通过
- 11
- 上传者