子矩阵(十四届蓝桥杯python组A)

这题思路就是用两次单调队列。

下面是TIE的代码。9/10;用了priority_queue做单调队列,而不是手动写的。

(手写单调队列好麻烦~~~,脑子晕死,考试的时候过了9个点也ok,)

cpp 复制代码
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;
const int N = 1E3+10;
const int mod = 998244353;
typedef long long ll;
typedef pair<int,int> pll;

priority_queue<pll> maxn[N];
priority_queue<pll,vector<pll>,greater<pll>> minn[N];
int maxnum[N][N];
int minnum[N][N];

priority_queue<pll> maxl[N];
priority_queue<pll,vector<pll>,greater<pll>> minl[N];


int main (){
	int n,m,a,b;cin>>n>>m>>a>>b;
	for(int i = 1;i<=n;++i){
		for(int j = 1;j<=m;++j){
			int a;cin>>a;
			maxn[i].push({a,j});
			minn[i].push({a,j});
			while(maxn[i].top().second<=j-b){
				maxn[i].pop();
			}
			while(minn[i].top().second<=j-b){
				minn[i].pop();
			}
			maxnum[i][j] = maxn[i].top().first;
			minnum[i][j] = minn[i].top().first;
		}

	}	
	ll hou = 0;
	ll ou = 0;
	for(int j = b;j<=m;++j){
		for(int i = 1;i<=n;++i){
			maxl[j].push({maxnum[i][j],i});
			minl[j].push({minnum[i][j],i});
			if(i>=a){
				while(maxl[j].top().second<=i-a){
					maxl[j].pop();
				}
				while(minl[j].top().second<=i-a){
					minl[j].pop();
				}
				hou = (ll)maxl[j].top().first*(ll)minl[j].top().first%mod;
				ou = (ou+hou)%mod;
				cout<<hou<<endl;
			}
		}
	}
	cout<<ou;
	return 0;
} 

单调队列

相关推荐
WJSKad12353 分钟前
果园树干识别与定位_faster-rcnn_x101-32x4d_fpn_1x_coco改进实践
python
深蓝电商API3 分钟前
Scrapy中间件实战:自定义请求头和代理池实现
python·scrapy·中间件
hui函数10 分钟前
Python系列Bug修复|如何解决 pip install 安装报错 invalid command ‘bdist_wheel’(缺少 wheel)问题
python·bug·pip
hui函数12 分钟前
Python系列Bug修复|如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
python·bug·pip
子午13 分钟前
【2026原创】动物识别系统~Python+深度学习+人工智能+模型训练+图像识别
人工智能·python·深度学习
o_insist19 分钟前
LangChain1.0 实现 PDF 文档向量检索全流程
人工智能·python·langchain
脑洞AI食验员24 分钟前
智能体来了:用异常与文件处理守住代码底线
人工智能·python
曲幽36 分钟前
FastAPI登录验证:用OAuth2与JWT构筑你的API安全防线
python·fastapi·web·jwt·token·oauth2
幻云201038 分钟前
Next.js指南:从入门到精通
开发语言·javascript·人工智能·python·架构
CCPC不拿奖不改名1 小时前
网络与API:从HTTP协议视角理解网络分层原理+面试习题
开发语言·网络·python·网络协议·学习·http·面试