zoukankan      html  css  js  c++  java
  • 疯牛

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cmath>
      4 #include<cstdio>
      5 using namespace std ;
      6 typedef long long LL;
      7 LL a[101000];
      8 int n,c;
      9 
     10 bool check(int x) //搜索
     11 {
     12     int cas=1,ch=a[0];
     13     for(int i=1;i<n;i++)
     14     {
     15         if(a[i]-ch>=x)
     16         {
     17             ch=a[i];
     18             cas++;
     19         }
     20         if(cas>=c)
     21             return true;
     22     }
     23     return false;
     24 }
     25 
     26     
     27     
     28 int reseach()  //二分查找
     29 {
     30     int l,mid,r;
     31     l=0;r=a[n-1]-a[0]; 
     32     while(l<=r)
     33     {
     34         mid=(l+r)/2;
     35         if(check(mid))
     36             l=mid+1;
     37         else
     38             r=mid-1;
     39     }
     40     return r;
     41 }
     42 
     43 
     44 int main()
     45 {
     46     
     47     while(~scanf("%d%d",&n,&c))
     48     {
     49         for(int i=0;i<n;i++)
     50         {
     51             scanf("%d",&a[i]);
     52         }
     53         sort(a,a+n);
     54         printf("%d
    ",reseach());
     55     }
     56 }
     57 
     58 
     59 
     60 
     61 
     62 
     63 
     64 
     65 
     66 
     67 
     68 
     69 
     70 
     71 
     72 
     73 
     74 
     75 
     76 
     77 
     78 
     79 
     80 
     81 
     82 
     83 
     84 /*#include <cstdio>
     85 #include <algorithm>
     86 using namespace std;
     87 
     88 typedef long long LL;
     89 LL a[101000];
     90 int n,c;
     91 int chack(int x)
     92 {
     93     int cas=1,ch=a[0];
     94     for(int i=1;i<n;i++)
     95     {
     96         if(a[i]-ch>=x)
     97         {
     98             ch=a[i];
     99             cas++;
    100         }
    101         if(cas>=c)
    102             return true;
    103     }
    104     return false;
    105 }
    106 int reaerch()  //二分搜索
    107 {
    108     int l=0,r=a[n-1]-a[0],mid;
    109     while(l<=r)
    110     {
    111         mid=(l+r)/2;
    112         if(chack(mid))
    113             l=mid+1;
    114         else
    115             r=mid-1;
    116     }
    117     return l-1;
    118 }
    119 int main()
    120 {
    121     while(~scanf("%d%d",&n,&c))
    122     {
    123         for(int i=0;i<n;i++)
    124             scanf("%lld",&a[i]);
    125         std::sort(a,a+n);
    126         printf("%d
    ",reaerch());
    127     }
    128     return 0;
    129 }*/
    View Code

    这题真坑回我了,题目读了好长时间没明白,上网上一看

    题意要表达的是:把C头牛放到N个带有编号的隔间里,使得任意两头牛所在的隔间编号的最小差值最大。

    就用二分查找+搜索;

  • 相关阅读:
    01
    王天宇0703作业
    0706作业
    0705作业
    0704作业
    0703作业
    数据库死锁语句脚本
    项目问题 : 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    工厂模式(Factory Patter)
    修改DevExpress中英文提示,将英文改为中文
  • 原文地址:https://www.cnblogs.com/WDKER/p/5367242.html
Copyright © 2011-2022 走看看