poj3258 二分最小值最大化
来源:互联网 发布:vnpy 知乎 编辑:程序博客网 时间:2024/06/07 10:17
如题:http://poj.org/problem?id=3258
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 distanceDi 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 toM 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 ofM rocks.
Input
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
Sample Input
25 5 2214112117
Sample Output
4
Hint
Source
思路:二分距离,对于每个距离d判断当前的N个石头能否删除小于M个小于距离d的石头,如果能,代表距离还要增大,否则,距离减小。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 50005
int a[MAXN];
int C(int d,int N,int M) //N个石头是否能删除掉小于M个<d距离的石头
{
int i;
int del=0;
int sum=0;
for(i=1;i<N;i++)
{
if((sum+=a[i]-a[i-1])<d)
del++;
else
sum=0;
}
if(del<=M)
return 1;
return 0;
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
int L,N,M;
int i;
cin>>L>>N>>M;
for(i=1;i<=N;i++)
cin>>a[i];
a[N+1]=L;
sort(a,a+N+2);
int l=0,r=L+1;
while(r-l>1)
{
int mid=(l+r)/2;
if(C(mid,N+2,M))
l=mid;
else
r=mid;
}
printf("%d\n",l);
return 0;
}
- poj3258 二分最小值最大化
- POJ3258 二分与最小值最大化
- poj3258 二分好题,最大化最小值
- POJ3258-River Hopscotch-二分+贪心【最小值最大化】
- River Hopscotch poj3258 (二分+贪心思想+最小值最大化)
- (最大化最小值),poj3258,River Hopscotch
- POJ3258 River Hopscotch(最大化最小值/二分法)
- POJ3258 River Hopscotch(二分法求最大化最小值)
- poj2456二分搜索最大化最小值
- poj3273(二分最大化最小值)
- poj3258 二分
- POJ3258 二分
- POJ3258 二分
- POJ3258【二分】
- poj3258 二分
- POJ 3273 Monthly Expense (二分&最大化最小值)
- POJ 3104 Drying (二分&最大化最小值)
- 二分 最大值最小化||最小值最大化 NOJ 1133
- Andriod 内存泄露问题的整理
- C++常见错误及解决方法
- SQLiteCursor和ContentProvider的线程安全问题
- Universal-Image-Loader.jar
- Service基础概念了解
- poj3258 二分最小值最大化
- HDU 1257 最少拦截系统
- 机房收费之细节优化
- 【linq学习笔记】1、linq to xml
- [LeetCode.Trick]Largest Number
- R的一些基本操作
- Android 扫描Scard卡所有的图片
- 如何监控 Nginx(第一篇)
- linux文件描述符与打开文件之间的关系