ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

跳石头

2022-08-14 23:30:21  阅读:119  来源: 互联网

标签:return int mid 距离 石头 const include


/*
 *                                |~~~~~~~|
 *                                |       |
 *                                |       |
 *                                |       |
 *                                |       |
 *                                |       |
 *     |~.\\\_\~~~~~~~~~~~~~~xx~~~         ~~~~~~~~~~~~~~~~~~~~~/_//;~|
 *     |  \  o \_         ,XXXXX),                         _..-~ o /  |
 *     |    ~~\  ~-.     XXXXX`)))),                 _.--~~   .-~~~   |
 *      ~~~~~~~`\   ~\~~~XXX' _/ ';))     |~~~~~~..-~     _.-~ ~~~~~~~
 *               `\   ~~--`_\~\, ;;;\)__.---.~~~      _.-~
 *                 ~-.       `:;;/;; \          _..-~~
 *                    ~-._      `''        /-~-~
 *                        `\              /  /
 *                          |         ,   | |
 *                           |  '        /  |
 *                            \/;          |
 *                             ;;          |
 *                             `;   .       |
 *                             |~~~-----.....|
 *                            | \             \
 *                           | /\~~--...__    |
 *                           (|  `\       __-\|
 *                           ||    \_   /~    |
 *                           |)     \~-'      |
 *                            |      | \      '
 *                            |      |  \    :
 *                             \     |  |    |
 *                              |    )  (    )
 *                               \  /;  /\  |
 *                               |    |/   |
 *                               |    |   |
 *                                \  .'  ||
 *                                |  |  | |
 *                                (  | |  |
 *                                |   \ \ |
 *                                || o `.)|
 *                                |`\\) |
 *                                |       |
 *                                |       |
 */
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<unordered_map>
// #pragma GCC optimize(3)
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define scan(x)   scanf("%lld",&x)
#define int long long
#define lowbit(x) x&(-x) //二进制最低位所代表的数
#define PI 3.1415926535
typedef pair<int,int> PII;
int gcd(int a,int b){
    return b>0 ? gcd(b,a%b):a;
}
int exgcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x = 1,y = 0;
        return a;
    }
    int d = exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
const int N = 1e6+10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
void init()
{
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
}

int a[N];

int L,n,m;

bool check(int x)
{
    int tot = 0,last=0;
    for(int i=0;i<=n;i++)
    {
        if(a[i]-last<x)tot++;
        else last=a[i];
    }

    return tot<=m;
}

signed main()
{
    // 最小值最大 二分
    init();
    cin>>L>>n>>m;
    for(int i=0;i<n;i++)cin>>a[i];
    a[n]=L;
    int l = 0,r = L;
    while(l<r)
    {
        int mid = l+r+1>>1;
        if(check(mid))l=mid;
        else r = mid-1;
    }
    cout<<l<<endl;
}
View Code

思路:

最小距离最大值:二分

直接看题意找,找区间距离,如果区间距离符合我猜的距离,那么 计数器CNT加一 如果计数器比输入的m大,那么就不符合,返回错误。

还有二分的细节,如果题目要求找<=的只能用l+r+1>>1的板子

如果题目要求找>=用l+r>>1的板子

标签:return,int,mid,距离,石头,const,include
来源: https://www.cnblogs.com/yeonnyuui/p/16586711.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有