二分和枚举(主要是二分思想)

本文探讨了二分法在解决最大最小值问题中的应用,通过疯牛问题和青蛙过河问题的例子,阐述了如何利用二分枚举找到最大最小距离,并结合贪心思想进行优化。此外,还提到了二分法在解决方程求解问题中的作用,如求解8x^4+7x^3+2x^2+3x+6-m=0的解。
摘要由CSDN通过智能技术生成

通常二分的题目都会使left=0,right=可能的最大值,然后再left 和right之间寻找最大值。
而最重要的就是能够把二分题目分析称这个思想。(目前,我的个人理解)

第一个是求最大的最小值,第二个是最小的最大值,好好理解一下

疯牛这道题就是先排序,然后知道最大的距离,在0 和MAX之间寻找答案
judge的想法是:判断这个当前距离,如果有另一个栅栏与他的距离大于当前距离,则假定可以安排牛,然后从当前牛开始继续寻找距离比当前距离大的。。
疯牛
时间限制:1000 ms | 内存限制:65535 KB
难度:4

描述
农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,…,xN (0 <= xi <= 1,000,000,000).
但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢?

输入
有多组测试数据,以EOF结束。
第一行:空格分隔的两个整数N和C
第二行——第N+1行:分别指出了xi的位置
输出
每组测试数据输出一个整数,满足题意的最大的最小值,注意换行。
样例输入
5 312849
样例输出
3

题意:简单的说就是给你一段长度,在这一段中给出m个点,然后在这m个点中选出k个点,让这k个点之间相邻两个点的之间距离的最小值最大

思路:通过二分枚举这个最小值,然后通过贪心的思想找出满足要求的最大的这个最小值

解析:——二分枚举 + 贪心
这道题用到了刘汝佳算法入门经典上贪心那一节讲的算法,用二分枚举满足条件的最大距离,
依次做相应判断.本题不需要担心最后求出的距离不能适应题目中的隔间间的距离,
因为二分枚举之后是按照贪心发判断的,如果当前距离满足要求,会继续增大枚举的距离,
一直到无法满足要求为止,即最后结果一定满足是隔间间的距离 .

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int n, c;
int pos[100005];

bool judge(int k)    
{
    int cnt = 1;
    int st = pos[0];
    for(int i = 1; i < n; ++i)
    {
        if(pos[i] - st >= k)
        {
            ++cnt;
            if(cnt >= c)
                return true; 
            st = pos[i];
        }
    }
    return false;
}

int binary_search()  // 二分枚举满足条件的最大距离 
{
    int left = 0;
    int right = pos[n-1] - pos[0];
    int mid = (left + right) >> 1;
    while(left <= right)
    {
        if(judge(mid))  // 所求距离 >= mid,可以继续增大试探 
            left = mid+1;
        else            // 所求距离 < mid,所以必须减小来试探 
            right = mid-1;
        mid = (left + right) >> 1;
    }
    return left-1;
}

int main()
{
    while(~scanf("%d%d", &n, &c))
    {
        for(int i = 0; i < n; ++i)
            scanf("%d", &pos[i]);
        sort(pos, pos+n);
        printf("%d\n", binary_search());
    }
    return 0;
}

Q:青蛙过河问题:
青蛙过桥
时间限制:1000 ms | 内存限制:65535 KB
难度:3

描述
The annual Games in frogs’ kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog’s longest jump distance).

输入
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值