WA了 #1样例,求助求助~
查看原帖
WA了 #1样例,求助求助~
146659
clo_ver楼主2023/3/23 23:36
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long 
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
using namespace std;
const int maxn=1e5+5;
const int maxl=1e7;
int len,n,k,a[maxn],ma=0;

bool check(int x)  //距离的最大值
{
	int cnt=0,pos=a[1],num=1;
	for(int i=1;i<=len;i++)
	{
		if(a[num+1]==i)  //当前对应的位置上之前就有路灯
		{
			pos=i;
			num++;
		}
		else
		{
			if(i-pos>=x) 
			{
				cnt++; //新增设的路灯数量++
				pos=i;
			}
		}
	}
	if(cnt<=k) return true;
	else return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>len>>n>>k; //表示公路的长度,原有路标的数量,以及最多可增设的路标数量
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		ma=max(ma,a[i]); //找到最右端点
	}
	sort(a+1,a+1+n);
	int l=0,r=ma,mid;
	while(l<r)  //找最大值的最小值
	{
		mid=(l+r)>>1;
		if(check(mid)) r=mid;
		else l=mid+1;
	}
	cout<<l;
	return 0;
}
2023/3/23 23:36
加载中...