洛谷 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;elsel=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;}elsereturn 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;elsel=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;}elsereturn 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;elsel=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;elsel=mid+1;}check1(i,j,l);}printf("%d\n",ans+1);return 0;
}
代码写的的确不聪明
2018.6.20
转载于:https://www.cnblogs.com/butterflydew/p/9203893.html
洛谷 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 ...
- 洛谷[USACO06JAN]把牛Corral the Cows
题目描述 约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐. 约翰的土地上共有C<=N< ...
- [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...
- 洛谷 P1344 [USACO4.4]追查坏牛奶Pollutant Control 解题报告
P1344 [USACO4.4]追查坏牛奶Pollutant Control 题目描述 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件事的时候 ...
- 洛谷 P3373 【模板】线段树 2 解题报告
P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面三种操作: 1.将某区间每一个数乘上\(x\) 2.将某区间每一个数加上\(x\) 3.求出某区间每一个数的和 输入输出格式 ...
- 「洛谷P2397」 yyy loves Maths VI (mode) 解题报告
P2397 yyy loves Maths VI (mode) 题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居 ...
- 洛谷 P2336 [SCOI2012]喵星球上的点名 解题报告
P2336 [SCOI2012]喵星球上的点名 题目描述 a180285 幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有 \(N\) 个喵星人,每个喵星人的 ...
最新文章
- MATLAB一些图的绘制,MATLAB的使用
- Ext.data-Store
- 无向图的最短路径求解算法之——Dijkstra算法
- 使用Xshell工具连接虚拟机
- WebService实例-CRM系统提供WebService实现用户注册功能
- [ffmpeg] 解码API
- C++ 语法都不会怎么写代码? 03
- JSP+JSTL+EL表达式,实现web页面的页面跳转功能(上一页下一页首页末页页面跳转)
- smartupload java_smartupload实现文件上传
- 三阶魔方还原步骤图_三阶魔方公式图解、教程
- pr控制C语言程序,PR控制(含代码)
- 前2周还很火的ChatGPT,怎么突然就哑火了?
- MarkDown的简介
- 不懂编程?节点包来凑——Dynamo常用节点包推荐(下)
- 如何用tushare复盘
- 有关防火墙的调研总结
- excel求方差和标准差的函数_Excel如何计算方差与均方差
- 计算机组网的有线传输媒介主要依赖,家庭无线局域网组建毕业论文
- 搜狗输入法繁简体切换
- 闲鱼如何保障交易链路质量
热门文章
- jdbc连接teradata仓库_java--teradata
- AspNetCore 3.1(ABP.Next)集成MiniProfile(简要)
- 2022-2027年中国中药大健康行业市场调研及未来发展趋势预测报告
- VMware Workstation 12
- 世界主要国家人工智能战略简析
- 5G独立组网(SA)和 非独立组网(NSA)
- ffmpeg实战教程(十三)iJKPlayer源码简析
- AT3576 E Popping Balls——计数思路
- 使用极狐GitLab CI/CD部署应用到Kubernetes集群的方案
- sonar java 编译_Sonar编译问题对应:File [...] can't be indexed twice.