洛谷[USACO06JAN]把牛Corral the Cows
题目描述
约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐.
约翰的土地上共有C<=N<=500)个草场,每个草场在一块1x1的方格内,而且这个方格的 坐标不会超过10000.有时候,会有多个草场在同一个方格内,那他们的坐标就会相同.
告诉约翰,最小的围栏的边长是多少?
输入输出格式
输入格式:
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.
输出格式:
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.
输入输出样例
输入样例#1:
3 4
1 2
2 1
4 1
5 2
输出样例#1:
4
说明
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*
+——
【题解】
咦,我竟然是0ms跑过的最优解。。。那就发个题解总结一下。
二维双指针法的完美结合
双指针法就是令l=1,从1到n枚举右指针r,然后始终保证[l,r]的区间是满足题目要求的区间,不满足就使l++,并每次用[l,r]更新答案(感觉就是简化的单调队列)
如果坐标都是一维的,我们只用双指针法就能搞定,但是由于是二维的,所以我们要用两重双指针法(四指针法。。。)
先二分答案,分别用双指针法维护纵坐标和横坐标,具体还是看代码吧~
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
int n,m,nx,ny;
struct field
{
int x,y;
}p[505];
int rx[505],ry[505],s[505];
il bool cmp1(field a,field b)
{
return a.x<b.x;
}
il bool cmp2(field a,field b)
{
return a.y<b.y;
}
il bool solve(int ml)
{
int i,a,b,c,d,sc,sd;
a=b=0;
memset(s,0,sizeof(s));
while(b<n&&rx[p[b+1].x]-rx[1]+1<=ml) s[p[++b].y]++;
for(;b<=n;s[p[++b].y]++)
{
while(rx[p[b].x]-rx[p[a+1].x]+1>ml) s[p[++a].y]--;
c=d=sc=sd=0;
while(d<ny&&ry[d+1]-ry[1]+1<=ml) sd+=s[++d];
for(;d<=ny;sd+=s[++d])
{
while(ry[d]-ry[c+1]+1>ml) sc+=s[++c];
if(sd-sc>=m) return 1;
}
}
return 0;
}
il int gi()
{
re int x=0;
re short int t=1;
re char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
int main()
{
m=gi();n=gi();
rx[0]=ry[0]=-1;
fp(i,1,n) p[i].x=gi(),p[i].y=gi();
sort(p+1,p+1+n,cmp2);
fp(i,1,n)
{
if(p[i].x>rx[nx]) ry[++ny]=p[i].y;
p[i].y=ny;
}
sort(p+1,p+1+n,cmp1);
fp(i,1,n)
{
if(p[i].x>rx[nx]) rx[++nx]=p[i].x;
p[i].x=nx;
}
int l=1,r=max(rx[nx],ry[ny]),mid;
while(l<r)
{
mid=(l+r)>>1;
if(solve(mid)) r=mid;
else l=mid+1;
}
printf("%d",r);
return 0;
}
洛谷[USACO06JAN]把牛Corral the Cows的更多相关文章
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷——P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- 洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
随机推荐
- java虚拟(一)--java内存区域和常量池概念
一.java运行时数据区 也可以称为java内存区域,和java内存模型不是一回事,不要弄混,这里基于jdk1.8之前 1.1.方法区 线程共享,类装载过程中产生的java.lang.Class对象保 ...
- gym101343J. Husam and the Broken Present 2 (状压DP)
题意:给定n个串 每个串长度不超过100 找到一个新串 使得这n个串都是它的字串 输出这个新串的最小长度 题解:n是15 n的阶乘的复杂度肯定不行 就想到了2的15次方的复杂度 想到了状压但是不知道怎 ...
- 我已经迷失在事件环(event-loop)中了【Nodejs篇】
我第一次看到他事件环(event-loop)的时候,我是一脸懵,这是什么鬼,是什么循环吗,为什么event还要loop,不是都是一次性的吗? 浏览器中和nodejs环境中的事件环是有一些区别的,这里我 ...
- [USACO06JAN] 冗余路径 Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- PAT 1129 Recommendation System
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 【Codeforces 979B】Treasure Hunt
[链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...
- App后台开发运维和架构实践学习总结(5)——App产品从需求到研发到开发到上线到产品迭代全过程
前言 如果没有做过开发,研发过产品的人,很难体会做产品的艰难,刚进公司的人,一般充当的是程序开发,我这里说的是开发,它与研发是有区别的. 一个需求下来,如果不能很好地理解产品需求,如果不能很好的驾驭需 ...
- D - Doing Homework 状态压缩 DP
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...
- [bzoj3668][Noi2014]起床困难综合症_暴力
起床困难综合征 bzoj-3668 Noi-2014 题目大意:题目链接. 注释:略. 想法:Noi考这题...联赛T1难度.... 我们将每个门上的数二进制拆分. 发现:当前位的操作可能直接确定了当 ...