欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【二分】【2020.8.24NOIP模拟赛】选数排列

程序员文章站 2022-06-02 17:30:30
...

LinkLink

luogu T145305luogu\ T145305

DescriptionDescription

【二分】【2020.8.24NOIP模拟赛】选数排列

SampleSample InputInput

7 2 3
170 205 225 190 260 225 160

SampleSample OutputOutput

30

HintHint

【二分】【2020.8.24NOIP模拟赛】选数排列

SolutionSolution

排个序,直接二分
预处理出i     to     i+c1i\ \ \ \ \ to\ \ \ \ \ i +c - 1的差,然后直接判断一下就好了

CodeCode

#include<algorithm>
#include<iostream>
#include<cstdio>

using namespace std;

int p[500005], cha[500005];
int n, r, c, ans;
const int inf = 1e9;

bool check(int mid)
{
	int num = 0;
	for (int i = 1; i <= n - c + 1; ++i)
	{
		if (cha[i] <= mid) {
			num++;
			i += c - 1;
		}
	}
	return num >= r;
}

int main()
{
	scanf("%d%d%d", &n, &r, &c);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &p[i]);
	sort(p + 1, p + n + 1);
	for (int i = c; i <= n; ++i)
	 cha[i - c + 1] = p[i] - p[i - c + 1];
	int L = 0, R = inf;
	while (L <= R)
	{
		int mid = (L + R) / 2;
		if (check(mid)) R = mid - 1, ans = mid;
		else L = mid + 1;
	}
	printf("%d", ans); 
}
相关标签: 二分