子矩阵(十四届蓝桥杯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;
} 

单调队列

相关推荐
小二·8 分钟前
Python Web 开发进阶实战:数字孪生平台 —— 在 Flask + Vue 中构建实时物理世界镜像
前端·vue.js·python
2501_9421584312 分钟前
服务设计从成本到利润引擎的重构
大数据·python·重构
23124_8016 分钟前
热身签到-ctfshow
开发语言·python
小白学大数据27 分钟前
移动端Temu App数据抓包与商品爬取方案
开发语言·爬虫·python
2501_9445264234 分钟前
Flutter for OpenHarmony 万能游戏库App实战 - 知识问答游戏实现
android·开发语言·javascript·python·flutter·游戏·harmonyos
人工智能AI技术1 小时前
【Agent从入门到实践】21 Prompt工程基础:为Agent设计“思考指令”,简单有效即可
人工智能·python
2501_941322031 小时前
铆钉表面缺陷检测:YOLO13-BiFPN模型实现与性能优化_1
python
CCPC不拿奖不改名1 小时前
python基础面试编程题汇总+个人练习(入门+结构+函数+面向对象编程)--需要自取
开发语言·人工智能·python·学习·自然语言处理·面试·职场和发展
我送炭你添花1 小时前
Pelco KBD300A 模拟器:10.报警联动规则编辑与执行
python·自动化·运维开发
眼眸流转2 小时前
MCP学习笔记
python·uv·pydantic·mcp