传送门

可以二分边长

然后另开两个数组,把x从小到大排序,把y从小到大排序

枚举x,可以得到正方形的长

枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞

其实发现算法的本质之后,x可以不用从小到大排序

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #define N 1001
  5. #define max(x, y) ((x) > (y) ? (x) : (y))
  6.  
  7. int c, n, ans;
  8. int x[N], y[N], a[N], b[N];
  9.  
  10. inline int read()
  11. {
  12. int x = 0, f = 1;
  13. char ch = getchar();
  14. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
  15. for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  16. return x * f;
  17. }
  18.  
  19. inline bool cmp1(int a, int b)
  20. {
  21. return x[a] < x[b];
  22. }
  23.  
  24. inline bool cmp2(int a, int b)
  25. {
  26. return y[a] < y[b];
  27. }
  28.  
  29. inline bool check(int len)
  30. {
  31. int i, j, k, l, r, sum;
  32. for(i = 1; i <= n; i++)
  33. {
  34. k = 1;
  35. sum = 0;
  36. l = x[a[i]];
  37. r = l + len - 1;
  38. for(j = 1; j <= n; j++)
  39. {
  40. while(y[b[k]] - y[b[j]] + 1 <= len && k <= n)
  41. {
  42. if(l <= x[b[k]] && x[b[k]] <= r) sum++;
  43. k++;
  44. }
  45. if(sum >= c) return 1;
  46. if(l <= x[b[j]] && x[b[j]] <= r) sum--;
  47. }
  48. }
  49. return 0;
  50. }
  51.  
  52. int main()
  53. {
  54. int i, l = 1, r, mid;
  55. c = read();
  56. n = read();
  57. for(i = 1; i <= n; i++)
  58. {
  59. x[i] = read();
  60. y[i] = read();
  61. r = max(r, x[i]);
  62. r = max(r, y[i]);
  63. a[i] = b[i] = i;
  64. }
  65. std::sort(a + 1, a + n + 1, cmp1);
  66. std::sort(b + 1, b + n + 1, cmp2);
  67. while(l <= r)
  68. {
  69. mid = (l + r) >> 1;
  70. if(check(mid)) ans = mid, r = mid - 1;
  71. else l = mid + 1;
  72. }
  73. printf("%d\n", ans);
  74. return 0;
  75. }

  

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

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

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

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

  6. [NOIP模拟赛][并没有用二分][乱搞AC]

    圆圈舞蹈 [问题描述] 熊大妈的奶牛在时针的带领下,围成了一个圆圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针走,到达 ...

  7. poj_3179 Corral the Cows (二分+二维前缀和+离散化)

    [题目链接] http://poj.org/problem?id=3179 [参考] http://www.cnblogs.com/evenbao/p/9243183.html [算法] 二分答案+判 ...

  8. $Poj3179\ Corral\ the\ Cows$ 二分+离散化+二维前缀和

    Poj $Description$ 在一个二维平面上,有$N$颗草,每颗草的大小是$1*1$,左下角坐标为$x_i,y_i$.要求一个正方形,正方形的边平行于$x$或$y$轴,正方形里面包含至少$C$ ...

  9. SCU 4437 Carries(二分乱搞)题解

    题意:问任意两对ai,aj相加的总进位数为多少.比如5,6,95分为(5,6)(5,95)(6,95),进位数 = 1 + 2 + 2 = 5 思路:显然暴力是会超时的.我们可以知道总进位数等于每一位 ...

随机推荐

  1. SQL 视图、事务

    假设看多个不同的表 select *from student ,score,course,teacher 有重复的    改为select student.Sno,sname,ssex,sbirthd ...

  2. es的插件 ik分词器的安装和使用

    今天折腾了一天,在es 5.5.0 上安装ik.一直通过官方给定的命令没用安装成功,决定通过手工是形式进行安装.https://github.com/medcl/elasticsearch-analy ...

  3. 2018_oakland_linuxmalware

    2018年oakland论文:理解linux恶意软件 论文地址:http://www.s3.eurecom.fr/~yanick/publications/2018_oakland_linuxmalw ...

  4. CAD控件界面显示与隐藏(网页版)

    控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏       mxOcx.ShowToolBar("常用工具",isShow ...

  5. Asp.Net Core 进阶(一) —— 读取appsettings.json

    我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...

  6. 判断一个链表是否为回文结构 【题目】 给定一个链表的头节点head,请判断该链表是否为回 文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。 15->6->15,返回true。 1->2->3,返回false。 进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。

    方式1:借助栈 空间辅助度是O(N) 方式2: 借助栈 空间复杂度是 O(n/2).只存后半个链表 方式3: 反转后半个链表  最后再反转回来 package my_basic.class_3; im ...

  7. XDB基于Library的备份及恢复

    基于standalone全备份 语句: xdb backup --federation xhive://localhost:1235 --standalone --file E:\xdbData\xD ...

  8. (39)zabbix snmp自定义OID nginx监控实例

    为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...

  9. Linux基础学习-数据备份工具Rsync

    数据备份工具rsync 作为一个系统管理员,数据备份是非常重要的,如果没有做好备份策略,磁盘损坏了,那么你的数据将全部丢失,所以在日常的维护工作中,一定要时刻牢记给数据做备份. rsync不仅可以可以 ...

  10. python爬虫基础02-urllib库

    Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...