博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3258 River Hopscotch(二分搜索之最大化最小值)
阅读量:6244 次
发布时间:2019-06-22

本文共 2826 字,大约阅读时间需要 9 分钟。

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

 

Sample Input

25 5 2214112117

 

Sample Output

4

 

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

 

Source

 
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 #define N 50006 9 int dis[N];10 int L,n,m;11 bool solve(int min_dis){12 int last=0;13 int cnt=0;14 for(int i=1;i<=n+1;i++){15 if(dis[i]-dis[last]<=min_dis) cnt++;16 else last=i;17 }18 19 if(cnt>m) return true;20 return false;21 22 }23 int main()24 {25 26 while(scanf("%d%d%d",&L,&n,&m)==3){27 for(int i=1;i<=n;i++){28 scanf("%d",&dis[i]);29 }30 31 if(n==m){32 printf("%d\n",L);33 continue;34 }35 36 dis[0]=0;37 dis[n+1]=L;38 sort(dis+1,dis+n+1);39 int low=0;40 int high=L;41 while(low
>1;43 if(solve(mid)){44 high=mid;45 }46 else{47 low=mid+1;48 }49 }50 printf("%d\n",low);51 }52 return 0;53 }
View Code

 

转载地址:http://lasia.baihongyu.com/

你可能感兴趣的文章
《数据分析变革:大数据时代精准决策之道》一1.4 全面看待运营型分析
查看>>
一分钟自我介绍:阿里云CDN
查看>>
《iOS 8开发指南》——第6章,第6.5节实战演练——使用模板Single View Application...
查看>>
【观点】离开了信息化,大数据就是为他人作嫁衣
查看>>
《HTML5+CSS3网页设计入门必读》——1.4 分裂:WHATWG TF
查看>>
《JavaScript核心概念及实践》——第2章 基本概念 2.1 数据类型
查看>>
Linux有问必答:如何修复"fatal error: jsoncpp/json/json.h: No such file..."
查看>>
阿里数据库内核月报:2016年11月
查看>>
简单了解Disruptor(一)
查看>>
编写更好 Bash 脚本的 8 个建议
查看>>
Mavens实战 1.5小结
查看>>
《 硬件创业:从产品创意到成熟企业的成功路线图》——第1章 硬件创业概述 1.1 早期的创客们...
查看>>
《Android游戏开发详解》——第3章,第3.5节继承
查看>>
《Docker生产环境实践指南》——2.6 编排
查看>>
Docker学习(一)
查看>>
云端架美购,精品零距离
查看>>
Java设计模式--享元模式
查看>>
码栈开发手册(五)---可视化方式开发(模块详解--浏览图)
查看>>
每天一个设计模式之装饰者模式
查看>>
基于自定义日志打印的UDAF调试
查看>>