【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][ ...
随机推荐
- 饭卡管理系统学生E-R图
- 2 - 1.1 可行性分析 1.1.1 项目背景 近年来学生食堂饭卡的使用给高校餐饮管理带来了一次革命, 从结算方式到账户管理, 从卫生便捷到数据统计等,不仅给就餐者带来了方便,也使餐饮结算手段发 ...
- 解析Linux系统的平均负载概念
一.什么是系统平均负载(Load average)? 在Linux系统中,uptime.w.top等命令都会有系统平均负载load average的输出,那么什么是系统平均负载呢?系统平均负载被定义为 ...
- vue.js 调用iOS原生方法,JavaScriptCore
1. 2. 3. 4.vue.js调用
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- PHPAdmin的安装和配置
phpadmin是用于管理mysql数据库的一个产品,,毕竟很多数据库服务器不能够公开连接,所以只能够使用http的方式来进行连接管理. 下载phpadmin( http://xj-http. ...
- scala学习手记40 - case表达式里的模式变量和常量
再来看一下之前的一段代码: def process(input: Any) { input match { case (a: Int, b: Int) => println("Proc ...
- php超级全局变量和魔术变量
php超级全局变量和魔术变量 一.总结 一句话总结: 1.两者的书写形式非常不一样,超级全局变量是$_大写变量名 的形式,魔术变量是 __大写变量名的形式__ 2.两者的应用范围不一样,超级全局变量是 ...
- Java 注解指导手册 – 终极向导
原文链接 原文作者:Dani Buiza 译者:Toien Liu 校对:深海 编者的话:注解是java的一个主要特性且每个java开发者都应该知道如何使用它. 我们已经在Java Code Gee ...
- 卸载MicrosoftBAF(删除C:\CommonFramework\instdata.dat)
发现有个可疑文件夹 C:\CommonFramework ,而且还会不停的删除创建 C:\CommonFramework\instdata.dat 最后被发现这是必应的一个框架程序在捣鬼,在微软论坛里 ...
- MongoDB GridFS——本质上是将一个文件分割为大小为256KB的chunks 每个chunk里会放md5标识 取文件的时候会将这些chunks合并为一个整体返回
MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). GridFS 也是文件存储的一种方式,但是它是存储在MonoDB的集合中 ...