zoukankan      html  css  js  c++  java
  • 最大化最小值 Aggressive cows

    Aggressive cows http://poj.org/problem?id=2456

    N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架。

    2<=N<=100000

    2<=M<=N

    0 <=xi<=109

    //////////////////////////////////////////////////////////////

    C(d):=可以安排牛的位置使得任意两头牛的间距都不小于d

    使用二分搜索法解决:

    //参考文献:挑战程序设计大赛(第二版)
    /************************************************************************* > File Name: AggressiveCows_poj2456.cpp > Author: spzhao > Mail: spzhaol@163.com > Created Time: 2015年10月14日 星期三 20时25分30秒 ************************************************************************/ #include<iostream> #include <cstdio> #include <algorithm> #include <cstring> #define INF 1000000000 using namespace std; int N,K; int x[100005]; bool C(int d) { int last = 0; for (int i = 1;i < K;i++) { int crt = last+1;                    // 只需要比较K-1次找出最适合的值d来放置K头牛,用last & crt 来表示上一头牛和当前牛的位置 while(crt < N && x[crt] - x[last] < d) crt++; if (crt == N) return false;             // 到达最大值N说明d的值小了 last = crt; } return true; } int main () { cin >> N >> K; for (int i = 0;i < N;i++) scanf("%d",&x[i]); sort(x,x+N); int l = 0,r = INF; while(r - l > 1) { int mid = (l+r)/2; if (C(mid)) l = mid; else r = mid; } printf("%d ",l); return 0; }

      

  • 相关阅读:
    理解MySQL——索引与优化
    ArrayList vs LinkedList vs Vector
    Java集合框架的接口和类层次关系结构图
    Java集合的10个最常见问题
    Mysql 中的事件//定时任务
    Mysql中的函数
    Mysql中的触发器
    Mysql中的存储过程
    Oracle数据库表的一些宏处理
    Oracle查询和过滤重复数据
  • 原文地址:https://www.cnblogs.com/ediszhao/p/4878990.html
Copyright © 2011-2022 走看看