跳石头———经典二分

12 篇文章 0 订阅

传送门

可能是今天状态不好,一个很简单的二分,挺早之前就ac过了,但是今天牛客遇到了,发现写不出来,说明当时还没搞懂。

题意不说了,这里就说一下几个点:
1.在什么情况下才可以搬掉石头?
应该是石头2到石头1的距离小于二分出的答案mid的时候。为什么呢?因为mid是二分出来的答案,也就是最短跳跃距离的最大值,既然他是最短跳跃距离的最大值,那么必然不能存在两个石头的距离小于mid,否则与mid矛盾。
2.ans的更新
二分算法的几个点,其中一个就是ans的更新,这题ans的更新条件就是搬掉的石头 要小于等于 可搬掉的石头数,听上去很简(sha)单(bi),实际上也很简单

溜了溜了 补作业去

/*
 * @Author: hesorchen
 * @Date: 2020-04-14 10:33:26
 * @LastEditTime: 2020-06-09 20:14:35
 * @Link: https://hesorchen.github.io/
 */
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI cos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define FRE                              \
    {                                    \
        freopen("in.txt", "r", stdin);   \
        freopen("out.txt", "w", stdout); \
    }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
//==============================================================================

ll a[100010];
int main()
{
    IOS;
    ll l, n, m;
    cin >> l >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    a[++n] = l;
    ll L = 1, R = INF, ans;
    while (L <= R)
    {
        ll mid = L + R >> 1;
        ll res = 0, la = 0;
        for (int i = 1; i <= n; i++)
            if (a[i] - a[la] < mid)
                //如果当前石头到前一个石头的距离小于最短距离,那么需要把这块石头搬掉
                res++;
            else
                la = i;
        if (res <= m) //如果需要搬掉的石头小于m,那么mid满足条件
        {
            ans = mid;
            L = mid + 1;
        }
        else
            R = mid - 1;
    }
    cout << ans << endl;
    return 0;
}

/*
 
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值