NOIP2015 跳石头

最近一直在刷二分答案的题,先来总结一下lyd大佬对于二分答案的解释

在这里插入图片描述

借助二分,我们把求最优解的问题,转化为给定一个值mid,判定是否存在一个可行的方案评分达到mid的问题。


对于跳石头这个题来说,我们可能枚举去掉哪几个石头来构成方案的问题很难,但是我们可以通过二分出来它最后跳跃的最短距离可能是多少来进行选择

从而本质上二分答案讲问题转化为判定问题

最终本题解题思路就是,看看在某一个最短距离的情况下,能不能去掉小于等于m块石头的方案

如果某个距离答案满足了去掉的小于等于m的方案个数,那么我就看看比它小的可以吗,就是求这个可行区间的最小值,就是求答案在右边区间的左端点,这也就说明了应该用哪个二分模版

// Problem: P2678 [NOIP2015 提高组] 跳石头
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2678
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Code by: ING__
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return
#define Endl "\n"
#define endl "\n"

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int x, m, n;

int d[50000 + 10];

int len[50000 + 10];

bool check(int le){
	int cnt = 0;
	
	int i = 0; 
	int now = 0; // 一开始是在起点的 
	
	while(i < n + 1){
		i++;
		if(d[i] - d[now] < le){
			cnt++;
		}
		else{
			now = i;
		}
	}
	
	return m >= cnt;
	
}

int main(){
	cin >> x >> n >> m;
	
	int l = 1;
	int r = x;
	
	for(int i = 1; i <= n; i++){
		cin >> d[i];
	}
	
	d[n + 1] = x; // 注意题目给的距离含义,这是中点的位置
	
	while(l < r){
		int mid = (l + r + 1) / 2;
		if(check(mid)){
			l = mid;
		}
		else{
			r = mid - 1;
		}
	}
	
	cout << r << Endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值