二分(POJ-3258)

/**********
Author Smile
二分最重要的就是分析状态,在判定条件中一定要把情况考虑清楚
决定AC否
注:特别是 =
注:在写输出的时候,思考跳出来的前一个状态的符合情况的可能值
二分的答案就是前一种情况的可能值
这就需要将题目分析透彻了 
注:再写 l = mid + 1, r = mid 时 要注意那个是能更上一层楼 
https://vjudge.csgrandeur.cn/contest/477149#problem/C
**********/
#include <iostream>
#include <algorithm>

using namespace std;
const int N = 5e4 + 10;
int l, n, m;
int w[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> l >> n >> m;
	w[1] = 0;
	for (int i = 2; i <= n + 1; ++i)
	{
		cin >> w[i];
	}
	w[n + 2] = l;
	sort(w + 1, w + 2 + n);
	int x = 0, y = l, mid, maxx = 0, r;
	l = x, r = y;
	while (l < r)
	{
		mid = (l + r) / 2;
		int ans = 0, pos = w[1];
		for (int i = 2; i <= n + 2; ++i)
		{
			if (w[i] - pos <= mid)
				ans++;
			else 
				pos = w[i];	
		}
		if (ans > m)
			r = mid;
		else
			l = mid + 1;		
	}
	cout << l << endl;
	return 0;
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值