传送门

良心题解

  1. #include <set>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. #define N 100001
  6. #define LL long long
  7. #define INF (~(1 << 31))
  8.  
  9. using namespace std;
  10.  
  11. int n, c, h = 1, ans, ans1;
  12. int f[N], sum[N];
  13.  
  14. struct node
  15. {
  16. LL x, y;
  17. node(int x = 0, int y = 0) : x(x), y(y) {}
  18. }p[N];
  19.  
  20. multiset <node> s;
  21. multiset <node> :: iterator it;
  22.  
  23. inline bool operator < (const node &a, const node &b)
  24. {
  25. return a.x < b.x;
  26. }
  27.  
  28. inline int read()
  29. {
  30. int x = 0, f = 1;
  31. char ch = getchar();
  32. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
  33. for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  34. return x * f;
  35. }
  36.  
  37. inline int find(int x)
  38. {
  39. return x == f[x] ? x : f[x] = find(f[x]);
  40. }
  41.  
  42. inline int merge(int x, int y)
  43. {
  44. x = find(x);
  45. y = find(y);
  46. if(x ^ y)
  47. {
  48. f[x] = y;
  49. ans--;
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. int i;
  56. LL x, y;
  57. ans = n = read();
  58. c = read();
  59. for(i = 1; i <= n; i++) f[i] = i;
  60. for(i = 1; i <= n; i++)
  61. {
  62. x = read();
  63. y = read();
  64. p[i].x = x + y;
  65. p[i].y = x - y;
  66. }
  67. sort(p + 1, p + n + 1);
  68. s.insert(node(-INF, 0));
  69. s.insert(node(INF, 0));
  70. for(i = 1; i <= n; i++)
  71. {
  72. while(p[i].x - p[h].x > c)
  73. {
  74. s.erase(s.lower_bound(node(p[h].y, h)));
  75. ++h;
  76. }
  77. y = p[i].y;
  78. it = s.lower_bound(node(p[i].y, i));
  79. node r = *it, l = *--it;
  80. if(r.x - y <= c) merge(r.y, i);
  81. if(y - l.x <= c) merge(l.y, i);
  82. s.insert(node(p[i].y, i));
  83. }
  84. for(i = 1; i <= n; i++) sum[find(i)]++;
  85. for(i = 1; i <= n; i++) ans1 = max(ans1, sum[i]);
  86. printf("%d %d\n", ans, ans1);
  87. return 0;
  88. }

  

[BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)的更多相关文章

  1. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  2. [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  3. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)

    题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...

  4. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...

  5. BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec  Memory Limit: 64 MB Description ...

  6. bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...

  7. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...

  8. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  9. BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...

随机推荐

  1. COGS 133. [USACO Mar08] 牛跑步

    ★★★   输入文件:cowjog.in   输出文件:cowjog.out   简单对比时间限制:1 s   内存限制:128 MB Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她 ...

  2. UVa 12219 Common Subexpression Elimination (stl,模拟,实现)

    一般来说,把一颗子树离散成一个int,把一个结点的字符离散成一个int会方便处理 直接map离散.当然一个结点最多只有4个小写字母,也可以直接编码成一个27进制的整数,舍掉0,为了区分0和0000. ...

  3. js获得本季度的开始日期 结束日期

    var now = new Date(); //当前日期var nowMonth = now.getMonth()+1; //当前月var nowYear = now.getFullYear(); / ...

  4. ewebeditor上传文件大小

    做项目大家都少不了要跟html在线编辑器打交道,这里我把我的一些使用经验及遇到的问题发出来和大家交流一下. Ewebeditor使用说明:一.部署方式:1.直接把压缩目录中的文件拷贝到您的网站发布目录 ...

  5. Java InputStream、String、File相互转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  6. ios多线程之NSOperation

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  7. Fortran学习笔记2(变量声明)

    常数的申明方式 变量初始化 等价申明EQUIALENCE 类型转化 自定义类型 KIND用法 常数的申明方式 程序中所有处理的数据,有些事固定不变的常数,如圆周率π和重力加速度G等. 此时,程序员可以 ...

  8. 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别

    1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...

  9. (43)zabbix报警媒介介绍

    zabbix触发器到了要发送通知的情况下,需要一个中间介质来接收并传递它的消息给运维们,以往用nagios,通常用脚本发送邮件或者发送飞信来达到报警.这个脚本实际上就是一个媒介了. zabbix有如下 ...

  10. CSS3的渐变-gradient

    CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变). CSS3的线性渐变 一.线性渐变在Mozilla下的应用 语法: -moz-li ...