Convention —— 二分
作者:互联网
Description
Input
Output
Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.
Sample Input
6 3 2
1 1 10 14 4 3
Sample Output
4
Hint
题意:
有n个人要上车,你有m辆车,每个车最多能容纳c个人,给你每个人到车站的时间,问你让所有人都能上车,并且人等待最长的时间最短,问你最短的等待时间是多少。
题解:
最大值最小,二分
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int n,m,c;
int check(int x)
{
int sum=0,car=0,sta=0;
for(int i=1;i<=n;i++)
{
if(sum==0)
sum++,sta=i;
else
{
if(a[i]-a[sta]>x)
car++,sum=1,sta=i;
else
sum++;
}
if(sum==c)
car++,sum=0;
if(car>m)
break;
}
if(sum)
car++;
if(car>m)
return 0;
return 1;
}
int main()
{
scanf("%d%d%d",&n,&m,&c);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
int low=0,high=1e9+7,mid,ans=0;
while(high>=low)
{
mid=low+high>>1;
if(check(mid)==0)
low=mid+1;
else
high=mid-1,ans=mid;
}
printf("%d\n",ans);
return 0;
}
标签:二分,int,car,sum,mid,++,low,Convention 来源: https://blog.csdn.net/tianyizhicheng/article/details/88371884