zoukankan      html  css  js  c++  java
  • POJ 3258:River Hopscotch 二分的好想法

    River Hopscotch
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 9326   Accepted: 4016

    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, Lunits 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 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: LN, 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).

    题意是给了n个石头,给了它们到起点的距离,还有一个终点。问要去掉这n个石头中的m个,使得其间距的最小值最大。

    一开始在想,这个题怎么二分。最后也是看了别人的思路才反应过来,二分最小值,再比较最小值是mid时去掉的石头个数来改动left还是right。这种想法真是很好,尤其是遍历一遍就知道当最短值是mid时去掉的石头个数,那处的想法很nice,涨姿势~

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    int L, N, M;
    int dis[50005];
    
    bool check(int mid)
    {
    	int i,before = 0,cnt = 0;
    	for (i = 1; i <= N + 1; )
    	{
    		if (dis[i] - dis[before] >= mid)//这块用于判断是否去掉石头
    		{
    			i++;
    			before = i - 1;
    		}
    		else
    		{
    			i++;
    			cnt++;
    		}
    	}
    	if (cnt <= M)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    int main()
    {
    	//freopen("i.txt","r",stdin);
    	//freopen("o.txt","w",stdout);
    
    	int i,left,right,mid;
    	cin >> L >> N >> M;
    	
    	dis[0] = 0;
    	dis[N + 1] = L;
    	
    	for (i = 1; i <= N; i++)
    	{
    		cin >> dis[i];
    	}
    	
    	sort(dis, dis + 1 + N);
    
    	left = 0;
    	right = 2*L;//可能要把所有的石头都去掉,所以可能取到最大值L,这里索性将右端点扩大了很多
    
    	while (right - left > 1)
    	{
    		mid = (right + left) / 2;
    		if (check(mid))
    		{
    			left = mid;
    		}
    		else
    		{
    			right = mid;
    		}
    	}
    	cout << left << endl;
    	return 0;
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    MySQL数据优化
    帆软报表(finereport)决策平台笔记
    帆软报表(finereport)实现自动滚屏效果
    帆软报表(finereport)动态列查询
    帆软报表(finereport) 复选框多值查询
    关于Thinkphp实现Excel实现创建sheet子分页
    【STM32F407的DSP教程】第25章 DSP变换运算-快速傅里叶变换原理(FFT)
    叱咤风云的ThreadX全家桶正式加入开源免费的大浪潮中
    【STM32H7的DSP教程】第24章 DSP变换运算-傅里叶变换
    【STM32F429的DSP教程】第24章 DSP变换运算-傅里叶变换
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785757.html
Copyright © 2011-2022 走看看