zoukankan      html  css  js  c++  java
  • poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description

    Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). 
    
    His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

    Input

    * Line 1: Two space-separated integers: N and C 
    
    * Lines 2..N+1: Line i+1 contains an integer stall location, xi

    Output

    * Line 1: One integer: the largest minimum distance

    Sample Input

    5 3
    1
    2
    8
    4
    9

    Sample Output

    3

    Hint

    OUTPUT DETAILS: 
    
    FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. 
    
    Huge input data,scanf is recommended.

    Source

     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<map>
     6 #include<set>
     7 using namespace std;
     8 #define N 100006
     9 #define inf 1<<32
    10 int x[N];
    11 int n,m;
    12 bool solve(int d){
    13     int cnt=1;
    14     int last=0;
    15     for(int i=1;i<n;i++){
    16         if(x[i]-x[last]>=d) {
    17             cnt++;
    18             last=i;
    19         }
    20     }
    21     if(cnt>=m) return false;
    22     return true;
    23 }
    24 int main()
    25 {
    26 
    27     while(scanf("%d%d",&n,&m)==2){
    28         for(int i=0;i<n;i++){
    29             scanf("%d",&x[i]);
    30         }
    31         sort(x,x+n);
    32         int low=0;
    33         int high=x[n-1]-x[0];
    34         while(low<high){
    35             int mid=(low+high)>>1;
    36             if(solve(mid)){
    37                 high=mid;
    38             }
    39             else{
    40                 low=mid+1;
    41             }
    42         }
    43         printf("%d
    ",low-1);
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    Cglib的动态代理
    idea中隐藏.idea文件夹和.iml文件
    JDBC工具类创建及使用
    JDBC的配置及使用入门
    mybatis的入门
    动态代理的具体实现
    【Flask】WTForms基本使用
    【Flask】Flask-Migrate基本使用
    【Flask】Flask-Sqlalchemy使用笔记
    【Flask】Sqlalchemy 子查询
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4780456.html
Copyright © 2011-2022 走看看