您的位置:首页 > 运维架构

POJ3258-River Hopscotch-二分+贪心【最小值最大化】

2016-02-27 11:15 429 查看
给你L,n,m

L是一条路总长度

n是路上n个石头

m是要移走m个石头(第一个和最后一个石头不能移走)

 

设X为剩下的n-m个石头里,石头之间相邻最近的距离

求这个X的最大值

二分X,对于每个X,我们贪心,从a[0]开始,把a[i]+x范围内的j个石头都移掉,接下来从i+j+1开始重复移石头。

如果最后移动的石头超过了m个,那么说明 这个X太大了答案取【left,mid-1】,如果小于等于m个,那么说明答案在[mid,right]之间

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
double eps=0.000001;

int n,m;
int tm[100005];
int bin(int x)
{
int i;
int num=0;
for (i=0;i<=n+1;i++)
{
int j=i+1;
while(tm[j]-tm[i]<x&&j<=n)
j++;
num+=j-i-1;
i=j-1;
}
return num;

}
int main()
{
int i;
int ll;
cin>>ll;
cin>>n>>m;
int minn=2147483647;
for (i=1;i<=n;i++)
{
scanf("%d",&tm[i]);
}
sort(tm+1,tm+1+n);
tm[0]=0;
tm[n+1]=ll;
int l=0;
int r=ll;
int ans=-1;
while(l<=r)
{
if (r-l<=1)
{
if (bin(r)== m)
ans =r;
else
ans=l;
break;
}
int mid=(l+r)>>1;
int ret=bin(mid);
if (ret<=m)
l=mid;
else
if (ret>m)
r=mid-1;

}
printf("%d\n",ans );
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: