zoukankan      html  css  js  c++  java
  • 疯牛-- Aggressive cows (二分)

    疯牛

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000).
    但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢?
     
    输入
    有多组测试数据,以EOF结束。 第一行:空格分隔的两个整数N和C 第二行——第N+1行:分别指出了xi的位置
    输出
    每组测试数据输出一个整数,满足题意的最大的最小值,注意换行。
    样例输入
    5 3
    1
    2
    8
    4
    9
    样例输出
    3
    题解:二分;
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long LL;
     8 const int MAXN=1e5+100;
     9 int m[MAXN];
    10 int N,C;
    11 bool js(int x){
    12     int t=m[0];
    13     int cnt=1;
    14     for(int i=1;i<N;i++){
    15         if(m[i]-t>=x)cnt++,t=m[i];
    16     }
    17     if(cnt>=C)return true;
    18     else return false;
    19 }
    20 int erfen(int l,int r){
    21     int mid;
    22     while(l<=r){
    23         mid=(l+r)/2;
    24         if(js(mid))l=mid+1;
    25         else r=mid-1;
    26     }
    27     return l;
    28 }
    29 int main(){
    30     while(~scanf("%d%d",&N,&C)){
    31         for(int i=0;i<N;i++)scanf("%d",m+i);
    32         sort(m,m+N);
    33         printf("%d
    ",erfen(0,m[N-1]-m[0])-1);//不明觉厉
    34 } 35 return 0; 36 }
    Aggressive cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9080   Accepted: 4512

    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.
  • 相关阅读:
    百度相关应用
    超实用js代码段一
    js模块化开发
    常见注入手法第一讲EIP寄存器注入
    异常处理第一讲(SEH),筛选器异常,以及__asm的扩展,寄存器注入简介
    32位汇编第六讲,OllyDbg逆向植物大战僵尸,快速定位阳光基址
    32位汇编第五讲,逆向实战干货,(OD)快速定位扫雷内存.
    32位汇编第四讲,干货分享,汇编注入的实现,以及快速定位调用API的数量(OD查看)
    32位汇编第三讲,RadAsm,IDE的配置和使用,以及汇编代码注入方式
    32位汇编第二讲,编写窗口程序,加载资源,响应消息,以及调用C库函数
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4963431.html
Copyright © 2011-2022 走看看