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

单调队列

相关推荐
weixin_452159556 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
Hello.Reader8 分钟前
面向 403 与域名频繁变更的合规爬虫工程实践以 Libvio 系站点为例
爬虫·python·网络爬虫
深蓝海拓21 分钟前
PySide6从0开始学习的笔记(二十五) Qt窗口对象的生命周期和及时销毁
笔记·python·qt·学习·pyqt
Dfreedom.31 分钟前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
2301_7903009635 分钟前
用Python读取和处理NASA公开API数据
jvm·数据库·python
葱明撅腚1 小时前
利用Python挖掘城市数据
python·算法·gis·聚类
Serendipity_Carl1 小时前
1637加盟网数据实战(数分可视化)
爬虫·python·pycharm·数据可视化·数据清洗
流㶡1 小时前
网络爬虫之requests.get() 之爬取网页内容
python·数据爬虫
yuankoudaodaokou1 小时前
高校科研新利器:思看科技三维扫描仪助力精密研究
人工智能·python·科技
言無咎1 小时前
从规则引擎到任务规划:AI Agent 重构跨境财税复杂账务处理体系
大数据·人工智能·python·重构