二分入门——poj 2456 aggressive cows
這是一道二分的神奇貪心題,先上題目
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11714 Accepted: 5732
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
OutputLine 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.
題目翻譯:
大概就是說fj與他的牛相愛相殺,fj要給牛建棚子,每個棚子都在一條直線上,都有不同的坐標(biāo),但是牛并不喜歡跟其他的牛住得太近,所以要求找出最小的最大距離。
輸入:
一個棚子數(shù),一個牛數(shù)。
輸出:
最小的最大距離。
這個題很多人先懵在最小最大距離上,這個我們圖示樣例看一看:
所以,我們要找的最優(yōu)解就是一個放1,一個放4,一個9。
最小的距離就是4-1=3。
這樣的意思就是讓牛們住得最分散,找出最小的距離。
然后我們考慮到算法上去。
代碼(注釋詳解代碼):
其實這個題的輸出更是一個神奇的東西,這里輸出要輸出ls,因為我們每次都是在滿足條件的情況下讓ls=midd,所以,即使找到了答案,也是ls找到的,故要輸出ls。
這個二分題應(yīng)該已經(jīng)不是入門難度了……
總結(jié)
以上是生活随笔為你收集整理的二分入门——poj 2456 aggressive cows的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python爬虫实例: 爬取“最好大学网
- 下一篇: Python爬虫练习(一) 爬取新笔趣阁