关闭

POJ 3258 River Hopscotch (二分法)

标签: ACM二分法POJ
273人阅读 评论(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网站的观点或立场

poj1700Crossing River(这过河真经典)

Description A group of N people wishes to go across a river with only one boat, which can at most c...
  • u010372095
  • u010372095
  • 2013-11-24 19:36
  • 1735

POJ 2104 K-th Number【整体二分 + 树状数组】

本来只是想学一下CDQ,还是先把整体二分搞懂一点。这题窝几个月前分别用划分树,树套树,主席树和挑战上介绍的分桶法实现了一发(然而现在都忘得差不多了) 最快的是划分树,其次是主席树,然后是树套树,...
  • Yukizzz
  • Yukizzz
  • 2016-04-29 01:09
  • 1394

【bzoj1143】 CTSC2008祭祀river 二分图匹配

貌似是我想少了,二分图不止只有最大匹配,先写一些结论吧,等着总结一下。 参考:http://endlesscount.blog.163.com/blog/static/8211978720126221...
  • u012288458
  • u012288458
  • 2015-08-17 20:03
  • 1131

POJ3258 River Hopscotch(二分法求最大化最小值)

Link:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS   Memory ...
  • Enjoying_Science
  • Enjoying_Science
  • 2017-11-21 20:48
  • 78

poj 3258 River Hopscotch(二分法,最小值最大化)

River HopscotchDescriptionEvery year the cows hold an event featuring a peculiar version of hopscotc...
  • blessLZH0108
  • blessLZH0108
  • 2017-07-08 08:42
  • 271

POJ 3258 River Hopscotch(二分法)

Description Every year the cows hold an event featuring a peculiar version of hopscotch that invo...
  • ShannonNansen
  • ShannonNansen
  • 2015-08-27 18:45
  • 237

POJ 3258-River Hopscotch(二分法-最大化最短距离)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12505 ...
  • MIKASA3
  • MIKASA3
  • 2017-02-09 14:40
  • 212

POJ3258 River Hopscotch(最大化最小值/二分法)

问题描述:Every year the cows hold an event featuring a peculiar version of hopscotch that involves caref...
  • karry_zzj
  • karry_zzj
  • 2017-04-18 00:26
  • 130

【二分法】POJ3258-River Hopscotch

【抱大腿】啊啊啊又是一道恶心的题目!!!这道题是出在二分法里面的,因为这跟前面的一道青蛙过河的题特别像但是不一样,按照青蛙过河那个思路来走根本行不通,正好要按照跟那个思路相反的想法来想才行~ ...
  • silent0001
  • silent0001
  • 2016-07-27 14:53
  • 219

POJ 3258 River Hopscotch【二分:最大化最小值】

Language: Default River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions...
  • Puyar_
  • Puyar_
  • 2017-06-27 15:02
  • 143
    个人资料
    • 访问:39378次
    • 积分:1897
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论