洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows
题目描述
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.
约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有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.
这个题目的数据比较小,我的做法是\(O(N^2*log^2N)\),优化掉一个\(logN\)不算难,预处理一下即可,优化到\(O(N*log^2N)\)就得用扫描线+线段树了
先离散化,然后枚举正方形左下角,分别二分两个相邻的边的变成并更新答案。
事实上这个题难在实现上,感觉代码不太好写。
Code:
#include <cstdio>
#include <set>
#include <map>
using namespace std;
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
const int N=504;
struct node
{
int x,y,cnt;
}t[N][N];
int n,rr,cntx,cnty,f[N][N],fx[N],fy[N],ans=0x3f3f3f3f,X[10010],Y[10010];
map <int,map<int,int > > m;
set <int > s1,s2;
bool check0(int i,int j,int R)//第i行第j列为左下角
{
int l=i,r=cnty,d=fx[R]-fx[j];
while(l<r)
{
int mid=l+r+1>>1;
if(fy[mid]-fy[i]>d)
r=mid-1;
else
l=mid;
}
if(f[l][R]-f[i-1][R]-f[l][j-1]+f[i-1][j-1]>=rr)
{
ans=min(ans,d);
return true;
}
else
return false;
}
bool check1(int i,int j,int R)//第i行第j列为左下角
{
int l=j,r=cntx,d=fy[R]-fy[i];
while(l<r)
{
int mid=l+r+1>>1;
if(fx[mid]-fx[j]>d)
r=mid-1;
else
l=mid;
}
if(f[R][l]-f[R][j-1]-f[i-1][l]+f[i-1][j-1]>=rr)
{
ans=min(ans,d);
return true;
}
else
return false;
}
int main()
{
scanf("%d%d",&rr,&n);
int x,y,x0=0,y0=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
m[x][y]++;
s1.insert(x);
s2.insert(y);
}
while(!s1.empty())
{
X[*s1.begin()]=++cntx;
s1.erase(s1.begin());
}
while(!s2.empty())
{
Y[*s2.begin()]=++cnty;
s2.erase(s2.begin());
}
for(map <int,map<int,int > >::iterator it1=m.begin();it1!=m.end();it1++)
{
for(map <int,int >::iterator it2=(it1->second).begin();it2!=(it1->second).end();it2++)
{
x0=X[it1->first],y0=Y[it2->first];
t[x0][y0].cnt=it2->second;
t[x0][y0].x=it1->first;
t[x0][y0].y=it2->first;
}
}
for(int i=1;i<=cnty;i++)
for(int j=1;j<=cntx;j++)
{
f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+t[j][i].cnt;
fx[j]=max(fx[j],t[j][i].x);
fy[i]=max(fy[i],t[j][i].y);
}
for(int i=1;i<=cnty;i++)//枚举从下往上第几行
for(int j=1;j<=cntx;j++)//枚举左下角
{
int l=j,r=cntx;//二分x的长度
while(l<r)
{
int mid=l+r>>1;
if(check0(i,j,mid))
r=mid;
else
l=mid+1;
}
check0(i,j,l);
l=i,r=cnty;//二分y的长度
while(l<r)
{
int mid=l+r>>1;
if(check1(i,j,mid))
r=mid;
else
l=mid+1;
}
check1(i,j,l);
}
printf("%d\n",ans+1);
return 0;
}
代码写的的确不聪明
2018.6.20
洛谷 P2862 [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 ...
- 洛谷 P1291 [SHOI2002]百事世界杯之旅 解题报告
P1291 [SHOI2002]百事世界杯之旅 题目描述 "--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...
- 洛谷[USACO06JAN]把牛Corral the Cows
题目描述 约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐. 约翰的土地上共有C<=N< ...
- [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...
- 洛谷 P4714 「数学」约数个数和 解题报告
P4714 「数学」约数个数和 题意(假):每个数向自己的约数连边,给出\(n,k(\le 10^{18})\),询问\(n\)的约数形成的图中以\(n\)为起点长为\(k\)的链有多少条(注意每个点 ...
- 洛谷 P4345 [SHOI2015]超能粒子炮·改 解题报告
P4345 [SHOI2015]超能粒子炮·改 题意 求\(\sum_{i=0}^k\binom{n}{i}\),\(T\)组数据 范围 \(T\le 10^5,n,j\le 10^{18}\) 设\ ...
- 洛谷 P1691 有重复元素的排列问题 解题报告
P1691 有重复元素的排列问题 题目描述 设\(R={r_1,r_2,--,r_n}\)是要进行排列的\(n\)个元素.其中元素\(r_1,r_2,--,r_n\)可能相同.使设计一个算法,列出\( ...
随机推荐
- 【LeeCode23】Merge k Sorted Lists★★★
1.题目描述: 2.解题思路: 题意:将K个已经排序的链表合并成一个排序的链表,分析并描述所用算法的复杂度. 方法一:基于“二分”思想的归并排序.本文用非递归和递归两种方法实现. (1)非递归:归并排 ...
- Django Rest Framework源码剖析(四)-----API版本
一.简介 在我们给外部提供的API中,可会存在多个版本,不同的版本可能对应的功能不同,所以这时候版本使用就显得尤为重要,django rest framework也为我们提供了多种版本使用方法. 二. ...
- 20155232《网络对抗》Exp7 网络欺诈防范
20155232<网络对抗>Exp7 网络欺诈防范 一.实践内容 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体实践有 (1)简单应用SET工具建立冒名网 ...
- 【第十二课】FTP服务
目录 FTP服务 1.Linux下部署pure-ftpd 2.FTP的主动和被动模式 2.1.什么是主动FTP 2.2.什么是被动FTP 2.3.主动模式ftp与被动模式FTP优点和缺点: FTP服务 ...
- 【第十课】Tomcat入门
目录 1.Tomcat介绍 2.Tomcat安装部署和配置 (1)tomcat下载和解压 (2)jdk环境变量配置 (3)设置tomcat以普通用户启动 (4)查看tomcat的配置 (5)tomca ...
- asp.net mvc2+nhibernate实体类映射问题之“尝试创建Controller类型的控制器时出错请确保控制器具有无参数公共构造函数”
程序出了问题,解决后发现如此简单,犯的错误是如此的低级啊,特此记录! 运行程序总是在浏览器中看到一片空白,什么也没有,用application_error跟踪发现抓出一个这样的异常 然后浏览器中就是这 ...
- 解决 引入本地jar包后 maven无法编译的问题及部署war包缺失本地jar包的问题
参考:https://blog.csdn.net/wang864676212/article/details/82626922 pom.xml 引入 <plugin> <plugin ...
- [SHOI2012]随机树[期望dp]
题意 初始 \(1\) 个节点,每次选定一个叶子节点并加入两个儿子直到叶子总数为 \(n\),问叶子节点深度和的平均值的期望以及最大叶子深度的期望. \(n\leq 100\) . 分析 对于第一问, ...
- [BZOJ4857][JSOI2016]反质数序列[最大点独立集]
题意 在长度为 \(n\) 的序列 \(a\) 中选择尽量长的子序列,使得选出子序列中任意两个数的和不为质数. \(n\leq3000\ ,a_i\leq10^5\). 分析 直接按照奇偶性建立二分图 ...
- pandas 初识(一)
基本内容 Series: Series 是有一组数据(numpy的数据类型 numpy.ndarray)以及一组数据标签(即索引)组成,可以看成一个一个定长的有序字典(索引值到数据值的一个映射) ob ...