1. 程式人生 > >Aggressive cows POJ 2456 (二分,最大化最小值)

Aggressive cows POJ 2456 (二分,最大化最小值)

Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14977 Accepted: 7219

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.

題目分析:先對隔間編號從小到大排序,則最大距離不會超過兩端的兩頭牛之間的差值,最小值為0。所以我們可以通過二分列舉最小值來求。假設當前的最小值為x,如果判斷出最小差值為x時可以放下C頭牛,就先讓x變大再判斷;如果放不下,說明當前的x太大了,就先讓x變小然後再進行判斷。直到求出一個最大的x就是最終的答案。

程式碼如下:

//
// Created by kzl on 17-7-25.
//

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxx = 1e5+500;
int n,c;
int cun[maxx];

bool flag(int num){
    int aa = cun[0];
    int temp = 1;
    for(int i=1;i<n;i++)
    {
        if(cun[i]-aa>=num){
            temp++; aa = cun[i];
        }
if(temp==c)return true;
    }
    return false;
}


int main(){
while (scanf("%d%d",&n,&c)!=EOF)
{
    for(int i=0;i<n;i++)scanf("%d",&cun[i]);
    sort(cun,cun+n);
    int l = 1,r = cun[n-1],mid = 0,ans = r+1;
    while(l<=r){
        mid = ((r-l)>>1)+ l;
        if(flag(mid))l = mid + 1;
        else  r = mid - 1;
    }
    printf("%d\n",l-1);
}
    return 0;
}

相關推薦

Aggressive cows POJ 2456 (二分最大化)

Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14977 Accepted: 7219 Description Farmer John has built

POJ 3273 二分最大化

Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20086 Accepted: 7916 Description Farmer

POJ 2456 Aggressive cows(二分搜尋最大化)

Aggressive cows Time Limit:1000MS Memory Limit:65536K Total Submissions:9062 Accepted:4504

POJ 2456 Aggressive cows(二分查詢 最大化

題意:給出n個牛棚的位置,選擇其中的m個給牛住,使得牛之間的距離的最小值最大。 首先我們可以知道距離的最小值一定產生於相鄰的牛之間,所以要保證相鄰牛之間的距離大於等於這個最小值d。所以把牛棚的位置從小到大排序,然後在最小的那個位置先放一頭牛,再找下一個牛棚放牛,下一個位

POJ 2456 Aggressive cows二分最大化

Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14220 Accepted: 6887 Description Farmer John has built

hihocoder 二分·二分答案【二分搜尋最大化】 (bfs)

題目 這道題做了幾個小時了都沒有做出來,首先是題意搞了半天都沒有弄懂,難道真的是因為我不打遊戲所以連題都讀不懂了? 反正今天是弄不懂了,過幾天再來看看。。。 題意:一個人從1點出發到T點去打boss,這個人有兩個屬性值,防禦值和戰鬥值,這兩個值成反比,為了打贏boss我

POJ 3045 Cow Acrobats (最大化)

amp 最小 space bool stdin using 自身 max pro 題目鏈接:click here~~ 【題目大意】 給你n頭牛疊羅漢。每頭都有自己的重量w和力量s,承受的風險數rank就是該牛上面全部牛的總重量減去該牛自身的力量,題目要求設計

Aggressive cows POJ - 2456 最大化 二分

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int maxn=111111; i

poj 2456 Aggressive cows二分+最大化

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21009 Accepted: 9984 Description Farmer John has built a n

POJ 2456 Aggressive cows二分-最大化

Description 農夫約翰搭了一間有N間牛舍的小屋。牛舍排在一條直線上,第i號牛舍在xi的位置。但是他的M頭牛對小屋很不滿意,因此經常相互攻擊。約翰為了防止牛之間相互傷害,因此決定把每頭牛都放在

POJ-2456 Aggressive cows---最大化(也就是求

思路 cst target main include IT urn cstring strong 題目鏈接: https://vjudge.net/problem/POJ-2456 題目大意: 有n個牛欄,選m個放進牛,相當於一條線段上有 n 個點,選取 m 個點, 使得相

POJ T2456 Aggressive cows二分搜尋:最大化

題解:   可以套用二分搜尋求解最優解的模型(這個部落格)   題意是有N個牛舍,C頭牛,給出的牛舍位置並不按順序。因為牛之間會打架,所以要求兩兩牛之間能隔開的最大距離。(一頭牛一個牛舍)。

POJ 25456 Aggressive cows二分最大化

Aggressive cowsTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Des

B - Aggressive cows POJ - 2456化)

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

poj 2456二分+貪心)(最大化

題目連結:http://poj.org/problem?id=2456 題目大意:有N件牛舍,M頭牛。現在要把M頭牛分到牛舍中,要求每兩頭牛之間的距離儘量大。輸出分配後的最小距離。 分析:用二分搜

NYOJ 586 瘋牛 & POJ 2456二分搜尋 + 貪心)(最大化

Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8752 Accepted: 4349 Description Farmer John has built a

H - Aggressive cows (POJ - 2456)

names out 因此 之間 rt+ 枚舉 i++ 問題 滿足 - 題目大意 農夫約翰搭了一間有n間牛舍的小屋。牛舍排在一條線上第i號牛舍在xi的位置。但是他的m頭牛對小屋很不滿意,因此經常互相攻擊。約翰為了防止牛之間互相傷害,因此決定把每頭牛都放在離其他牛盡可能

POJ - 3258 River Hopscotch 【二分--最大化

題目傳送門 題目描述:第一行輸入三個整數L,N,K,表示一個長度為L的河上有N塊石頭,任意移去K塊,問任意兩塊石頭之間的最小間隔是多少(包括起始點到第一塊石頭的間隔和最後一塊石頭到終點的間隔),接下來N行是N塊石頭距離起始點的位置。 解題思路:這是一道典型的利用二分最大化最小值的題。現在

Aggressive cows最大化問題)

Problem Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a stra

[二分-最大化]POJ

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement ta