Aggressive cows
程序员文章站
2022-03-14 23:08:57
...
基本的二分题目,check()函数中对每一个牛棚与其前一个牛棚距离和枚举的距离mid比较,如果大于等于mid,则可以放一头牛,最后对放的牛数量与要求放的牛数量比较
#include<bits/stdc++.h>
using namespace std;
int n,c;
int a[1000005];
int check(int mid)
{
int pre=a[0],num=1;
for(int i=1;i<n;i++)
{
if(a[i]-pre>=mid)
{
num++;
pre=a[i];
}
}
if(num>=c)
return 1;
return 0;
}
int main()
{
int ans=0;
cin>>n>>c;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int l=0,r=1000000000/c;
while(l<r)
{
int mid=(l+r+1)/2;
if(check(mid))
{
l=mid;
}
else
r=mid-1;
}
printf("%d",r);
return 0;
}
推荐阅读
-
洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
-
P2858 [USACO06FEB]Treats for the Cows G/S (区间DP)
-
洛谷 P1204 [USACO1.2]挤牛奶Milking Cows(模拟)
-
【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows
-
【洛谷P1204】【USACO1.2】挤牛奶Milking Cows
-
java P1204 [USACO1.2]挤牛奶Milking Cows
-
【模拟】洛谷 P1204 [USACO1.2]挤牛奶Milking Cows
-
洛谷--------P1204 [USACO1.2]挤牛奶Milking Cows
-
[USACO1.2]挤牛奶Milking Cows 差分
-
洛谷 1204 [USACO1.2]挤牛奶Milking Cows