【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的快速检索能力. ...
随机推荐
- linux下使用汇编语言编写hello world!程序
最近公司需要完成安全方面的测试,随之带来需要更深入地学习攻击方法和漏洞分析的技术,总感觉有点像黑客:),不过不能只知道一些安全测试工具的方法和工具的使用,更需要基础功夫,首先从大学学过的汇编语言(呵呵 ...
- 洛谷P2252 取石子游戏(威佐夫博弈)
题目背景 无 题目描述 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 优步UBER司机全国各地奖励政策汇总 (2月1日-2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(3月9日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- vim 安装
Ubuntu 16.04 下 Vim安装及配置 默认已经安装了VIM-tiny linuxidc@linuxidc:~$ locate vi | grep 'vi$' |xargs ls -al lr ...
- hdu1455Sticks(经典dfs+剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Python序列及其操作(常见)
python序列及函数入门认识: 0. 我们根据列表.元组和字符串的共同特点,把它们三统称为什么? 序列,因为他们有以下共同点: 1)都可以通过索引得到每一个元素 2)默认索引值总是从0开始(当 ...
- Swift使用AVAudioPlayer来调节游戏的背景音乐大小
音乐文件的声音大小有时在做为游戏背景音乐时会过大,而如果我们只是简单应用SKAudioNode来加载音乐的话,是无法进行声音大小的调节的,因此我们必须使用更强大的AVAudioPlayer来进行声音大 ...
- leetcode-最长无重复字符的子串
参考他的人代码:https://blog.csdn.net/littlebai07/article/details/79100081 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: ...