poj3258 二分 最小值最大化问题

River Hopscotch

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10842   Accepted: 4654

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题目大意:奶牛过河游戏,胆小的奶牛只敢跳往最近的石头,河长L米,里面有N块石头,给你每一块石头距离起始位置的距离,现在让你移除M块石头,使得相邻石头之间的距离最大,让你输出最长的距离是多少。思路分析:最近几天一直在做二分的的题目,导致做这道题的时候读懂题意,看了下数据范围就基本确定是用二分来写了,基本都差不多,但是每一道题都有需要值得注意的地方,比如为了防止溢出特地用了__int64(不造有没有用),本题弱还是贡献了两发wa,原因主要是在起点和终点的处理上,起点和终点的石头是不能移动的!后来把终点单独拿出来进行处理,其余的如果不满足就去掉右边的石头,成功A掉。代码:#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int maxn=50000+100;__int64 a[maxn];int n,m;bool check(__int64 x){    int t=0;    int i;    __int64 len;    __int64 now=a[0],next;    __int64 sum=0;    for(i=n;i>=0;i--)    {        if(a[n+1]-a[i]<x) t++;        else break;    }    int h=i;    for(i=1;i<=h;i++)    {       next=a[i];       len=next-now;       if(len<x)       {           t++;           if(t>m) return false;       }      else now=a[i];    }    return true;}int main(){    __int64 L;    while(scanf("%I64d%d%d",&L,&n,&m)!=EOF)    {        a[0]=0;        for(int i=1;i<=n;i++)            scanf("%I64d",&a[i]);        a[n+1]=L;        sort(a,a+n+2);        __int64 l=0,r=L;        __int64 ans=0;        while(l<=r)        {            __int64 mid=(l+r)>>1;            //cout<<mid<<endl;            if(check(mid)) ans=mid,l=mid+1;            else r=mid-1;        }        //cout<<check(4)<<endl;        printf("%I64d\n",ans);    }    return 0;}
时间: 2024-10-03 17:53:22

poj3258 二分 最小值最大化问题的相关文章

poj 2456 Aggressive cows,二分,最大化最小值

描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢? 输入 有多组测试数据,以EOF结束. 第

最大值最小化问题 和最小值最大化问题 ---(二分)

最大值最小化 即是当存在一个x为最大值的最小化,则x-1不成立,x+1可行,但他不满足最小,所以设边界最小值L,最大值R,二分查找第一个满足题意的, 例子: 把一个包含n个正整数的序列划分成m个连续的子序列.设第i个序列的各数之和为S(i),求所有S(i)的最大值最小是多少? 例如序列1 2 3 2 5 4划分为3个子序列的最优方案为 1 2 3 | 2 5 | 4,其中S(1),S(2),S(3)分别为6,7,4,那么最大值为7: 如果划分为 1 2 | 3 2 | 5 4,则最大值为9,不是

poj 2456 最小值最大化

http://poj.org/problem?id=2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows

poj3258——二分优化

poj3258——二分优化 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8201   Accepted: 3531 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock

[ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697   Accepted: 2893 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. T

poj 2112 floyd+Dinic最大流+二分最小值

题目大意是: K台挤奶机器,C头牛,K不超过30,C不超过200,每台挤奶机器最多可以为M台牛工作,给出这些牛和机器之间,牛和牛之间,机器与机器之间的距离,在保证让最多的牛都有机器挤奶的情况下,给出其中距离最长的一头牛移动距离的最小值. 首先用Floyd求出任意两点之间的最短距离,然后再用二分法限定最多的移动距离d,在求最大流时,搜索增广路的时候同时也判断距离有没有超过d就行了. 1 #include <cstdio> 2 #include <cstring> 3 #include

【二分查找-最大化平均值】POJ2976 - Dropping Test

[题目大意] 给出n组ai和bi,去掉k个使得a的总和除以b的总和最大. [思路] 也就是取(n-k)个数,最大化平均值,见<挑战程序设计竞赛>P144,最后公式为c(x)=((ai-x*bi)从大到小排列的前(n-k)个的和不小于0) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath>

最小值最大化,最大值最小化

问题描述把一个包含n个正整数的序列划分成m个连续的子序列.设第i个序列的各位上的数之和为S(i),求所有S(i)的最大值最小是多少? 例子: 序列1 2 3 2 5 4划分为3个子序列的最优方案为 1 2 3 | 2 5 | 4,其中S(1),S(2),S(3)分别为6,7,4,那么最大值为7: 如果划分为 1 2 | 3 2 | 5 4,则最大值为9,不是最小. 每次划分后,所有的序列的S[i]求出来,选择其中的最大值 每一种划分,都对应着一个最大的S[i] 求一种划分方法,使得S[i]的最大

POJ 3258 最小值最大化 二分搜索

题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值. 首先去理解题意,去除一些石头之后,使得跳跃的最短距离是最大的,这个跳跃的距离一定是一个值而且一定小于总距离,同时我们可以知道的是,如果移除某几块石头,以某一最短距离跳跃都满足的话,小于这个最短距离的话一定都满足,大于这个最短距离便不一定,所以二分搜索的好处在于可以精准地通过之前的判断对下一次的范围进