zoukankan      html  css  js  c++  java
  • codevs 2072 分配房间

    2072 分配房间

    二分

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 #define maxn 1000000
     5 #define LL long long
     6 
     7 LL n,m,num[maxn],ans;
     8 
     9 inline void read(LL &now)
    10 {
    11     char ch=getchar(); now=0;
    12     while(ch>'9'||ch<'0') ch=getchar();
    13     while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 
    14 }
    15 
    16 bool check(LL x)
    17 {
    18     int t=1,tot=1;
    19     for(int i=2;i<=n;i++)
    20         if(num[i]-num[t]>=x) tot++,t=i;
    21     return tot>=m?true:false;
    22 }
    23 
    24 int main()
    25 {
    26     read(n); read(m);
    27     LL l=0,r=0;
    28     for(int i=1;i<=n;i++) read(num[i]);
    29     sort(num+1,num+n+1);
    30     r=num[n]-num[1];
    31     while(l<r)
    32     {
    33         LL mid=(l+r)>>1;
    34         if(check(mid)) ans=mid,l=mid+1;
    35         else r=mid;
    36     }
    37     printf("%lld",ans);
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    1167E (尺取法)
    Report CodeForces
    Maximum Xor Secondary CodeForces
    Sliding Window POJ
    单调队列 Sliding Window POJ
    尺取法
    目标
    NOIP系列(续)
    NOIP系列
    近期目标
  • 原文地址:https://www.cnblogs.com/chen74123/p/7517965.html
Copyright © 2011-2022 走看看