The 36th ACM/ICPC Asia Regional Dalian Site 1003 The Frog's Games

来源:互联网 发布:了解茶的软件 编辑:程序博客网 时间:2024/05/17 01:41

The Frog's Games

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65768/65768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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 2225 3 311 218

Sample Output

411

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

总结:

不难发现,如果我们正向来看这个问题的话,我们要解决的问题就是从点集中选择m-1个点使得它们与相邻的两个点直接间最大距离最小,看到使最大距离最小的问题,从正面一般不好解决,从反面很容易想到二分法,所以我们直接二分答案即可
////  main.cpp//  The Frog's Games////  Created by 张嘉韬 on 16/9/15.//  Copyright © 2016年 张嘉韬. All rights reserved.//#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int l,n,m;int map[500000+10];void init(){    map[0]=0;    for(int i=1;i<=n;i++)    {        scanf("%d",&map[i]);    }    map[n+1]=l;    sort(map+0,map+n+1);}int find(int now,int p){    int temp=now;    while(map[temp]<=p)    {        temp++;    }    return temp-1;}int judge(int d){    int flag=0;    int father=0;    int now=0;    int counter=0;    while(1)    {        father=now;        int p=map[now]+d;        if(p<l) now=find(now,p);        else now=n+1;        counter++;        if(now==n+1) {flag=1; break;}        else if(father==now||counter>=m) break;    }    return flag;}int search(int s,int e){    if(s==e&&judge(s)) return s;    int m=(s+e)/2;    if(judge(m))  return search(s,m);    else return search(m+1,e);}int main(int argc, const char * argv[]) {    //freopen("/Users/zhangjiatao/Documents/ACM/input.txt","r",stdin);    while(scanf("%d%d%d",&l,&n,&m)==3)    {        init();       // find()        int ans=search(0,l);        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击