首页 > 其他分享 >POJ--2456 Aggressive cows(二分搜索/最大化最小值)

POJ--2456 Aggressive cows(二分搜索/最大化最小值)

时间:2023-02-09 23:23:08浏览次数:51  
标签:最大化 lb -- ll 2456 current int 最小值 POJ

记录
22:55 2023-2-9

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

reference:《挑战程序设计竞赛(第2版)》3.1.3 p142

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

二分搜索,最大化最小值。
这里判断的条件C(d) = 可以安排牛的位置使得最近两头牛的距离不小于d。最小值体现在 距离 >= d (即最小值为d)
题目是要求满足C(d)的最大的d,这是最大化。(即求最大的)
这个最小值是指牛之间距离最小值,最大化是把距离最小值最大化。
比如两头牛,位置为1 3 4 5。
最小值取1 取2 都可以 都满足任意俩头牛距离大于d。
最大化就是在这俩个之间取大的那个,即2。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX_N 100000
typedef long long ll;
const int INF = 0x3f3f3f3f;
ll N, M;
ll L[MAX_N + 1];

bool check(int x) {
    int last = 0, current = 0;
    for(ll i = 1; i < M; i++) {
        current = last + 1;
        while (current < N && L[current] - L[last] < x) {
            current++;
        }
        if(current == N) return false;
        last = current;
    }
    return true;
}

void solve() {
    sort(L, L + N);
    int lb = 0, ub = INF;

    while (ub - lb > 1) {
        int mid = (lb + ub) / 2;
        if(check(mid)) {
            lb = mid;
        } else {
            ub = mid;
        }
    } 
    printf("%d\n", lb); 
}

int main() {
    scanf("%d %d", &N, &M);
    for(ll i = 0; i < N; i++){
        scanf("%lld", &L[i]);
    }
    solve();
}

标签:最大化,lb,--,ll,2456,current,int,最小值,POJ
From: https://www.cnblogs.com/57one/p/17107458.html

相关文章

  • 罗技Logitech M557蓝牙鼠标更换左键微动开关【已修复】
    故障:鼠标左键单击变双击维修:更换微动开关这里我选择的是欧姆龙微动【蓝】(店家号称寿命有5000W次)本来以为很简单的事情,结果反复折腾了一下午~(大无语事件)于是顺便做一下......
  • 【牛客刷题】BM50 两数之和
    本题的链接:BM50两数之和最初拿到这个题目首先想到的就是两个指针,然后向后遍历,于是写出来的代码也简明易懂:packagemain/****@paramnumbersint整型一维数组*......
  • 77、thymeleaf判断是否某个值包含某个字段
    进行注册时,我们在后端编写vo接受前端数据,并使用JSR303进行数据校验当前端通过表单提交数据时,我们的后端处理如下:@Valid表示这个数据要被校验BindingResult可以收集JSR......
  • 预处理指令详解(C语言
    一、预处理符号预处理符号是C语言内置的符号,是可以直接使用的。其中,若遵顼ANSIC,则__STDC__为1,否则未定义。二、#define1)定义标识符define可以用来定义标识符,其语法......
  • 蜗牛式学习Java--运行我的第一个程序
    安装jdk安装idea工具  packagecom.itheima;//java中最小组成单位是classpublicclassHelloWorld{//main是Java程序的入口publicstaticvoidmain......
  • 【Web】emscripten编译ffmpeg
    编译命令:emconfigure./configure--cc="emcc"--cxx="em++"--ar="emar"--ranlib=emranlib--prefix=$(pwd)/dist--enable-cross-compile--target-os=none--arch=x86......
  • 算法刷题 Day 35 | ● 860.柠檬水找零 ● 406.根据身高重建队列 ● 452. 用最少数量
    860.柠檬水找零本题看上好像挺难,其实挺简单的,大家先尝试自己做一做。https://programmercarl.com/0860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6.htmlTips:......
  • 函数
    常用函数大小写转换lower()使函数内的字符变成小写upper()使函数内的字符变成大写截取字符substr()substr(字段,截取的位置,截取的位数)从第5个字符开始截取,截取......
  • CFS三层靶机-内网环境渗透
    <1>靶场介绍及环境配置三个主机的网络环境拓扑图,攻击机的网段在192.168.236.0/24,三台靶机的IP地址分别如图:上面的Target1、2、3分别对应CentOS7、Ubuntu、Windows7三......
  • Asp.net: sometimes OutOfMemoryException when many w3wp processes run
    Asp.net:sometimesOutOfMemoryExceptionwhenmanyw3wpprocessesrunAskQuestionAsked 4years,11monthsagoModified 4years,11monthsagoIhaveani......