bzoj4525: [Usaco2016 Jan]Angry Cows

bzoj4525: [Usaco2016 Jan]Angry Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 90  Solved: 71
[Submit][Status][Discuss]

Description

奶牛Bessie设计了一个游戏:“愤怒的奶牛”。游戏的原型是:有一些可爆炸的草堆分布在一条数轴的某些坐标上,玩家用弹弓把一头奶牛发射到数轴上。奶牛砸到数轴上的冲击波会引发附近的草堆爆炸。游戏的目标是玩家用一些奶牛炸掉所有的草堆。 
有N个草堆在数轴的不同位置,坐标为x1,x2,….,xn。如果玩家以能量R把奶牛发射到坐标x,就会引爆半径R及以内的的草堆,即坐标范围[x−R,x+R]的草堆都会燃爆。 
现在有K头奶牛,每头奶牛的能量都是R,请计算如果要引爆所有的草堆,最小的R是多少? 
 

Input

第一行:2个整数N(1≤N≤50,000)和K(1≤K≤10)。 
下面有N行,每行一个整数:x1,x2,…,xn,范围在[0…1,000,000,000]。 

Output

输出最小可能的R。

 
 

Sample Input

7 2
20
25
18
8
10
3
1

Sample Output

5

HINT

 

Source

二分答案+贪心验证

#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define N 50001
using namespace std;
inline int read() {
     int x=0,ch=getchar();
     while(ch<'0'||ch>'9') ch=getchar();
     while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
     return x;
}
int main () {
     int n,k,s[N],l,r,mid,last,now,ans=2147483647;
     scanf("%d%d",&n,&k);
     rep(i,1,n) s[i]=read();
     sort(s+1,s+1+n);
     l=1; r=500000000;
     while(l<=r) {
          mid=(l+r)>>1; 
          now=0; 
          rep(i,1,k) {
               last=s[now+1];
               while(s[now+1]-last<=mid*2&&now<n) ++now;
          }
          if(now>=n) ans=min(ans,mid),r=mid-1;else l=mid+1;
     }
     printf("%d",ans);
}
View Code
原文地址:https://www.cnblogs.com/Bloodline/p/5492423.html