传送门

https://www.cnblogs.com/violet-acmer/p/9793209.html

题意:

  有 N 块岩石,从中去掉任意 M 块后,求相邻两块岩石最小距离最大是多少?

题解:

  二分答案(假设答案为res)

  定义 l = 0 , r = L ;

  mid = (l+r)/2 ;

  判断当前答案 mid 至少需要去除多少块岩石,如果去除的岩石个数 > M,说明当前答案mid > res,r=mid;反之,说明当前答案 mid <= res , l =mid;

AC代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5. const int maxn=5e4+;
  6.  
  7. int L,N,M;
  8. int dist[maxn];
  9.  
  10. bool Check(int mid)
  11. {
  12. int res=;
  13. int pre=;
  14. for(int i=;i <= N;++i)
  15. {
  16. if(dist[i]-dist[pre] < mid)
  17. res++;
  18. else
  19. pre=i;
  20. }
  21. return res <= M ? true:false;
  22. }
  23. int Solve()
  24. {
  25. sort(dist+,dist+N+);
  26. int l=,r=L+;
  27. while(r-l > )
  28. {
  29. int mid=l+((r-l)>>);
  30. if(Check(mid))
  31. l=mid;
  32. else
  33. r=mid;
  34. }
  35. return l;
  36. }
  37. int main()
  38. {
  39. scanf("%d%d%d",&L,&N,&M);
  40. dist[]=;
  41. for(int i=;i <= N;++i)
  42. scanf("%d",dist+i);
  43. printf("%d\n",Solve());
  44. return ;
  45. }

poj 3258"River Hopscotch"(二分搜索+最大化最小值问题)的更多相关文章

  1. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

  2. [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697   Accepted: 2893 D ...

  3. POJ 3258 River Hopscotch(二分查找答案)

    一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...

  4. poj 3258 River Hopscotch 题解

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

  5. POJ 3258 River Hopscotch

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11031   Accepted: 4737 ...

  6. POJ 3258 River Hopscotch (binarysearch)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...

  7. POJ 3258 River Hopscotch(二分答案)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...

  8. poj 3258 River Hopscotch(二分搜索之最大化最小值)

    Description Every year the cows hold an ≤ L ≤ ,,,). Along the river between the starting and ending ...

  9. POJ 3258 River Hopscotch(二分法搜索)

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

随机推荐

  1. lombok 使用 Idea

    Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的.import lombok.Getter;i ...

  2. delphi中 dataset容易出错的地方

    最近写delphi项目,用到的数据集中的dataset,一直修改exception啊,写下过程. 在对数据集进行任何操作之前,首先要打开数据集.要打开数据集,可以把Active属性设为True,例如: ...

  3. 关于事务回滚,rollback tran到底要不要写?

    关于事务回滚,有些不明白,不知道rollback tran在什么时候用. begin tran update 表1 update 表2 commit tran 这种写法,在更新表1或表2时出错,事务会 ...

  4. vue-cli:渲染过程理解2(vue init webpack方式创建)

    main.js: 入口文件 import Vue from 'vue' //引入node_modules中的vue import App from './App' //引入当前路径(src)下的App ...

  5. 中断MSI INTA

    转载https://blog.csdn.net/huangkangying/article/details/11178425 MSI VS INTx(Pin-based interrupt) MSI的 ...

  6. YUV格式与RGB格式

    YUV420介绍: YUV420格式是指,每个像素都保留一个Y(亮度)分量,而在水平方向上,不是每行都取U和V分量,而是一行只取U分量,则其接着一行就只取V分量,以此重复(即4:2:0, 4:0:2, ...

  7. 1、linux下对绝对路径和相对路径

    cd /  回到根目录         cd  /etc 回到根目录下的etc 目录下  绝对路径  路径写法是从根目录/ 写起来的. cd . 当前目录 cd .. 上层目录 cd ~回到自家的根目 ...

  8. sed 收集

    #删除倒数第二行的最后的逗号 一条命令 sed ':1;$b;N;/InnoDB/!b1;s/,\n)/\n)/'

  9. [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree

    题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...

  10. Django auth认证系统

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...