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:

  1. #include <cstdio>
  2. #include <set>
  3. #include <map>
  4. using namespace std;
  5. int max(int x,int y){return x>y?x:y;}
  6. int min(int x,int y){return x<y?x:y;}
  7. const int N=504;
  8. struct node
  9. {
  10. int x,y,cnt;
  11. }t[N][N];
  12. int n,rr,cntx,cnty,f[N][N],fx[N],fy[N],ans=0x3f3f3f3f,X[10010],Y[10010];
  13. map <int,map<int,int > > m;
  14. set <int > s1,s2;
  15. bool check0(int i,int j,int R)//第i行第j列为左下角
  16. {
  17. int l=i,r=cnty,d=fx[R]-fx[j];
  18. while(l<r)
  19. {
  20. int mid=l+r+1>>1;
  21. if(fy[mid]-fy[i]>d)
  22. r=mid-1;
  23. else
  24. l=mid;
  25. }
  26. if(f[l][R]-f[i-1][R]-f[l][j-1]+f[i-1][j-1]>=rr)
  27. {
  28. ans=min(ans,d);
  29. return true;
  30. }
  31. else
  32. return false;
  33. }
  34. bool check1(int i,int j,int R)//第i行第j列为左下角
  35. {
  36. int l=j,r=cntx,d=fy[R]-fy[i];
  37. while(l<r)
  38. {
  39. int mid=l+r+1>>1;
  40. if(fx[mid]-fx[j]>d)
  41. r=mid-1;
  42. else
  43. l=mid;
  44. }
  45. if(f[R][l]-f[R][j-1]-f[i-1][l]+f[i-1][j-1]>=rr)
  46. {
  47. ans=min(ans,d);
  48. return true;
  49. }
  50. else
  51. return false;
  52. }
  53. int main()
  54. {
  55. scanf("%d%d",&rr,&n);
  56. int x,y,x0=0,y0=0;
  57. for(int i=1;i<=n;i++)
  58. {
  59. scanf("%d%d",&x,&y);
  60. m[x][y]++;
  61. s1.insert(x);
  62. s2.insert(y);
  63. }
  64. while(!s1.empty())
  65. {
  66. X[*s1.begin()]=++cntx;
  67. s1.erase(s1.begin());
  68. }
  69. while(!s2.empty())
  70. {
  71. Y[*s2.begin()]=++cnty;
  72. s2.erase(s2.begin());
  73. }
  74. for(map <int,map<int,int > >::iterator it1=m.begin();it1!=m.end();it1++)
  75. {
  76. for(map <int,int >::iterator it2=(it1->second).begin();it2!=(it1->second).end();it2++)
  77. {
  78. x0=X[it1->first],y0=Y[it2->first];
  79. t[x0][y0].cnt=it2->second;
  80. t[x0][y0].x=it1->first;
  81. t[x0][y0].y=it2->first;
  82. }
  83. }
  84. for(int i=1;i<=cnty;i++)
  85. for(int j=1;j<=cntx;j++)
  86. {
  87. f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+t[j][i].cnt;
  88. fx[j]=max(fx[j],t[j][i].x);
  89. fy[i]=max(fy[i],t[j][i].y);
  90. }
  91. for(int i=1;i<=cnty;i++)//枚举从下往上第几行
  92. for(int j=1;j<=cntx;j++)//枚举左下角
  93. {
  94. int l=j,r=cntx;//二分x的长度
  95. while(l<r)
  96. {
  97. int mid=l+r>>1;
  98. if(check0(i,j,mid))
  99. r=mid;
  100. else
  101. l=mid+1;
  102. }
  103. check0(i,j,l);
  104. l=i,r=cnty;//二分y的长度
  105. while(l<r)
  106. {
  107. int mid=l+r>>1;
  108. if(check1(i,j,mid))
  109. r=mid;
  110. else
  111. l=mid+1;
  112. }
  113. check1(i,j,l);
  114. }
  115. printf("%d\n",ans+1);
  116. return 0;
  117. }

代码写的的确不聪明


2018.6.20

洛谷 P2862 [USACO06JAN]把牛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. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

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

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

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

  4. 洛谷 P1291 [SHOI2002]百事世界杯之旅 解题报告

    P1291 [SHOI2002]百事世界杯之旅 题目描述 "--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...

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

    题目描述 约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐. 约翰的土地上共有C<=N< ...

  6. [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)

    传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...

  7. 洛谷 P4714 「数学」约数个数和 解题报告

    P4714 「数学」约数个数和 题意(假):每个数向自己的约数连边,给出\(n,k(\le 10^{18})\),询问\(n\)的约数形成的图中以\(n\)为起点长为\(k\)的链有多少条(注意每个点 ...

  8. 洛谷 P4345 [SHOI2015]超能粒子炮·改 解题报告

    P4345 [SHOI2015]超能粒子炮·改 题意 求\(\sum_{i=0}^k\binom{n}{i}\),\(T\)组数据 范围 \(T\le 10^5,n,j\le 10^{18}\) 设\ ...

  9. 洛谷 P1691 有重复元素的排列问题 解题报告

    P1691 有重复元素的排列问题 题目描述 设\(R={r_1,r_2,--,r_n}\)是要进行排列的\(n\)个元素.其中元素\(r_1,r_2,--,r_n\)可能相同.使设计一个算法,列出\( ...

随机推荐

  1. Codeforces round 1111

    CF Div 2 537 比赛链接 感觉题目难度OK,五个题都能做,后俩题考察人的翻译水平... 另外,$Claris$太强了... A 直接按照题意模拟,不知道为啥有人会被× 代码: #includ ...

  2. ubuntu 下 go 语言调试器 dlv 的安装

    1.  从 https://github.com/derekparker/delve.git 下载delve压缩包delve-master.zip. 2. 使用 winscp 工具将delve-mas ...

  3. LORA---关于LORA的30个常见问题解答

    1) 什么是LoRa调制? LoRa (Long Range,远距离)是一种调制技术,与同类技术相比,提供更长的通信距离.调制是基于扩频技术,线性调制扩频(CSS)的一个变种,具有前向纠错(FEC). ...

  4. DotNetCore部署(IIS)踩坑记

    一.windows系统中Dotnet core runtime 安装后,无法启动次程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll的解决方法 错误现象如图 因为 ...

  5. 本地navicat远程连接到云服务器数据库

    本来没有开启秘钥的远程服务器端数据库连接非常方便,就在新建连接上填入数据就ok了,但是开启SSH秘钥后的服务器连接有一个大坑,下面来详细讲讲. 其实开启了秘钥,在新建连接下,先选择SSH方式登录到远程 ...

  6. [穷尽]ADO.NET连接字符串

    微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...

  7. KVM虚拟机管理——虚拟机创建和操作系统安装

    1. 概述2. 交互式安装2.1 图形化-本地安装2.1.1 图形化本地CDROM安装2.2.2 图形化本地镜像安装2.2 命令行-本地安装2.2.1 命令行CDROM安装2.3 图形化-网络安装2. ...

  8. index索引的一些简单理解

    index索引(普通索引,允许出现相同的索引内容) 1.索引 索引是在数据量和访问量较大的时候,而出现的一种优化数据库的手段 索引可以提高查询(select)的效率,但相应的,它的 INSERT 与 ...

  9. 简单的RNN和BP多层网络之间的区别

    先来个简单的多层网络 RNN的原理和出现的原因,解决什么场景的什么问题 关于RNN出现的原因,RNN详细的原理,已经有很多博文讲解的非常棒了. 如下: http://ai.51cto.com/art/ ...

  10. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...