【bzoj4165】矩阵 堆+STL-map
题目描述
输入
输出
样例输入
3 4 2 2 3
0 1 3 7
1 16 5 2
7 6 9 3
样例输出
19
题解
堆+STL-map
这种类型的题也没少做了,初次写这样的大概是 [NOI2010]超级钢琴 。
由于所有元素非负,因此一个矩形的权值和一定比其任意一个子矩形权值和大。因此只有在处理完子矩形后才处理该矩形。
使用堆维护贪心顺序,初始时把所有长度为Mina,宽度为Minb的矩形加入堆中,每次取堆顶元素,并把该举行左、右、上、下扩展一层所得的矩形加入堆中。
然而这样矩形会计算重复,因此需要使用hash表储存一个矩形是否出现过。
我使用了map,由于常数巨大而垫底...
时间复杂度 $O(nm+k\log k)$
#include <set>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#define N 1010
using namespace std;
typedef pair<int , int> pr;
typedef long long ll;
ll sum[N][N];
struct data
{
int a , b , c , d;
data() {}
data(int w , int x , int y , int z) {a = w , b = x , c = y , d = z;}
bool operator<(const data &x)const {return a == x.a ? b == x.b ? c == x.c ? d < x.d : c < x.c : b < x.b : a < x.a;}
ll query()const {return sum[c][d] - sum[c][b - 1] - sum[a - 1][d] + sum[a - 1][b - 1];}
};
struct cmp
{
bool operator()(const data &x , const data &y)
{
return x.query() > y.query();
}
};
priority_queue<data , vector<data> , cmp> heap;
set<data> s;
inline char nc()
{
static char buf[100000] , *p1 , *p2;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf , 1 , 100000 , stdin) , p1 == p2) ? EOF : *p1 ++ ;
}
inline int read()
{
int ret = 0; char ch = nc();
while(!isdigit(ch)) ch = nc();
while(isdigit(ch)) ret = ((ret + (ret << 2)) << 1) + (ch ^ '0') , ch = nc();
return ret;
}
int main()
{
int n = read() , m = read() , p = read() , q = read() , k = read() , i , j;
ll ans = 0;
data t , tmp;
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= m ; j ++ )
sum[i][j] = read() + sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
for(i = 1 ; i <= n - p + 1 ; i ++ )
for(j = 1 ; j <= m - q + 1 ; j ++ )
t = data(i , j , i + p - 1 , j + q - 1) , heap.push(t) , s.insert(t);
for(i = 1 ; i <= k ; i ++ )
{
if(heap.empty())
{
puts("-1");
return 0;
}
t = heap.top() , heap.pop() , ans = t.query();
if(t.a > 1 && s.find(tmp = data(t.a - 1 , t.b , t.c , t.d)) == s.end()) heap.push(tmp) , s.insert(tmp);
if(t.b > 1 && s.find(tmp = data(t.a , t.b - 1 , t.c , t.d)) == s.end()) heap.push(tmp) , s.insert(tmp);
if(t.c < n && s.find(tmp = data(t.a , t.b , t.c + 1 , t.d)) == s.end()) heap.push(tmp) , s.insert(tmp);
if(t.d < m && s.find(tmp = data(t.a , t.b , t.c , t.d + 1)) == s.end()) heap.push(tmp) , s.insert(tmp);
}
printf("%lld\n" , ans);
return 0;
}
【bzoj4165】矩阵 堆+STL-map的更多相关文章
- bzoj4165 矩阵 堆维护多路归并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4165 题解 大概多路归并是最很重要的知识点了吧,近几年考察也挺多的(虽然都是作为签到题的). ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
随机推荐
- 北京Uber优步司机奖励政策(2月26日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
- 三 Hive 数据处理 自定义函数UDF和Transform
三 Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...
- 代码重复率检查工具jsinspect
检查重复代码,去掉冗余代码. 安装: npm install -g jsinspect 用法:jsinspect [options] <paths ...> 检测复制粘贴和结构类似的Jav ...
- 「日常训练」School Marks(Codeforces Round 301 Div.2 B)
题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...
- 浅谈如何提高自动化测试的稳定性和可维护性 (pytest&allure)
装饰器与出错重试机制 谈到稳定性,不得不说的就是“出错重试”机制了,在自动化测试中,由于环境一般都是测试环境,经常会有各种各种的抽风情况影响测试结果,这样就为测试的稳定性带来了挑战,毕竟谁也不想自己的 ...
- APP性能测试工具-GT(随身调)
GT(随身调)是APP的随身调测平台,它是直接运行在手机上的“集成调测环境”(IDTE, Integrated Debug Environment).利用GT,仅凭一部手机,无需连接电脑,您即可对AP ...
- Micro:bit 硬件架构介绍
Micro:bit做为当红的少儿编程工具,这两年在编程教育领域越来越火.今天就从硬件架构开始,分享Micro:bit的相关主题. Microbit 硬件设计是根据ARM mbed技术所开发的应用IC及 ...
- Struts2(九.利用layer组件实现图片显示功能)
1.layer前端组件介绍 layer是一款口碑极佳的web弹层组件,她具备全方位的解决方案,致力于服务各个水平段的开发人员,您的页面会轻松地拥有丰富而友好的操作体验. http://sentsin. ...
- 实现Bidirectional LSTM Classifier----深度学习RNN
双向循环神经网络(Bidirectional Recurrent Neural Networks,Bi-RNN),Schuster.Paliwal,1997年首次提出,和LSTM同年.Bi-RNN,增 ...