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 ...
随机推荐
- Android Studio无法关联Api23源码-提示Souces for android api 23 platform not found
最近升级了As,然后忽然就关联不上源码了,很不方便,找个Activity的源码都只有outline没有解释,还提示这个错误: Decompiled .class file, bytecode vers ...
- SSH框架常会出现的一些错误
1.jquery datatable插件报JSON数据错误 错误原因:hql语句拼接有问题,前一个字符串与后一个字符串之间缺少空格,导致数据库查询失败. 2.addInput页面中input内容不为空 ...
- androik_sdk 更新慢问题解决办法。
在windows中更改hosts(找到文件夹C:\Windows\System32\drivers\etc找到里面的hosts文件 )在这里添加74.125.237.1 dl-ssl.google.c ...
- EventBusException:xxxx has no methods onEvent
使用第三方框架EventBus,在register时出现Exception:xxxx has no methods onEvent. 场景:在Activity中没有接收事件,只是需要发送事件,但是有对 ...
- 在windows平台下忘记了root的密码如何解决?
1.打开MySQL配置文件 my.ini中,添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面. 2.然后重启MYSQL服务 windows环境中: n ...
- aspnetpager分页,不使用存储过程
一.前台显示界面代码Default.aspx(注意,代码运行环境是VS.2005) <%@ Page Language="C#" AutoEventWireup=" ...
- jquery实现div垂直居中
<html> <head> <meta charset="UTF-8"> <title></title> <scr ...
- bootstrap 简易模版
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Scrapy的架构初探
Scrapy,Python开发的一个web抓取框架. 1,引言 Python即时网络爬虫启动的目标是一起把互联网变成大数据库.单纯的开放源代码并不是开源的全部,开源的核心是“开放的思想”,聚合最好的想 ...
- ffmpeg在Win7 VS2010中debug通过,release出错的问题解决方法
我所用的系统环境是Win7 32位操作系统+VS2010编译环境.所以在debug模式下调通之后,在Release模式下调试不通过,最后通过上网查资料和自己对比两个编译选项得出以下结论: 修改“项目- ...