poj 3258 River Hopscotch
来源:互联网 发布:带端口的域名解析 编辑:程序博客网 时间:2024/06/14 03:52
题目链接:点击打开链接
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
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
Sample Input
25 5 2214112117
Sample Output
4
基本思路:这和之前坐过的一个题类似的,就是求最小值的最大化,用排序+二分.这里的最小跳跃是一次所跳的距离,当然是从距离最小的开始枚举了,而最大值是要根据拿掉m个石头后所满足的距离,二分枚举.
#include <iostream>#include<algorithm>#include<cstdlib>#include<cmath>using namespace std;int dist[50100];int main(){ int L,n,m; while(cin>>L>>n>>m) { dist[0]=0; dist[n+1]=L; for(int i=1; i<=n; i++) { cin>>dist[i]; } sort(dist,dist+1+n);///最小距离的话要先排序(从小到大) int left=0x3f3f3f3f; int right=L; for(int i=1; i<=n+1; i++) { left=min(left,dist[i]-dist[i-1]); } int ans=0,sum=0; while(right>=left) { ans=sum=0; int mid=(left+right)/2; for(int i=1; i<=n+1; i++) { sum=sum+dist[i]-dist[i-1]; if(sum<=mid)///因为是求最大跳跃的最小值,所以从前往后 ///把石头拿走,判断是否符合要求 { ans++; } else { sum=0; } } if(ans<=m)left=mid+1; else right=mid-1; } cout<<left<<endl; } return 0;}
- poj 3258 River Hopscotch
- poj-3258 River Hopscotch
- poj-3258 River Hopscotch
- poj 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ-3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258--River Hopscotch
- poj 3258 River Hopscotch
- Android View总结
- java常用的文件读写操作
- LocalActivityManager
- runTime用法
- Python如何将Unicode中文字符串转换成 string字符串
- poj 3258 River Hopscotch
- Small Game
- RabbitMQ Publish/Subscribe 发布/订阅
- Linux 系统定时任务
- 将html转化为JPG存档
- 最热门IT技术类网址大全(从开发者的角度归类IT站点)
- 2048小游戏
- 数据抓取必须学会的三种技术
- 深入浅出 classloader