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

单调队列

相关推荐
橡晟3 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子3 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba4 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
Rvelamen5 小时前
LLM-SECURITY-PROMPTS大模型提示词攻击测评基准
人工智能·python·安全
【本人】5 小时前
Django基础(一)———创建与启动
后端·python·django
SHIPKING3936 小时前
【python】基于pygame实现动态粒子爱心
开发语言·python·pygame
kk_stoper8 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源
java1234_小锋8 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 架构搭建
python·自然语言处理·flask