水题,20分钟AC,最大值最小,一看就是二分答案。。。

代码:

  1. Description
  2.  
  3. Farmer John has built a new long barn, with N ( <= N <= ,) stalls. The stalls are located along a straight line at positions x1,...,xN ( <= xi <= ,,,). His C ( <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
  4.  
  5. 农夫 John 建造了一座很长的畜栏,它包括NN ( <= N <= ,)个隔间,这些小隔间依次编号为x1,...,xN ( <= xi <= ,,,). 但是,John的C ( <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢
  6. Input
  7. * Line : Two space-separated integers: N and C * Lines ..N+: Line i+ contains an integer stall location, xi
  8. 第一行:空格分隔的两个整数N和C
  9. 第二行---第N+1行:i+1行指出了xi的位置
  10. Output
  11. * Line : One integer: the largest minimum distance
  12. 第一行:一个整数,最大的最小值
  13. Sample Input
  14.  
  15. Sample Output
  16.  
  17. 把牛放在1,,8这样最小距离是3

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<ctime>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<cstring>
  8. using namespace std;
  9. #define duke(i,a,n) for(int i = a;i <= n;i++)
  10. #define lv(i,a,n) for(int i = a;i >= n;i--)
  11. #define clean(a) memset(a,0,sizeof(a))
  12. const long long INF = (60LL);
  13. typedef long long ll;
  14. typedef double db;
  15. template <class T>
  16. void read(T &x)
  17. {
  18. char c;
  19. bool op = ;
  20. while(c = getchar(), c < '' || c > '')
  21. if(c == '-') op = ;
  22. x = c - '';
  23. while(c = getchar(), c >= '' && c <= '')
  24. x = x * + c - '';
  25. if(op) x = -x;
  26. }
  27. template <class T>
  28. void write(T x)
  29. {
  30. if(x < ) putchar('-'), x = -x;
  31. if(x >= ) write(x / );
  32. putchar('' + x % );
  33. }
  34. int l,r,maxn = ,n,c,ok;
  35. ll x[];
  36. int main()
  37. {
  38. read(n);
  39. read(c);
  40. duke(i,,n)
  41. {
  42. read(x[i]);
  43. if(x[i] > maxn)
  44. {
  45. maxn = x[i];
  46. }
  47. }
  48. sort(x + ,x + n + );
  49. l = ;
  50. r = maxn;
  51. while(l != r)
  52. {
  53. ok = ;
  54. int mid = (l + r) >> ;
  55. int tot = ,ans = ;
  56. // cout<<endl;
  57. // cout<<mid<<endl;
  58. duke(i,,n)
  59. {
  60. if(x[i] - x[i - ] + tot < mid)
  61. {
  62. tot = tot + x[i] - x[i - ];
  63. }
  64. else
  65. {
  66. // cout<<tot<<" "<<x[i] - x[i - 1]<<endl;
  67. ans ++;
  68. tot = ;
  69. }
  70. if(ans >= c)
  71. {
  72. ok = ;
  73. break;
  74. }
  75. }
  76. if(ok == )
  77. {
  78. l = mid + ;
  79. }
  80. else
  81. {
  82. r = mid;
  83. }
  84. }
  85. ok = ;
  86. int mid = l;
  87. int tot = ,ans = ;
  88. duke(i,,n)
  89. {
  90. if(x[i] - x[i - ] + tot < mid)
  91. {
  92. tot = tot + x[i] - x[i - ];
  93. }
  94. else
  95. {
  96. ans ++;
  97. tot = ;
  98. }
  99. if(ans >= c)
  100. {
  101. ok = ;
  102. break;
  103. }
  104. }
  105. if(ok == )
  106. write(l);
  107. else
  108. write(l - );
  109. return ;
  110. }
  111. /*
  112. 7 3
  113. 1
  114. 2
  115. 3
  116. 7
  117. 8
  118. 9
  119. 10
  120. */

B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案的更多相关文章

  1. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  2. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 407  Solved: 325[S ...

  3. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

  4. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...

  5. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  6. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  7. 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)

    话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...

  8. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  9. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

随机推荐

  1. javascript中执行环境和作用域(js高程)

    执行环境(execution context,为简单起见,有时也成为“环境”)是javascript中最为重要的一个概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为.每个执行环境 ...

  2. matplotlib之pyplot 学习示例

    现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子.  1. 简单的绘图( ...

  3. Android 概览屏幕

    文章照搬过来的:原文地址https://developer.android.google.cn/guide/components/recents.html 概览屏幕(也称为最新动态屏幕.最近任务列表或 ...

  4. SAP computer之program counter

    Program counter The program is stored in memory with the first instruction at binary address 0000, t ...

  5. SLAM: 图像角点检测的Fast算法(OpenCV文档)

    官方链接:http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_fast/py_fast.html#fast-algorithm- ...

  6. 【sqli-labs】 less4 GET - Error based - Double Quotes - String (基于错误的GET双引号字符型注入)

    提交id参数 加' http://localhost/sqli/Less-4/?id=1' 页面正常,添加" http://localhost/sqli/Less-4/?id=1" ...

  7. cocos creator 底部按钮touch延迟

    cocos论坛里有这个问题: http://forum.cocos.com/t/ios-touchstart-bug/63367我的引擎版本:"engine_version": & ...

  8. java操作Excel的poi 创建一个sheet页

    package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.us ...

  9. 从输入url到页面展示出来经历了哪些过程

    本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址.  此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...

  10. Lua操作系统库、流、文件库

    Lua操作系统库.流.文件库 1.Lua中所有的操作系统库函数 (1)os.clock() --功能:返回执行该程序cpu花费的时钟秒数 (2)os.time(...) --按参数的内容返回一个时间值 ...