1. 程式人生 > >POJ T2456 Aggressive cows(二分搜尋:最大化最小值)

POJ T2456 Aggressive cows(二分搜尋:最大化最小值)

題解:

  可以套用二分搜尋求解最優解的模型(這個部落格

  題意是有N個牛舍,C頭牛,給出的牛舍位置並不按順序。因為牛之間會打架,所以要求兩兩牛之間能隔開的最大距離。(一頭牛一個牛舍)。

  這是二分搜尋最大化最小值的例子。

  C(d):安排牛的位置使得任意牛之間的距離大於等於d

  1.牛舍位置無序,先排序

  2. 第一頭牛放入第一個牛舍

  3.第i頭牛放入Xj的牛舍,則第i+1頭牛放入牛舍需滿足Xj + d <= Xk

程式碼

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1e5;
const int INF = 1e9; 
int sta[MAXN+5],n,c;
bool vis[MAXN+5];

bool judge(int x){
	int sp = 0;
	for(int i = 1; i < c; ++i){
		int np = sp+1;
		while(np < n && sta[np] - sta[sp] < x)
			np++;
		if(np == n) return false;
		sp = np;
	}
	return true;
}

void solve(){
	int L = -1,R = INF;
	while(R - L > 1){
		int mid = (L + R)/2;
		if(judge(mid)) L = mid;
		else R = mid;
	}
	printf("%d\n",L);
}

int main(){
	while(~scanf("%d%d",&n,&c)){
		for(int i = 0; i < n; ++i)
			scanf("%d",&sta[i]);
		sort(sta,sta+n);
		solve();
	}
	return 0;
} 

相關推薦

POJ T2456 Aggressive cows二分搜尋最大化

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

POJ3258 River Hopscotch二分法求最大化

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 t

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

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

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

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

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 2456 Aggressive cows二分-最大化

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

POJ 2456 Aggressive cows二分最大化

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

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

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

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

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

poj 2456二分+貪心最大化

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

POJ 3258 River Hopscotch最大化

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 t

River Hopscotch 最大化

每年,奶牛都會舉辦一場特別版的跳房子活動,其中包括在河中小心翼翼地從岩石跳到岩石。興奮發生在一條長而直的河流上,開始時有一塊岩石,最後有另一塊岩石,距離開始L單位(1≤L≤1,000,000,000)。沿著起始岩石和結束岩石之間的河流,出現N(0≤N≤50,000)更多的岩

POJ 2456 Aggressive cows最大化+二分

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

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

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

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

POJ2456 Aggressive cows 二分答案

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

POJ 2456 Aggressive cows (貪心 + 二分

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

Aggressive cows POJ 2456 (二分最大化)

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