当前位置:网站首页>E. OpenStreetMap(二维单调队列)
E. OpenStreetMap(二维单调队列)
2022-07-19 01:50:00 【to cling】
Codeforces Round #574 (Div. 2)
Problem
给定一个大小为 n × m n \times m n×m 的矩阵h, 求出所有大小为 a × b a \times b a×b 的子矩阵中最小值的和。
Solution
题解
求出h中,所有行中长度为b的滑动窗口中的最小值,将其结果存入f数组
求出f中, 所有列中长度为a的滑动窗口中的最小值,其结果即为所有大小为 a × b a \times b a×b 的子矩阵中的最小值
时间复杂度 O ( n m ) O(nm) O(nm)
Code
int h[3003][3003], g[3003 * 3003];
int f[3003][3003];
int main()
{
IOS;
int n, m, a, b; cin >> n >> m >> a >> b;
int x, y, z; cin >> g[0] >> x >> y >> z;
for (int i = 1; i <= n * m; i++)
g[i] = ((ll)g[i - 1] * x + y) % z;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
h[i][j] = g[(i - 1) * m + j - 1];
//求出所有行中的长度为b的窗口中的最小值
for (int i = 1; i <= n; i++)
{
//操作数组为h
deque <int> q;
for (int j = 1; j <= m; j++)
{
if (!q.empty() && q.back() - q.front() + 1 >= b) q.pop_front();
while (!q.empty() && h[i][q.back()] >= h[i][j]) q.pop_back();
q.push_back(j);
f[i][j] = h[i][q.front()];
}
}
//求出所有列中的长度为a的窗口中的最小值(实质为a * b的矩阵的最小值)
for (int j = 1; j <= m; j++)
{
//操作数组为f
deque<int> q;
for (int i = 1; i <= n; i++)
{
if (!q.empty() && q.back() - q.front() + 1 >= a) q.pop_front();
while (!q.empty() && f[q.back()][j] >= f[i][j]) q.pop_back();//
q.push_back(i);
h[i][j] = f[q.front()][j];//此处为优化空间,所以直接用h来存
}
}
ll ans = 0;
for (int i = a; i <= n; i++)
for (int j = b; j <= m; j++)
ans += h[i][j];
cout << ans << endl;
return 0;
}
边栏推荐
- 详解的wc find xargs zip gzip bzip2 xz tar sftp命令或者协议
- 这几款实用的安全浏览器插件,让你效率提高
- Conditions and details of polar coordinate substitution for solving the limit of multivariate functions with high numbers
- Live broadcast today | Apache pulsar meetup: vivo, Tencent cloud, bigo, Yunxing technology practice sharing
- tsconfig常用配置全解
- 走进企业系列 |StreamNative x 众安保险
- [leetcode daily question] - 109 Ordered linked list transformation binary search tree
- c语言---程序环境与预处理
- &lt;/ script&gt;& lt; script&gt; console. log(7890)-{&quot; xxx&quot; :&quot; aaa
- MySQL 事务
猜你喜欢
随机推荐
Kaggle注册方法,解决人机验证问题
认证培训|StreamNative Certification 培训第 2 期
走进企业系列 |StreamNative x 众安保险
Sovit3D快速开发物联网智慧农业三维可视化系统
MySQL中的二进制日志
基于yarn1.x的monorepo实践分享
Yum install mysql 常见问题
Introduce you to C language custom types - structure, enumeration, union
codeforces每日5题(均1500)-第十九天
自己用U++写的植物大战僵尸豌豆射手部分逻辑记录
【C语言刷LeetCode】1462. 课程表 IV(M)
力扣每日一题-第39天-67. 二进制求和
【论文阅读】CoaT:Co-Scale Conv-Attentional Image Transformers
468-82(142、199、509、70、746)
[leetcode daily question] - 109 Ordered linked list transformation binary search tree
Time flies.
18_过滤器
VLAN aggregation
这几款实用的安全浏览器插件,让你效率提高
B tree b+ tree