Codeforces 846D Monitor(简单二分+二维BIT)
2 seconds
256 megabytes
standard input
standard output
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).
The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.
Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.
We consider that pixel is already broken at moment ti.
Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2
8
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100
-1
题目链接:CF 846D
显然二分一下时间,然后把小于等于二分时间t的坏点坐标加入到二维矩阵中,然后暴力枚举k*k矩阵的右下角端点看是否存在矩阵的和刚好为k*k,若存在则说明这一块全坏了,看范围懂做法的水题。。。
代码:
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 510;
int T[N][N];
struct info
{
int x, y, t;
bool operator<(const info &rhs)const
{
return t < rhs.t;
}
};
inline int lowbit(const int &x)
{
return x & (-x);
}
info arr[N * N];
int n, m, k, q;
int kk; inline void add(int x, int y)
{
for (int i = x; i < N; i += lowbit(i))
for (int j = y; j < N; j += lowbit(j))
T[i][j] += 1;
}
inline sum(int x, int y)
{
int r = 0;
for (int i = x; i > 0; i -= lowbit(i))
for (int j = y; j > 0; j -= lowbit(j))
r += T[i][j];
return r;
}
inline bool check(int t)
{
CLR(T, 0);
int i, j;
for (i = 0; i < q; ++i)
{
if (arr[i].t <= t)
{
add(arr[i].x, arr[i].y);
}
else
break;
}
for (i = k; i <= n; ++i)
{
for (j = k; j <= m; ++j)
{
int area = sum(i, j) - sum(i - k, j) - sum(i, j - k) + sum(i - k, j - k);
if (area == kk)
return 1;
}
}
return 0;
}
int main(void)
{
int i;
while (~scanf("%d%d%d%d", &n, &m, &k, &q))
{
int L = 0, R = 0;
kk = k * k;
for (i = 0; i < q; ++i)
{
scanf("%d%d%d", &arr[i].x, &arr[i].y, &arr[i].t);
R = max(R, arr[i].t);
}
sort(arr, arr + q);
int ans = -1;
while (L <= R)
{
int mid = MID(L, R);
if (check(mid))
{
ans = mid;
R = mid - 1;
}
else
L = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
Codeforces 846D Monitor(简单二分+二维BIT)的更多相关文章
- 使用C语言实现二维,三维绘图算法(3)-简单的二维分形
使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...
- [BZOJ2738]矩阵乘法(整体二分+二维树状数组)
整体二分+二维树状数组. 好题啊!写了一个来小时. 一看这道题,主席树不会搞,只能用离线的做法了. 整体二分真是个好东西,啥都可以搞,尤其是区间第 \(k\) 大这种东西. 我们二分答案,然后用二维树 ...
- VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例
一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...
- 【bzoj2738】矩阵乘法 整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5个数 ...
- 猫狗大战("简单的二维背包")
题面:https://www.luogu.com.cn/problem/P1489 看上去是一道简单的二维费用背包,但是要特别小心循环顺序. Ⅰ先循环物品,再循环限制条件. Ⅱ每一个限制条件都必须从后 ...
- codeforces 713D D. Animals and Puzzle 二分+二维rmq
题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
- c#简单实现二维数组和二维数组列表List<>的转置
刚看到网上一篇文章里用sql实现了行列转置.sql server 2005/2008只用一个pivot函数就可以实现sql server 2000很多行的复杂实现.提到转置,立刻想起还在求学阶段曾经做 ...
- BZOJ2738矩阵乘法——整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5 ...
随机推荐
- Angular/cli的安装
Angular cli 是一个命令行工具,用来创建,打包,发布项目. Angular cli 安装之前必须先安装Node 6.9.0及以上版本,NPM 3 及以上版本. 在cmd控制台窗口执行命令no ...
- 7-3 python操作excel
1.写excel 写入特定单元格数据 # .导入xlwt模块 # .新建一个excel # .添加一个sheet页 # .往指定的单元格中写入数据 # .保存excel import xlwt boo ...
- ES6笔记03-Set和Map数据结构
ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值.Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, 3, 5, 4, ...
- ethereum(以太坊)(十三)--异常处理/元祖
pragma solidity ^0.4.4; contract Students{ uint[] data= new uint[](4); address _owner = msg.sender; ...
- PHP关闭notice级别的错误提示
1.在php.ini文件中改动error_reporting改为: error_reporting=E_ALL & ~E_NOTICE 2.如果你不能操作php.ini文件,你可以使用如下方法 ...
- zookeeper的搭建方法
1.创建三台虚拟机分别在虚拟机上安装Ubuntu16.04Server版的系统. 2.首先选择配置好第一台虚拟机,使用命令vim /etc/hosts对该文件进行修改 3.将zookeeper-3.4 ...
- Python学习第一弹
开发语言: 高级:Python.java.PHP C# GO ruby C++ ——>字节码 低级:C.汇编 ...
- Requests库:python实现的简单易用的http库
1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...
- cookie操作和代理
cookie操作 爬取豆瓣个人主页 # -*- coding: utf-8 -*- import scrapy class DoubanSpider(scrapy.Spider): name = 'd ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...