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

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

題意:把C頭牛放到N個帶有編號的隔間裡,使得任意兩頭牛所在的隔間編號的最小差值最大。例如樣例排完序後變成1 2 4 8 9,那麼1位置放一頭牛,4位置放一頭牛,它們的差值為3;最後一頭牛放在8或9位置都可以,和4位置的差值分別為4、5,和1位置的差值分別為7和8,不比3小,所以最大的最小值為3。

解題思路:

  • 以後看見求最小值中的最大值或者求最大值中的最小值,都要想到用二分這種模板。
  • 用求最小值中的最大值來寫的大致思路:

    • 首先將要處理的序列從小到大sort一次
    • 然後求出最小值能取得範圍為[0,arr[n]-arr[1]]。即最小值最小為0,最大可取的最小值為 arr[n]-arr[1]。
    • 將最小值可取的這個範圍進行二分。如果check滿足,就繼續找更大的符合條件的x,直到l>r為止。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long LL;
const int N = 100000 + 100;
LL arr[N];
int n,c;
bool check(LL x)
{
    LL tmp = arr[0
] + x; int cnt = 1; for(int i=1;i<n;i++) { if(arr[i]>=tmp) { cnt++; tmp = arr[i] + x; if(cnt==c) return true; } } return false; } int binsearch(LL l,LL r) { LL mid; while(l<=r) { mid = (l+r)/2
; if(check(mid)) l = mid +1; else r = mid-1; } return l-1; } int main() { scanf("%d %d",&n,&c); for(int i=0;i<n;i++) { scanf("%lld",&arr[i]); } sort(arr,arr+n); printf("%d\n",binsearch(0,arr[n-1]-arr[0])); return 0; }

相關推薦

POJ 2456 Aggressive cows最大化+二分

題意:把C頭牛放到N個帶有編號的隔間裡,使得任意兩頭牛所在的隔間編號的最小差值最大。例如樣例排完序後變成1 2 4 8 9,那麼1位置放一頭牛,4位置放一頭牛,它們的差值為3;最後一頭牛放在8或9位

POJ 2456 Aggressive cows二分-最大化

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

POJ 2456 Aggressive cows 二分

Once cati and res cat -a isp ive pen 題目傳送門 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000)

POJ 2456 Aggressive cows二分

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 pos

POJ 2404 Jogging Trails權完美匹配

pan [0 const 之間 brush 歐拉回路 如果 ref %d 【題目鏈接】 http://poj.org/problem?id=2404 【題目大意】   給出一張圖,求走遍所有的路徑至少一次,並且回到出發點所需要走的最短路程 【題解】

3971 -二分答案最大化

Assemble 題意:你有b塊錢,想要組裝一臺電腦。給出n個配件格子的種類,品質因子和價格, 要求每種型別的配件各買一個。總價格不超過b,且品質最差的配件的品質因子儘量大。 思路:求出單個的最大值,這是二分答案的上界,0為下界,求一個最小值的最大化

POJ 1325 Machine Schedule 點覆蓋 && 二分大匹配

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"&g

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

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

poj 2456 Aggressive cows二分+最大化

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

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

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

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

POJ 3621 Sightseeing Cows優比例環+SPFA檢測

span fort exp ros 說明 6.0 lines choice stdio.h Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submission

POJ 3164 Command Network樹形圖模板題+詳解

noop clu html 建立 eof std const temp pri http://poj.org/problem?id=3164 題意: 求最小樹形圖。 思路: 套模板。 引用一下來自大神博客的講解:http://www.cnblogs.co

POJ 2456 Aggressive cows ( 二分 && 貪心 )

oca 繼續 con %d style code algo ++ family 題意 : 農夫 John 建造了一座很長的畜欄,它包括N (2 <= N <= 100,000)個隔間,這些小隔間依次編號為x1,...,xN (0 <= xi <= 1

poj 3308 Paratroopers點權覆蓋

ORC pst nic contain namespace align accep cstring ret Paratroopers Time Limit: 1000MS Memory Limit: 65536K Total Submis

POJ 3216 Repairing Company路徑覆蓋

false 時間 cpp algorithm true 持續時間 set rgb AR POJ 3216 Repairing Company id=3216">

POJ 2456 Aggressive cows

end ase cin std code sync algo poj while 二分答案+貪心 #include <iostream> #include <algorithm> using namespace std; const int MA

POJ - 3020 Antenna Placement覆蓋路徑

bool 內容 res col 兩個 -s != 感覺 就是 ---恢復內容開始--- https://vjudge.net/problem/POJ-3020 題意 *--代表城市,o--代表空地 給城市安裝無線網,一個無線網最多可以覆蓋兩座城市,問覆