[HDU]4004-The Frog's Games

The Frog's Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 10745 Accepted Submission(s): 4854

Problem Description
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).

Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.

Output
For each case, output a integer standing for the frog's ability at least they should have.

Sample Input
6 1 2
2
25 3 3
11
2
18

Sample Output
4
11

二分法解這道題

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
int d[500010];

int l,n,m;

int full(int mid){
    int num=0,k=1;
    for(int i=1;i<n+2;i++){
        num+=d[i]-d[i-1];
        if(num>mid){
            num=d[i]-d[i-1];
            k++;
        }
    }
    if(k<=m){
        return 1;
    }else{
        return 0;
    }
}


int main(){
    while(scanf("%d %d %d",&l,&n,&m)!=EOF){
    d[0]=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&d[i]);
    }
    d[n+1]=l;
    sort(d, d+n+1);
//    for(int i=0;i<n+2;i++){
//        cout<<d[i]<<" ";
//    }
//    cout<<endl;
    
    int left=0,right=l;
    for(int i=1;i<n+2;i++){
        left=max(left,d[i]-d[i-1]);
    }
    
    while (left<right) {
        int mid=(left+right)/2;
        if(full(mid)){
            right=mid;
        }else{
            left=mid+1;
        }
    }
    cout<<left<<endl;
}
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末裆蒸,一起剝皮案震驚了整個濱河市小染,隨后出現(xiàn)的幾起案子仁热,更是在濱河造成了極大的恐慌爵赵,老刑警劉巖坪创,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件诵盼,死亡現(xiàn)場離奇詭異九昧,居然都是意外死亡夷蚊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門傲诵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來凯砍,“玉大人箱硕,你說我怎么就攤上這事拴竹。” “怎么了剧罩?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵栓拜,是天一觀的道長。 經(jīng)常有香客問我,道長幕与,這世上最難降的妖魔是什么挑势? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮啦鸣,結(jié)果婚禮上潮饱,老公的妹妹穿的比我還像新娘。我一直安慰自己诫给,他們只是感情好香拉,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著中狂,像睡著了一般凫碌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胃榕,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天盛险,我揣著相機與錄音,去河邊找鬼勋又。 笑死苦掘,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赐写。 我是一名探鬼主播鸟蜡,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挺邀!你這毒婦竟也來了揉忘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤端铛,失蹤者是張志新(化名)和其女友劉穎泣矛,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體禾蚕,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡您朽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了换淆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哗总。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖倍试,靈堂內(nèi)的尸體忽然破棺而出讯屈,到底是詐尸還是另有隱情,我是刑警寧澤县习,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布涮母,位于F島的核電站谆趾,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏叛本。R本人自食惡果不足惜沪蓬,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望来候。 院中可真熱鬧跷叉,春花似錦、人聲如沸营搅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剧防。三九已至植锉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間峭拘,已是汗流浹背俊庇。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鸡挠,地道東北人辉饱。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像拣展,于是被迫代替她去往敵國和親彭沼。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容