codeforces 15D . Map 优先队列
题目链接
题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数。
选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置。
输出所有矩阵的左上角那个数的坐标以及这个矩阵的值。
思路很简单, 将所有矩阵的值加到一个优先队列里面, 然后一个一个的删除。 直到矩阵空。
不好做的是求一个矩阵中的最小值。 我先是用二维线段树, tle。 然后用二维st表, mle....
然后看cf上的代码 ,用一个multiset来搞。 具体看代码..
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1002;
int vis[maxn][maxn], n, m, c[maxn][maxn], d[maxn][maxn];
ll sum[1002][1002];
struct node
{
ll val;
int x, y;
bool operator < (node a) const{
if(val != a.val)
return val>a.val;
if(x != a.x)
return x>a.x;
return y>a.y;
}
node() {}
node(ll _val, int _x, int _y):val(_val), x(_x), y(_y){}
};
vector <node> ans;
int main()
{
int a, b;
priority_queue <node> q;
cin>>n>>m>>a>>b;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
scanf("%I64d", &sum[i][j]);
c[i][j] = sum[i][j];
}
}
for(int i = 1; i <= n; i++) {
multiset <int> tmp;
for(int j = 1; j <= m; j++) {
if(j>b) {
tmp.erase(tmp.find(c[i][j-b]));
}
tmp.insert(c[i][j]);
d[i][j] = *(tmp.begin());
}
}
for(int j = 1; j <= m; j++) {
multiset <int> tmp;
for(int i = 1; i <= n; i++) {
if(i>a) {
tmp.erase(tmp.find(d[i-a][j]));
}
tmp.insert(d[i][j]);
c[i][j] = *(tmp.begin());
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
sum[i][j] += sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
}
for(int i = 1; i <= n-a+1; i++) {
for(int j = 1; j <= m-b+1; j++) {
int x = i+a-1;
int y = j+b-1;
ll num = sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
ll res = num - 1LL*c[x][y]*a*b;
q.push(node(res, i, j));
}
}
while(!q.empty()) {
node tmp = q.top(); q.pop();
if(vis[tmp.x][tmp.y])
continue;
ans.pb(tmp);
for(int i = max(1, tmp.x-a+1); i <= min(tmp.x+a-1, n); i++) {
for(int j = max(tmp.y-b+1, 1); j <= min(tmp.y+b-1, m); j++) {
vis[i][j] = 1;
}
}
}
cout<<ans.size()<<endl;
sort(ans.begin(), ans.end());
reverse(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++) {
printf("%d %d %I64d\n", ans[i].x, ans[i].y, ans[i].val);
}
return 0;
}
codeforces 15D . Map 优先队列的更多相关文章
- CodeForces 15D Map
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译.(注意翻译里有错误,应该是优先选上面的矩阵,在同一行的优先选左边的矩阵) 这题一看就会做啊 (以下设大矩阵是\( ...
- CodeForces 912d fishes(优先队列+期望)
While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...
- CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)
题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...
- codeforces 446B(优先队列)
题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- STL之容器(1)
STL容器类的模板 容器部分主要由头文件<vector>,<list>,<deque>,<set>,<map>,<stack>和 ...
- [luogu1110][报表统计]
题目链接 思路 set+map+优先队列就可以水过去.可以发现,每插入一个元素,都会使得操作2中原来相邻的那个差值消失,然后多了两个新的差值.对于新的差值,只要直接扔到优先队列里就好了.那么删除呢.可 ...
- gym 101911
A. Coffee Break 题意:每天有m小时,你喝咖啡需要花一小时,你想在n个时刻都喝过一次咖啡,老板规定连续喝咖啡的间隔必须是d以上,求最少需要多少天才能喝够n次咖啡,并输出每个时刻第几天喝. ...
- 通过Jedis操作Redis
package com.yh; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis ...
随机推荐
- oracle 定义数据完整性
1. 定义主键约束 1.1 在创建表时定义主键约束 create table student(name varchar2(8),studentid varchar2(10) primary key,s ...
- 解决jquery zclip 插件点击无效的问题
使用jquery zclip 用于页面复制文本内容. 首先引入js <script type="text/javascript" src="../js/jquery ...
- Oracle EBS-SQL (WIP-6):检查任务已完成但状态是发放的任务.sql
select WE.WIP_ENTITY_NAME ,MSI.SEGMENT1 ,MSI.DESCRIPTION ,WDJ.CLASS_CODE ...
- Compiling Qt 5.5.1 (With Qtwebkit) With Visual Studio 2015
I usually avoid writing articles about building a specific version of a software project but this ti ...
- delphi 读网页线程TReadHtmlThread
读网页,通常是一个耗时操作.故把读网页放入线程是显得比较重要了. 本例用改进后的 TIdhttpEx 加上线程来实现读网页. 它的父类TSimpleThread 在此 本例程源码在此 源码中包含了所有 ...
- 使用Task简化Silverlight调用Wcf
原文http://www.cnblogs.com/lemontea/archive/2012/12/09/2810549.html 从.Net4.0开始,.Net提供了一个Task类来封装一个异步操作 ...
- 第52周二Restful
今天去spring官网发现一个关键词:Restful,以前只在与一个系统对接时用到过这种形式的接口,但印象不深,百度搜索后才感觉自己太out了,这个概念2000年提出,2009年时国内就有很多人推荐使 ...
- mysql允许远程IP访问
默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: mysql> Grant all privileges on * ...
- poj3006
Dirichlet's Theorem on ...
- paip.QQ音乐导出歌单总结
paip.QQ音乐导出歌单总结 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...