poj_3179 Corral the Cows (二分+二维前缀和+离散化)
【题目链接】
http://poj.org/problem?id=3179
【参考】
http://www.cnblogs.com/evenbao/p/9243183.html
【算法】
- 二分答案+判定
- 二维坐标的离散化去除不存在草的行和列
- 二维前缀和
- lower_bound (>=) upper_bound (>)
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct data{int x,y;}a[];
int c,n,tot,i,j,tx,ty;
int rec[][],b[];
void discrete()
{
sort(b+, b+tot+);
tot = unique(b+, b+tot+) - b - ;
for(i = ; i <= n; i++) {
tx = lower_bound(b+,b+tot+,a[i].x) - b;
ty = lower_bound(b+,b+tot+,a[i].y) - b;
rec[tx][ty]++;
}
b[++tot] = ;
for(i = ; i <= tot; i++)
for(j = ; j <= tot; j++)
rec[i][j] = rec[i-][j] + rec[i][j-] - rec[i-][j-] + rec[i][j];
}
bool valid(int cur)
{
int p;
if(cur >= b[tot]) { return true; }
p = upper_bound(b+, b+tot+, b[tot]-cur+) - b - ;
for(i = ; i <= p; i++) {
for(j = ; j <= p; j++) {
tx = upper_bound(b+, b+tot+, b[i]+cur-) - b - ;
ty = upper_bound(b+, b+tot+, b[j]+cur-) - b - ;
if(rec[tx][ty] - rec[i-][ty] - rec[tx][j-] + rec[i-][j-] >= c) return true;
}
}
return false;
}
int main()
{
scanf("%d%d", &c, &n);
for(i = ; i <= n; i++)
scanf("%d%d", &a[i].x, &a[i].y),
b[++tot] = a[i].x, b[++tot] = a[i].y;
discrete();
int l = , r = ;
while(l<r) {
int mid = (l + r) >> ;
if(valid(mid)) r = mid;
else l = mid + ;
}
printf("%d\n", l);
return ;
}
poj_3179 Corral the Cows (二分+二维前缀和+离散化)的更多相关文章
- Acwing-121-赶牛入圈(二分, 二维前缀和,离散化)
链接: https://www.acwing.com/problem/content/123/ 题意: 农夫约翰希望为他的奶牛们建立一个畜栏. 这些挑剔的畜生要求畜栏必须是正方形的,而且至少要包含C单 ...
- Gym 102091L Largest Allowed Area 【二分+二维前缀和】
<题目链接> 题目大意:给你一个由01组成的矩形,现在问你,该矩形中,最多只含一个1的正方形的边长最长是多少. 解题分析: 用二维前缀和维护一下矩形的01值,便于后面直接$O(1)$查询任 ...
- cf1073c 二分+二维前缀和
#include<bits/stdc++.h> using namespace std; #define maxn 200005 char s[maxn]; ][maxn]; map< ...
- Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)
题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...
- [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)
题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...
- C - Monitor CodeForces - 846D (二维前缀和 + 二分)
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...
- openjudge1768 最大子矩阵[二维前缀和or递推|DP]
总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...
- COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)
题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...
- poj-3739. Special Squares(二维前缀和)
题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...
随机推荐
- 【前端】CSS基础
3种html中设置css的方法: head中style标签 head中link标签rel属性.href后跟css文件目录 在需要使用css样式的标签添加style属性 1.CSS选择器 #id值 .c ...
- AngualJS-leaflet之视图等级缩放
在http://tombatossals.github.io/angular-leaflet-directive/#!/examples/events 中的则是zoomlevelschange,然后识 ...
- mongdb 数据库
安装mongdb 下载地址 https://www.runoob.com/mongodb/mongodb-window-install.html 检查 mongdb 是否安装成功which mongd ...
- SpringBoot的启动简述
一.注解和启动类SpringBootApplication 它是一个复式注解. @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME ...
- 文件打包压缩——tar
tar——压缩数据/解压数据内容 命令语法: tar zcvf 生成压缩包路径/压缩包.tar.gz 压缩数据01,02,03.... 巧记: 压缩名称为tar.gz,可以理解为tar命令,g ...
- 阿里云服务器不能使用apt-get
因为阿里云使用的是自己的源.所以在/etc/apt/sources.list中加上: deb cdrom:[Ubuntu 16.04.3 LTS _Xenial Xerus_ - Release am ...
- php array_keys()函数 语法
php array_keys()函数 语法 作用:返回包含数组中所有键名的一个新数组.直线电机选型 语法:array_keys(array,value,strict) 参数: 参数 描述 array ...
- BigDecimal.setScale 处理java小数点[转]
BigDecimal.setScale()方法用于格式化小数点setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOWN)直接删除多余 ...
- 转:SqlBulkCopy类进行大数据(一万条以上)插入测试
转自:https://www.cnblogs.com/LenLi/p/3903641.html 结合博主实例,自己测试了一下,把数据改为3万行更明显!! 关于上一篇博客中提到的,在进行批量数据插入数据 ...
- CMakeLists.txt 语法
命令不区分大小写(参数区分大小写) add_executable(demo main.cpp main.h main.rc) 用main.cpp源文件,main.h文件,main.rc文件构造可执行文 ...