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

单调队列

相关推荐
最强小杰17 分钟前
Claude Opus 4.8 调用报 529 怎么办?并发限制触发过载——退避策略与降级方案完整指南
运维·网络·python·ai
databook31 分钟前
MinMax vs Standard vs Robust:三种特征缩放怎么选
python·机器学习·数据分析
在世修行33 分钟前
智学文枢:实体模型层
python·orm·sqlalchemy
张张123y1 小时前
# 后端第七课:FastAPI 实战入门(用刚学的 Python 写第一个 API 接口)
开发语言·python·mysql·api·fastapi
CCblandy1 小时前
一致性哈希到底省了多少搬迁?本机实测 3 轮:取模 6→7 搬 85.8%,V=128 只搬 14.3%,虚拟节点太少会被倾斜骗
后端·python·云原生·性能优化·架构
Yan-英杰1 小时前
【论文精读】 基于 TensorFlow 对卷积神经网络的优化研究:异构式 CPU+GPU 协同计算 + ReLU-Swish 激活函数
python·卷积神经网络
ydjs678899-bot1 小时前
如何在Java中打印pdf静默打印和打印对话框选择
java·python·pdf
把我格子衫拿来2 小时前
误差预算怎么做?从雅可比到蒙特卡洛的三维定位不确定度分析
python·蒙特卡洛·三维定位·误差分析·不确定度
文章永久免费只为良心2 小时前
Ctfshow_Web应用安全与防护_第二章_无字母数字命令执行
前端·python·安全·网络安全·渗透测试·ctf·红队渗透测试
2601_962885723 小时前
如何用 Python 获取上证指数、沪深300、中证500 等指数行情?
开发语言·python