题目链接

题目意思很简单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 优先队列的更多相关文章

  1. CodeForces 15D Map

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译.(注意翻译里有错误,应该是优先选上面的矩阵,在同一行的优先选左边的矩阵) 这题一看就会做啊 (以下设大矩阵是\( ...

  2. CodeForces 912d fishes(优先队列+期望)

    While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...

  3. CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)

    题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...

  4. codeforces 446B(优先队列)

    题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...

  5. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  6. STL之容器(1)

    STL容器类的模板 容器部分主要由头文件<vector>,<list>,<deque>,<set>,<map>,<stack>和 ...

  7. [luogu1110][报表统计]

    题目链接 思路 set+map+优先队列就可以水过去.可以发现,每插入一个元素,都会使得操作2中原来相邻的那个差值消失,然后多了两个新的差值.对于新的差值,只要直接扔到优先队列里就好了.那么删除呢.可 ...

  8. gym 101911

    A. Coffee Break 题意:每天有m小时,你喝咖啡需要花一小时,你想在n个时刻都喝过一次咖啡,老板规定连续喝咖啡的间隔必须是d以上,求最少需要多少天才能喝够n次咖啡,并输出每个时刻第几天喝. ...

  9. 通过Jedis操作Redis

    package com.yh; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis ...

随机推荐

  1. poj 2431

    大意: 有n个加油点,给出每个加油点距离终点的位置和能加多少油,最后一行给出总长度和最初的油量.求最少加几次油能到终点,不能到的话输出-1. Sample Input 4 4 4 5 2 11 5 1 ...

  2. C# DropDownList绑定添加新数据的几种方法

    第一种:在前台手动绑定(适用于固定不变的数据项) <asp:DropDownList ID="DropDownList1" runat="server"& ...

  3. Cordova了解

    概述 Cordova让我们可以使用HTML.JS以及CSS开发跨平台App的公共部分(整个App就是一个WebView或者或者嵌入到本地组件中),与原生API的交互通过Cordova插件实现. 安装配 ...

  4. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  5. EC读书笔记系列之19:条款49、50、51、52

    条款49 了解new-handler的行为 记住: ★set_new_handler允许客户指定一个函数,在内存分配无法获得满足时被调用 ★Nothrow new是一个颇为局限的工具,∵其只适用于内存 ...

  6. MySQL取得当前时间的函数是什么 格式化日期的函数是什么

    取得当前时间用 now() 就行.在数据库中格式化时间 用DATE_FORMA T(date, format) .根据格式串format 格式化日期或日期和时间值date,返回结果串. 可用DATE_ ...

  7. js 中的for...in循环

    in:其左边是一个字符串或可以转换成字符串,右边是一个对象或数组 例:var person={firstname:"Bob", lastname:"Kin"}; ...

  8. Canvas、Paint、的简单使用及辅助类(Path、Shader、简介)

    1.Canvas类 作用:1.绘制各种图形.图片.字等.2.提供各种方法进行坐标变换(rotate.scale.skew.translate) 获取Canvas:一般是子类继承View并重写onDra ...

  9. python 学习day5(模块)

    一.模块介绍 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能 ...

  10. cdoj 791 Frozen Rose-Heads

    //本来想做白书上一题 结果发现又要二染色 又要dp的 想了两个小时没想通 然后做了个傻逼题安慰自己 解:不多说,就是递归到叶节点,然后回来的时候在解决子树和直接删边的代价中间取个最小值 #inclu ...