POJ3258 P2855 River Hopscotch

http://poj.org/problem?id=3258
http://noi.openjudge.cn/ch0111/10/
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
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
11
14
17
21

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


题解:
实际和猜数字一样,在1-100以内有一个数字是结果数字,那么,你要猜测这个数字,你一定先猜50,然后对方说大了,你就以1-50的范围再去猜,你一定会猜25,对方说小了,你就以25-50的范围去猜。这就是二分,时间复杂度是log2级别的。
本题也是一样,你可以先设一个最大的最短跳跃距离mid逐一判断,如果两个石头之间的距离已经大于mid,继续向右扫,如果小于,则要去掉这个石头。
举例:
0 20 23 26 29 31 51 (r=52)
这样我们先设0-50内有一个最小距离。
mid为(1+50)/2=25,则会移除20、23两块石头,再移除29、31两块石头,ans>m,r=mid=25
(这里思考,石头移除的多了什么意思?表示距离设的大了。因此我们要从1-25内的距离重新选择一个)
mid=(1+25)/2=13,则移除23、26、29、31,同理r=mid=13
mid=(0+13)/2=6,移除23,移除29,31,ans==m,l=mid=6
(这时可以再试着放大距离)
mid=(6+13)/2=9,移除23、26、31。ans==m了,就行了吗?不,明显20与29距离是9,还可以更长为11,那就加长试试,l=9
mid=(9+13)/2=11,移除23、26、29。此时还不能确定11就是最优解,因为还有12,所以l=11
mid=12,移除了23、26、29、31,所以r=12
mid=11,又区间是[l,r)左闭右开型的,即现在能确定距离就是mid=l=11了。

#include<iostream>
#include<cstdio>
using namespace std;
int l,r,s,n,m,river[50050],mid,ans;
int main()
{
    //cin>>s>>n>>m;
    scanf("%d%d%d",&s,&n,&m);
    for(int i=1;i<=n;i++)
//            cin>>river[i];
            scanf("%d",&river[i]);
    river[n+1]=s;
    river[0]=0;
    l=1;
    r=s-1;
    while(l<r) //
    {
        mid=(r+l)/2;
        if(mid==l)break; //说明加不长了,因为r还不变,如果l依然不变,则进入死循环 
        ans=0;
        int i=0;//初始化 
        int j=1;//初始化 
        while(j<=n+1) //这里就限制了j的范围 
        {
              while((river[j]-river[i])<mid && j<=n+1)            
                    j++;
              ans+=j-i-1;                      
              i=j;
              j++;
        }
        if(ans<=m)  l=mid;//加长试试 
        else r=mid;
    }
    cout<<mid<<endl;//或者l,比如10 11 (10+11)/2=10 mid==l 
    //while(1);
    return 0;
}

PS:例子转载。


愤怒的牛P1824

Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,
这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000,000)。
他的C(2<=C<=N)头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。
为了防止牛之间的互相打斗,Farmer John想把这些牛安置在指定的隔间,
所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?

程序名:aggr

输入格式:
    第1行:两个用空格隔开的数字N和C。
    第2~N+1行:每行一个整数,表示每个隔间的坐标。

输入样例:(aggr.in)
5 3
1
2
8
4
9

输出格式:
    输出只有一行,即相邻两头牛最大的最近距离。

输出样例:(aggr.out)
3

样例说明:
    Farmer John把他的三头牛分别放在坐标为148的隔间里,这样所得的最近距离是3

程序名:aggr

输入格式:
第1行:两个用空格隔开的数字N和C。
第2~N+1行:每行一个整数,表示每个隔间的坐标。

输入样例:(aggr.in)
5 3
1
2
8
4
9

输出格式:
输出只有一行,即相邻两头牛最大的最近距离。

输出样例:(aggr.out)
3

样例说明:
Farmer John把他的三头牛分别放在坐标为1、4、8的隔间里,这样所得的最近距离是3。

#include<iostream>
#include<cstdio>
#include<algorithm> 
using namespace std;
int n,m,ans; 
int a[100005],b[100005];
int main()
{
    freopen("aggr.in","r",stdin);
    freopen("aggr.out","w",stdout);
    cin>>n>>m;
    int nm=n-m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
//  for(int i=1;i<=n;i++)
//      cout<<b[i]<<endl;
    sort(a+1,a+n+1);
    int l=1,r=a[n]-a[1];//
//  cout<<l<<" "<<r;
    int s;
    while(l<r)//3 4, s一直是3,死循环 
    {
        s=(l+r)/2;
        if(s==l) break; 
        int bg=a[1],ans=0;
        for(int i=2;i<=n;i++)
        {
            if((a[i]-bg)<s) ans++;
            else bg=a[i]; 
        }
        if(ans>nm) r=s;// 
        else l=s;//如果正好,可以再把距离往大了放放
    }
    cout<<s<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值