Cable master POJ - 1064 题解

冷不防 2022-09-02 04:16 275阅读 0赞

题目链接

https://vjudge.net/problem/POJ-1064

当然我是在Virtual Judge上面写的,可以直接去POJ去提交。

这个题目感觉并不是很困难的样子,主要就是一个二分的思想,之前写的时候,主要是结尾判断的时候,没有向下取整,直接四舍五入的,导致一直提交失败,甚至写了不只一个版本的代码。

下面,我随便给出两个版本的结果,都能过。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5. int N, K;
  6. const int MAX_N = 10000;
  7. double L[MAX_N];
  8. bool binary_search_L(double mid_L)
  9. {
  10. int total = 0;
  11. for (int i = 0 ; i < N; ++i)
  12. {
  13. total += int(L[i] / mid_L);
  14. }
  15. return total >= K;
  16. }
  17. void solve()
  18. {
  19. double min_L = 0;
  20. double max_L = 10000000;
  21. for (int i = 0; i < 200; ++i)
  22. {
  23. double mid_L = (min_L + max_L) / 2;
  24. if (binary_search_L(mid_L))
  25. {
  26. min_L = mid_L;
  27. }
  28. else
  29. {
  30. max_L = mid_L;
  31. }
  32. }
  33. cout.setf(ios_base::fixed);
  34. cout.precision(2);
  35. cout << floor(min_L *100) / 100 << endl;
  36. //printf("%.2f",min_L);
  37. }
  38. int main()
  39. {
  40. ios::sync_with_stdio(false);
  41. cin.tie(0);
  42. cin >> N;
  43. cin >> K;
  44. for (int i = 0; i < N; ++i)
  45. {
  46. cin >> L[i];
  47. }
  48. solve();
  49. return 0;
  50. }
  51. #include <iostream>
  52. #include <cstdio>
  53. #include <algorithm>
  54. #include <iomanip>
  55. #include <cmath>
  56. using namespace std;
  57. const int N = 10000+10;
  58. const int K = 10000+10;
  59. int n,k;
  60. double a[N];
  61. bool greed(double mid)
  62. {
  63. int cnt=0;
  64. for(int i=0;i<n;++i)
  65. {
  66. cnt+=int(a[i]/mid);
  67. }
  68. if(cnt>=k)
  69. return true;
  70. else{
  71. return false;
  72. }
  73. }
  74. double Find()
  75. {
  76. double l=0,r=100010;
  77. while(r-l>1e-5)
  78. {
  79. double mid=(r+l)/2;
  80. if(greed(mid))
  81. {
  82. l=mid;
  83. }
  84. else
  85. {
  86. r=mid;
  87. }
  88. }
  89. return r;
  90. }
  91. int main()
  92. {
  93. double Max=-1;
  94. cin>>n>>k;
  95. for(int i=0;i<n;i++)
  96. {
  97. scanf("%lf",&a[i]);
  98. if(a[i]>Max){
  99. Max=a[i];
  100. }
  101. }
  102. sort(a,a+n);
  103. printf("%.2f\n",floor(Find()*100)/100);
  104. //cout<<setiosflags(ios::fixed)<<setprecision(2);
  105. //cout<<Find(Max)<<endl;
  106. return 0;
  107. }

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 poj1064(二分)Cable master

    //二分判断 /假定一个解并判断是否可行 题意:有n条绳子,长度分别为L[i]。如果从他们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多长