zoukankan      html  css  js  c++  java
  • POJ 3258 River Hopscotch(二分 最大化最小值)

    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 2
    2
    14
    11
    21
    17
    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).

    题意:

    整数数线上,有一个石头在原点代表起点,一个石头在L代表终点,他们之间有N个石头。
    想从起点移动到终点,只能从一个石头跳到相邻的下一个石头。求相邻石头间的最短距离。

    现在从起点终点以外的N个石头中移走M个石头,
    对于每一种移法,移完完都有一个新的最短跳跃距离s'(s'> = s)产生。
    请输出s'可能的最大值。

    题解:

    二分查找最大值。最好用一个变量记录前一个石头,如果移除的是中间连续的石头的话,处理会麻烦一点。具体实现请看代码。

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=50005,INF=1e9+5;
    int L,n,m;
    int a[maxn];
    bool check(int x)//移除M颗石头后,能使相邻的两颗石头的间距>=x
    {
    	int cnt=0;
    	int pre=0;//记录需要维护的前一颗石头
    	for(int i=1;i<=n+1;i++)
    	{
    		if(a[i]-a[pre]<x)
    			cnt++;
    		else
    			pre=i;//更新
    	}
    
    	return cnt<=m;
    }
    void solve()
    {
    	int lb=0,ub=INF;
    	while(lb+1<ub)
    	{
    		int mid=(lb+ub)>>1;
    		if(check(mid))
    			lb=mid;
    		else 
    			ub=mid;
    	}
    	cout<<lb<<endl;
    }
    int main()
    {
    	cin>>L>>n>>m;
    	for(int i=1;i<=n;i++)
    		cin>>a[i];
    	a[n+1]=L;
    	sort(a+1,a+n+1);
    	solve();
    	return 0;
    }
    
  • 相关阅读:
    程序员找工作必备 PHP 基础面试题 (四)
    Laravel 教程:使用Fast Excel解决导出超大 XLSX 文件(千万级)带来的内存问题
    ThinkPHP无限分类的使用
    PHP 的 interface 有什么用处?
    编写可读代码:通过提前返回来减少缩进
    调试事件的派发
    调试对象的构建
    [反汇编分析] 局部变量复用
    [IDA]批量载入结构体
    [反汇编分析]调用函数传入参数不一致时可能寄存器传入参数
  • 原文地址:https://www.cnblogs.com/orion7/p/7749374.html
Copyright © 2011-2022 走看看