Corral the Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1352   Accepted: 565

Description

Farmer John wishes to build a corral for his cows. Being finicky beasts, they demand that the corral be square and that the corral contain at least C (1 <= C <= 500) clover fields for afternoon treats. The corral's edges must be parallel to the X,Y axes.

FJ's land contains a total of N (C <= N <= 500) clover fields, each a block of size 1 x 1 and located at with its lower left corner at integer X and Y coordinates each in the range 1..10,000. Sometimes more than one clover field grows at the same location; such a field would have its location appear twice (or more) in the input. A corral surrounds a clover field if the field is entirely located inside the corral's borders.

Help FJ by telling him the side length of the smallest square containing C clover fields.

Input

Line 1: Two space-separated integers: C and N

Lines 2..N+1: Each line contains two space-separated integers that are the X,Y coordinates of a clover field.

Output

Line 1: A single line with a single integer that is length of one edge of the minimum size square that contains at least C clover fields.

Sample Input

3 4
1 2
2 1
4 1
5 2

Sample Output

4

Hint

Explanation of the sample:

|*   *

| * *

+------

Below is one 4x4 solution (C's show most of the corral's area); many others exist.

|CCCC

|CCCC

|*CCC*

|C*C*

+------

Source

 
 
题意:需要你搭建一个正方形的围栏,围栏里面需要有至少C个特殊的点,问你正方形最短的边长是多少
题解:给的坐标可能会重复,所以我们按照x坐标的和y坐标从小到大排序后离散化,用二维前缀和记录区域内点的个数,然后二分答案,每次检查时检查区域内的点是否满足大于C个即可
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int C,n;
struct node{
int x,y;
}p[maxn];
int xn,yn,rx[maxn],ry[maxn];
int sum[maxn][maxn];
bool cmp1(node a,node b){
return a.x<b.x;
}
bool cmp2(node a,node b){
return a.y<b.y;
}
bool check(int k){
for(int a=,b=;;a++){
while(rx[b+]-rx[a]+<=k&&b<xn) b++;
for(int c=,d=;;c++){
while(ry[d+]-ry[c]+<=k&&d<yn) d++;
int ans=sum[b][d]+sum[a-][c-]-sum[a-][d]-sum[b][c-];
if(ans>=C) return true;
if(d==yn) break;
}
if(b==xn) break;
}
return false;
} int main(){
scanf("%d%d",&C,&n);
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p+,p+n+,cmp1);
xn=;rx[]=p[].x;p[].x=;
for(int i=;i<=n;i++){
if(p[i].x!=p[i-].x) rx[++xn]=p[i].x;
p[i].x=xn;
}
sort(p+,p+n+,cmp2);
yn=;ry[]=p[].y;p[].y=;
for(int i=;i<=n;i++){
if(p[i].y!=p[i-].y) ry[++yn]=p[i].y;
p[i].y=yn;
}
for(int i=;i<=n;i++) sum[p[i].x][p[i].y]++;
for(int i=;i<=xn;i++){
for(int j=;j<=yn;j++){
sum[i][j]+=sum[i][j-]+sum[i-][j]-sum[i-][j-];
}
}
int l=,r=max(rx[xn],ry[yn]),ans;
while(l<=r){
int mid=l+r>>;
if(check(mid)){
ans=mid;
r=mid-;
}else{
l=mid+;
}
}
printf("%d\n",ans);
}

POJ 3179 Corral the Cows的更多相关文章

  1. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  2. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  3. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  6. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  7. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  8. 【POJ 3179】 Corral the Cows

    [题目链接] http://poj.org/problem?id=3179 [算法] 首先,我们发现答案是具有单调性的,也就是说,如果边长为C的正方形可以,那么比边长C大的正方形也可以,因此,可以二分 ...

  9. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

随机推荐

  1. Kubernetes-运维指南

    Node隔离与恢复 cat unschedule_node.yaml apiVersion: kind: Node metadata: name: k8s-node-1 labels: kuberne ...

  2. java 上溯造型与下塑造型

    父类: package com.neusoft.chapter07; public class Father { public int i = 1; public void say(){ System ...

  3. C#中Equals和= =(等于号)的比较)(转载)

    C#中Equals和= =(等于号)的比较) 相信很多人都搞不清Equals和 = =的区别,只是零星的懂一点,现在就让我带大家来进行一些剖析 一.           值类型的比较 对于值类型来说  ...

  4. CSS3单选动画

    本示例实现了两种单选按钮动画效果,一种是缩放,一种是旋转,以下是html布局以及css样式 html:这里使用了label标签的for属性,以此来绑定radio <div class=" ...

  5. 2426: [HAOI2010]工厂选址

    2426: [HAOI2010]工厂选址 链接 代码: /* 贪心: 奇妙!!!!! 因为所有的煤矿不是给新厂,就是给旧厂(而且旧厂的得到b) 为了使费用最小,感性的理解,那么一个煤矿给哪个厂,取决于 ...

  6. java doc 编写

    总而言之,我觉得有用的是: @see 只要敲了@see 然后会自动写你的类名的,很方便.# 去连接字段 {@link } 只要敲了{@link } 然后会自动写你的类名的,很方便.# 去连接字段 如果 ...

  7. springmvc基础篇—使用注解方式为前台提供数据

    一.新建一个Controller package cn.cfs.springmvc.service; import java.util.ArrayList; import java.util.Hash ...

  8. beanshell引用参数化数据

    步骤: 1.添加参数化组件CSV Data Set  Config: 2.添加beanshell preprocessor,引用变量: 验证: 2个线程,迭代2次,分别取了4个不同的值.

  9. Fiddler 4 实现手机App的抓包

    Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook ...

  10. 在Android上,怎样与Kotlin一起使用Retrofit(KAD21)

    作者:Antonio Leiva 时间:Apr 18, 2017 原文链接:https://antonioleiva.com/retrofit-android-kotlin/ 这是又一个例子,关于怎样 ...