【POJ 3179】 Corral the Cows
【题目链接】
http://poj.org/problem?id=3179
【算法】
首先,我们发现答案是具有单调性的,也就是说,如果边长为C的正方形可以,那么比边长C大的正方形也可以,因此,可以二分答案
那么,我们怎么检验呢?
每个点的坐标最大时达到10000,因此,直接二维前缀和显然是会超时的
考虑将坐标离散化,然后求二维前缀和,由于N<=500,所以离散化后最多也只有1000个点
检验时,我们枚举正方形的左上角,用二分求出它的右下角,然后,判断正方形内是否有大于C的草量
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 510 int C,N,i,j,l,r,mid,ans,tx,ty,len;
int tmp[MAXN<<],s[MAXN<<][MAXN<<],x[MAXN],y[MAXN]; inline int getsum(int xa,int ya,int xb,int yb)
{
return s[xb][yb] - s[xa-][yb] - s[xb][ya-] + s[xa-][ya-];
}
inline bool check(int x)
{
int i,j,tx,ty,pos;
if (x > tmp[len])
{
if (s[len][len] >= C) return true;
else return false;
}
pos = upper_bound(tmp+,tmp+len+,tmp[len]-x+) - tmp - ;
for (i = ; i <= pos; i++)
{
for (j = ; j <= pos; j++)
{
tx = upper_bound(tmp+,tmp+len+,tmp[i]+x-) - tmp - ;
ty = upper_bound(tmp+,tmp+len+,tmp[j]+x-) - tmp - ;
if (getsum(i,j,tx,ty) >= C) return true;
}
}
return false;
} int main()
{ scanf("%d%d",&C,&N);
for (i = ; i <= N; i++)
{
scanf("%d%d",&x[i],&y[i]);
tmp[++len] = x[i];
tmp[++len] = y[i];
}
sort(tmp+,tmp+len+);
len = unique(tmp+,tmp+len+) - tmp - ;
for (i = ; i <= N; i++)
{
tx = lower_bound(tmp+,tmp+len+,x[i]) - tmp;
ty = lower_bound(tmp+,tmp+len+,y[i]) - tmp;
s[tx][ty]++;
}
tmp[++len] = ;
for (i = ; i <= len; i++)
{
for (j = ; j <= len; j++)
{
s[i][j] = s[i-][j] + s[i][j-] - s[i-][j-] + s[i][j];
}
}
l = ; r = ;
while (l <= r)
{
mid = (l + r) >> ;
if (check(mid))
{
r = mid - ;
ans = mid;
} else l = mid + ;
}
printf("%d\n",ans); return ; }
【POJ 3179】 Corral the Cows的更多相关文章
- 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)
Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- Percona-Server-5.7.16 启动错误
基于:percona-server-5.7.16 启动报错: [root@monitor mysql]# ./bin/mysqld_safe --defaults-file=/data/config ...
- 配置windows qt开发环境
1.解压缩MinGW-gcc440_1.zip.将解压后的文件夹复制至C盘根目录下.2.安装qt-creator-win-opensource-2.5.2.3.安装qt-win-opensource- ...
- 华为S5700系列交换机使用高级ACL限制不同网段的用户互访
组网图形 图1 使用高级ACL限制不同网段的用户互访示例 组网需求 如图一所示,某公司通过Switch实现各部门之间的互连.为方便管理网络,管理员为公司的研发部和市场部规划了两个网段的IP地址.同时为 ...
- LeetCode——Hamming Distance
LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...
- 前端人脸识别框架Tracking.js与JqueryFaceDetection
这篇文章主要就介绍两种前端的人脸识别框架(Tracking.js和JqueryFaceDetection) 技术特点 Tracking.js是使用js封装的一个框架,使用起来需要自己配置许多的东西,略 ...
- 分享知识-快乐自己:能使 Oracle 索引失效的六大限制条件
Oracle 索引的目标是避免全表扫描,提高查询效率,但有些时候却适得其反. 例如一张表中有上百万条数据,对某个字段加了索引,但是查询时性能并没有什么提高,这可能是 oracle 索引失效造成的.or ...
- shell基础复习笔记
变量的设置 以等号连接,等号两边不能有空格 变量名首个字符必须是英文,可以使用下划线,不能使用标点符号,不能使用bash里的关键字 可以使用转义字符\将特殊符号(如Enter.$.空格.!等)变成一般 ...
- 2017年浙江工业大学大学生程序设计迎新赛热身赛-J-Forever97与寄信
Forever97与未央是一对笔友,他们经常互相写信.有一天Forever97去邮局寄信,发现邮局的收费方式变成了按字收费,收取的费用为总字数除了其自身以外的最大因子.虽然Forever97是一个有情 ...
- Tomcat和Servlet
Tomcat Tomcat是什么,Tomcat是目前市场上主流Web服务器之一,是用Java语言开发的项目.Tomcat支持Servlet和JSP的规范,它由一组嵌套的层次和组件组成.结构如下图 所有 ...
- javascript: what can javascript do?
1.Javascript can change html content <!DOCTYPE html> <html> <body> <h2>What ...