【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的快速检索能力. ...
随机推荐
- 2017Noip普及组游记
Day0 一天都基本在休息,早上信心赛,大家都是400整. 下午一群人窝在教室里打三国杀. Day1:Before Contest 早上大约十点到了试场,在考提高组,不能进. 喝了一杯咖啡去除早起的身 ...
- jsp传递参数的四种方法
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- 一个体验好的Windows 任务栏缩略图开发心得
本文来自网易云社区 作者:孙有军 前言: 对于一个追求极致体验的软件来说,利用好系统的每一点优秀的特性,将会大大提高软件的品质. Windows vista以来任务栏缩略图,及Win + TAB的程序 ...
- nested class 例子
#include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclo ...
- 「功能笔记」性能分析工具gprof使用笔记
根据网上信息整理所成. 功能与优劣 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,它使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...
- selenium--特殊元素定位
该篇博客总结特殊元素(select.radio\checkbox.时间控件.文件上传.图片验证码.模拟鼠标操作.Js 或 JQuery调用)操作. 1.select @Test public void ...
- Web应用服务器性能压力测试
压力测试需要关注三个方面:如何正确产生压力.如何定位瓶颈.如何预估系统的承载能力 产生压力的方法 通常可以写脚本产生压力机器人对服务器进行发包和收包操作,也可以使用现有的工具(像jmeter.Load ...
- Python字典操作大全
//2018.11.6 Python字典操作 1.对于python编程里面字典的定义有以下几种方法: >>> a = dict(one=1, two=2, three=3) > ...
- Maxscript-获取选中文件
Maxscript - 获取选中文件 使用 .Net 的方法弹出窗口选择文件,并范围所有选中文件的路径“” Fn Fun_GetFilePaths strTitle strFilter = ( dia ...
- nginx 重启报错
错误信息: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or d ...