zoukankan      html  css  js  c++  java
  • POJ

    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.
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 #include<vector>
    17 long long n,k;
    18 long long a[110000];
    19 long long panduan(long long zhi)
    20 {
    21     int add=1;
    22     int kaishi=1,jieshu=1;
    23     while(1)
    24     {
    25         if(a[jieshu]-a[kaishi]<zhi&&jieshu<=n)
    26         {
    27             jieshu++;
    28         }
    29         else if(a[jieshu]-a[kaishi]>=zhi)
    30         {
    31             add++;
    32             kaishi=jieshu;
    33         }
    34         else
    35             break;
    36     }
    37     //cout<<zhi<<"_"<<add<<endl;
    38     return add;
    39 }
    40 int  main()
    41 {
    42     scanf("%lld %lld",&n,&k);
    43     for(long long  i=1;i<=n;i++)
    44         scanf("%lld",&a[i]);
    45     sort(a+1,a+1+n);
    46     long long kaishi=0,jieshu=1e18;
    47     long long mid;
    48     long long t;
    49     while(kaishi<jieshu-1)
    50     {
    51         //cout<<kaishi<<"_"<<jieshu<<endl;
    52         mid=(kaishi+jieshu)>>1;
    53         if(panduan(mid)>=k)
    54             kaishi=mid;
    55         else
    56             jieshu=mid;
    57     }
    58     cout<<kaishi<<endl;
    59     return 0;
    60 }
    View Code
     
  • 相关阅读:
    easyui 删除数据表格
    项目中的五级地址联动效果(js)
    项目中树形结构的添加与立即删除该数据问题
    sqllite connectionstring setting
    sqllite 学习-1
    dxper 开发者论坛»开发者技术论坛
    CefSharp的引用、配置、实例
    WPF ContextMenu DataTemplate MenuItem Visibility 问题
    WPF ChromiumWebBrowser 网页背景透明
    WPF仿百度Echarts人口迁移图
  • 原文地址:https://www.cnblogs.com/dulute/p/7966712.html
Copyright © 2011-2022 走看看