关闭

POJ 3258 River Hopscotch (二分法)

标签: ACM二分法POJ
226人阅读 评论(0) 收藏 举报
分类:

传送门:http://poj.org/problem?id=3258
River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10889   Accepted: 4677

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 ≤ MN).

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



解题思路:因为直接计算比较困难,又因为只能搬整数块石头,所以使用二分法逼近答案。具体注意事项见代码~

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int a[50005];
int vis[50005];

int main()
{
    //freopen("test.txt","r",stdin);
    int l,m,n,i;
    int up,low,mid;
    int ans;
    scanf("%d%d%d",&l,&n,&m);
    a[0] = 0;
    a[n+1] = l;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n+1);
    low = 0;up = l + 1;mid = (low + up)/2;//特别注意这里要把上界声明为l+1,因为l本身可能是答案,例如数据1 0 0
    while(up-low>1)
    {
        //memset(vis,0,sizeof(vis));
        //ans = mid;
        int last = a[0];
        int k = 0;
        int flag = 0;
        for(i=1;i<=n;i++)
        {
            if(k>m)
            {
                flag = 1;
                break;
            }
            else if(a[i]-last<mid)
            {
                k++;
            }
            else
            {
                last = a[i];
            }
        }
        if(k>m)     flag = 1;
        if(a[n+1]-last<mid && k==m)     flag = 1;
        if(flag)    up = mid;
        else        low = mid;
        mid = (low+up)/2;
    }
    printf("%d\n",low);
    //cout << "Hello world!" << endl;
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:31475次
    • 积分:1796
    • 等级:
    • 排名:千里之外
    • 原创:152篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论