Processing math: 100%

洛谷P5119 Convent 题解

题目

很好想的一道二分题,首先,二分一定满足单调性,而题目中非常明显的就是用的车越多,所用时间越少,所以可以枚举时间,判断是否可以比m少。

然后在二分时,更是要注意下标的一些问题,也要注意车和m作比较的顺序。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
int n, m, c;
int data[100100];
bool check(int a)
{
	int i = 1, pos = 1, ans = 0;
	while (i <= n)
	{
		if (i - pos + 1 > c || data[i] - data[pos] > a)
			ans++, pos = i; 
		i++;
		if (ans == m)
			return false;
	}
	if (ans == m)
		return false;
	return true;
}
int main()
{
	scanf("%d%d%d", &n, &m, &c);
	for (int i = 1; i <= n; i++)
		scanf("%d", &data[i]); 	
	sort(data + 1, data + 1 + n);
	int l = 1, r = 1e9 + 1;
	int mid;
	while (l < r)
	{
		mid = (l + r) >> 1;
		if (check(mid))
			r = mid;
		else
			l = mid + 1;
	}
	printf("%d", l);
}
posted @   DAGGGGGGGGGGGG  阅读(119)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示