
第1行:3个数N X M,中间用空格分隔(1 <= N <= 50000, 1 <= X <= 10^9, 1 <= M <= 10^9)。 第2 - N + 1行:每行1个数Pi,对应木桩的位置(0 <= Pi <= Pi+1 <= M),并且给出的数据是有序的。
输出最长绳子的最小值。如果码头排不下所有船则输出-1。
3 2 16 1 3 14
3
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=50000+100;
int a[maxn];
int b[maxn];//记录第i个及i个之后的木桩到船心的最小距离
int bbs(int x)
{
if(x<0)
return -x;
return x;
}
int main()
{
int n,x;
int m;
scanf("%d%d%d",&n,&x,&m);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
if(m/2/x<n)
{
printf("-1\n");
}
else
{
int ans=0,cur=0;
for(int i=n-1;i>=0;i--)
{
b[i]=a[i]-(i*2*x+x);
if(i<n-1)
b[i]=min(b[i],b[i+1]);
}
for(int i=0;i<n;i++)
{
int temp=(a[i]-(i*2*x+x)-cur+b[i]-cur)/2;
if(temp>0)
{
cur+=min(temp,m-n*x*2-cur);
}
ans=max(ans,bbs(a[i]-(i*2*x+x)-cur));
}
cout<<ans<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/caduca/article/details/48137741