【题目链接】

      http://poj.org/problem?id=3179

  【参考】

      http://www.cnblogs.com/evenbao/p/9243183.html

  【算法】

  1. 二分答案+判定
  2. 二维坐标的离散化去除不存在草的行和列
  3. 二维前缀和
  4. 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 (二分+二维前缀和+离散化)的更多相关文章

  1. Acwing-121-赶牛入圈(二分, 二维前缀和,离散化)

    链接: https://www.acwing.com/problem/content/123/ 题意: 农夫约翰希望为他的奶牛们建立一个畜栏. 这些挑剔的畜生要求畜栏必须是正方形的,而且至少要包含C单 ...

  2. Gym 102091L Largest Allowed Area 【二分+二维前缀和】

    <题目链接> 题目大意:给你一个由01组成的矩形,现在问你,该矩形中,最多只含一个1的正方形的边长最长是多少. 解题分析: 用二维前缀和维护一下矩形的01值,便于后面直接$O(1)$查询任 ...

  3. cf1073c 二分+二维前缀和

    #include<bits/stdc++.h> using namespace std; #define maxn 200005 char s[maxn]; ][maxn]; map< ...

  4. Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)

     题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...

  5. [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)

    题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...

  6. C - Monitor CodeForces - 846D (二维前缀和 + 二分)

    Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...

  7. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

  8. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  9. poj-3739. Special Squares(二维前缀和)

    题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...

随机推荐

  1. 配置apache运行cgi程序

    配置apache运行cgi程序 文章目录 [隐藏] ScriptAlias目录的CGI ScriptAlias目录以外的CGI 配置apache运行cgi程序可分为两种情况,一是ScriptAlias ...

  2. QQ输入法用户评价

    1.用户界面 用户界面简洁,并且可以随用户喜好自己更换,人性化,优化性比较大 2.记住用户选择 当输入一个字时,下一次输入这个拼音第一位的字就是上一次,或者使用次数最多的字.假如所使用的的字在后边,输 ...

  3. sqlite3 on python for newbies

    python 集成了 sqlite3 ,其接口很简单: import sqlite3 db_connection = sqlite3.connect(db_filename) db_cursor = ...

  4. layui jquery ajax,url,type,async,dataType,data

    $.ajax({ url: '/foensys/user/userDelete/'+data[0].id, type:"get", async:true, dataType:&qu ...

  5. ORM详解,ORM Object relation mapping (对象关系映射)

  6. 超好用json转excel工具

    给大家安利一个超实用的json数据转excel工具:http://www.yzcopen.com/doc/jsonexcel

  7. Bugku 杂项 又一张图片,还单纯吗

    又一张图片,还单纯吗 下载后,用binwalk打开图片 使用foremost 2.png进行分离 得到图片 关于foremost foremost [-v|-V|-h|-T|-Q|-q|-a|-w-d ...

  8. 获取浏览区变化的方法resize() 方法

    当调整浏览器窗口的大小时,发生 resize 事件. resize() 方法触发 resize 事件,或规定当发生 resize 事件时运行的函数. <html> <head> ...

  9. WIN10系统和压缩内存占用磁盘过高的解决方案(亲测有效)

    系统和压缩内存磁盘占用过高的解决方案 最近一段时间玩游戏看视频经常莫名的卡顿,观察发现电脑卡的时候,WIN10进程中的"系统和压缩内存"一项占用了近100%的磁盘空间. 百度搜索到 ...

  10. Linux shell -查找字符(find,xargs,grep)

    在当前目录下查找含有jmxremote字符的文件 test@>find . -type f|xargs grep "jmxremote" . 当前目录 -type 查找文件类 ...