日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二分入门——poj 2456 aggressive cows

發(fā)布時間:2023/12/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二分入门——poj 2456 aggressive cows 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這是一道二分的神奇貪心題,先上題目

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
    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.
題目翻譯:
大概就是說fj與他的牛相愛相殺,fj要給牛建棚子,每個棚子都在一條直線上,都有不同的坐標(biāo),但是牛并不喜歡跟其他的牛住得太近,所以要求找出最小的最大距離。
輸入:
一個棚子數(shù),一個牛數(shù)。
輸出:
最小的最大距離。

這個題很多人先懵在最小最大距離上,這個我們圖示樣例看一看:
所以,我們要找的最優(yōu)解就是一個放1,一個放4,一個9。
最小的距離就是4-1=3。
這樣的意思就是讓牛們住得最分散,找出最小的距離。
然后我們考慮到算法上去。
代碼(注釋詳解代碼):

#include<stdio.h> #include<cmath> #include<vector> #include<functional> #include<queue> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int maxn=100011;struct cmp{bool operator()(int &a,int &b){return a>b;} };//這是一個優(yōu)先隊列的比較函數(shù)。 int n,c,k; int array[maxn],qu[maxn]; int main() {priority_queue<int,vector<int>,cmp>que;//讓優(yōu)先隊列從小到大排序。 cin>>n>>c;for(int i=1;i<=n;i++){int x;cin>>x;que.push(x);}for(int i=0;i<n;i++){array[i]=que.top();que.pop();}//以上為輸入,將輸入的牛棚按與原點的距離排一遍 int ls=0,rs=1e9;//二分套路,總在一個范圍內(nèi)進(jìn)行尋找。這里沒有辦法知道最大值可能是什么情況,所以用一個1e9代替。 while(rs-ls>1){int midd=(rs+ls)/2;//以下為算法核心 int find=0,flag=1;//先找一個標(biāo)識變量find和一個判斷變量flag for(int i=1;i<c;i++)//因為尋找的是距離,所以遍歷的次數(shù)是總數(shù)-1{int cnt=find+1; // 而且我們默認(rèn)了第一頭牛在第一個棚子里while(cnt<n &&array[cnt]-array[find]<midd){cnt++;}if(cnt==n){flag=0;break;}elsefind=cnt;//這個地方是比較神奇的,我們先默認(rèn)了第一個牛在第一個棚子里,然后讓他開始找,跟每一個標(biāo)識find進(jìn)行比較,如果小于midd,就讓cnt++; //當(dāng)cnt在重復(fù)的比較中滿足了條件,一直加到了n,就是能找到小的,就break。 }if(flag)//他滿足了條件,就說明要么是找到了,要么是找小了,就讓它等于ls,接著二分進(jìn)去。 {ls=midd;}else{rs=midd;}//*********** }cout<<ls<<endl;return 0; }

其實這個題的輸出更是一個神奇的東西,這里輸出要輸出ls,因為我們每次都是在滿足條件的情況下讓ls=midd,所以,即使找到了答案,也是ls找到的,故要輸出ls。
這個二分題應(yīng)該已經(jīng)不是入門難度了……

總結(jié)

以上是生活随笔為你收集整理的二分入门——poj 2456 aggressive cows的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。