2216: [Poi2011]Lightning Conductor
Time Limit: 25 Sec Memory Limit: 64 MBSubmit: 1516 Solved: 554
[Submit][Status][Discuss]
Description
已知一个长度为n的序列a1,a2,...,an。
对于每个1<=i<=n,找到最小的非负整数p满足 对于任意的j, aj < = ai + p - sqrt(abs(i-j))
Input
第一行n,(1<=n<=500000)
下面每行一个整数,其中第i行是ai。(0<=ai<=1000000000)
Output
n行,第i行表示对于i,得到的p
Sample Input
6
5
3
2
4
2
4
5
3
2
4
2
4
Sample Output
2
3
5
3
5
4
3
5
3
5
4
决策单调性入门题
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef double db;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=500500;
struct node{int l,r,d;}q[N];
int n,a[N];
inline db get_f(int i,int j)
{if(!j || j>n) return 0;return sqrt(abs(i-j))+a[j];}
int find(int tail,int i)
{
int l(q[tail-1].l),r(q[tail-1].r),mid;
while(l<=r)
{
mid=(l+r)>>1;
get_f(mid,i)>=get_f(mid,q[tail-1].d) ? r=mid-1 : l=mid+1;
}
return r+1;
}
void solve(int *f)
{
register int i,head(0),tail(1),tmp;
q[head]=(node){0,n,0};
for(i=1;i<=n;++i)
{
q[head].l++;
while(head<tail && q[head].r<i) head++;
f[i]=q[head].d;
if(head>=tail || get_f(n,i)>get_f(n,q[tail-1].d))
{
while(head<tail && get_f(q[tail-1].l,i)>=get_f(q[tail-1].l,q[tail-1].d))
tail--;
if(head>=tail) q[tail++]=(node){i,n,i};
else
{
tmp=find(tail,i);
q[tail-1].r=tmp-1;
q[tail++]=(node){tmp,n,i};
}
}
}
}
int f[N],g[N];
int main()
{
n=read();
register int i;
for(i=1;i<=n;++i) a[i]=read();
solve(f);
reverse(a+1,a+1+n);
solve(g);
for(i=n;i;--i)
printf("%d\n",max(0,int(ceil(max(get_f(i,n-f[n-i+1]+1),get_f(i,g[i]))))-a[i]));
return 0;
}