二分搜索法-POJ3258-算法筆記

二分搜索法

是一種求最優解的方法。

思路

其思路是通過二分法,不斷縮小解的可能範圍,直到最優解。
其特點是,解有一定順序特徵,比如越大越可能是解,並且解可快速驗證。
我們就可以假設,一箇中間大小的數,驗證是否是解,然後縮小一半範圍。

典型問題,給定長度n的單調不降序列a0an1a_0…a_{n-1}和一個數K,求滿足aika_i \ge k條件下最小的i。不存在就輸出n。
逐個驗證則花費O(n)。
二分搜索的思路,是每次取中間的進行驗證,若滿足條件則最優解在一側區間,從而達到縮小範圍的目的,最終求得最優解。

實現

void solve(){
int lb=1,ub=n;
while(ub-lb>1){
int mid=(lb+ub)/2;
if(a[mid]>=k){
    ub=mid;
}else{
    lb=mid;
}
}
//ub是結果
}

stl實現

stl lower_bound 實現二分搜索。

例題 POJ3258

Description

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 takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers: L, N, and M
Lines 2…N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

解答思路

典型的最大化最小值問題
可行解方便驗證,最優解不易直接求得
那我們就從二分搜索去做。

vector<int> p;
int mm;
int l;
//驗證是否可行
bool verify(int d){
    int m=mm;//剩下的個數
    int cur=0;
    
    for(int i=0;i<p.size();i++){
        while(p[i]-cur<d){
            i++;
        }
        cur=p[i];
        m--;
        if(m==0)
        {
            if(l-cur>=d) return true;
            return false;
        }
    }
    return false;
}
int main(){
    int L,N,M;
    cin>>L>>N>>M;
    mm=N-M;
    l=L;
    int d;
    int lb=0,ub=L;
    
    for(int i=0;i<N;i++)
    {
        int temp;
        cin>>temp;
        p.push_back(temp);
    }
    sort(p.begin(),p.end());
    while(ub-lb>1){
        int mid=(ub+lb)/2;
        if(verify(mid)){
            lb=mid;
        }else{
            ub=mid;
        }
    }
    cout<<lb<<endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章