题目描述

Progressive climate change has forced the Byteburg authorities to build a huge lightning conductor that would protect all the buildings within the city.

These buildings form a row along a single street, and are numbered from [POI2011]Lightning Conductor_动态规划 to [POI2011]Lightning Conductor_二分_02.

The heights of the buildings and the lightning conductor are non-negative integers.

Byteburg's limited funds allow construction of only a single lightning conductor.

Moreover, as you would expect, the higher it will be, the more expensive.

The lightning conductor of height [POI2011]Lightning Conductor_分治_03 located on the roof of the building [POI2011]Lightning Conductor_分治_04 (of height [POI2011]Lightning Conductor_四边形不等式_05) protects the building [POI2011]Lightning Conductor_分治_06 (of height [POI2011]Lightning Conductor_四边形不等式_07) if the following inequality holds:

[POI2011]Lightning Conductor_动态规划_08 where [POI2011]Lightning Conductor_四边形不等式_09 denotes the absolute value of the difference between [POI2011]Lightning Conductor_分治_10 and [POI2011]Lightning Conductor_二分_11.

Byteasar, the mayor of Byteburg, asks your help.

Write a program that, for every building [POI2011]Lightning Conductor_分治_12, determines the minimum height of a lightning conductor that would protect all the buildings if it were put on top of the building [POI2011]Lightning Conductor_四边形不等式_13.

输入格式

In the first line of the standard input there is a single integer [POI2011]Lightning Conductor_二分_14 ([POI2011]Lightning Conductor_分治_15) that denotes the number of buildings in Byteburg.

Each of the following [POI2011]Lightning Conductor_单调队列_16 lines holds a single integer [POI2011]Lightning Conductor_二分_17 ([POI2011]Lightning Conductor_分治_18) that denotes the height of the [POI2011]Lightning Conductor_二分_19-th building.

输出格式

Your program should print out exactly [POI2011]Lightning Conductor_单调队列_20 lines to the standard output.

The [POI2011]Lightning Conductor_二分_21-th line should give a non-negative integer [POI2011]Lightning Conductor_四边形不等式_22 denoting the minimum height of the lightning conductor on the [POI2011]Lightning Conductor_单调队列_23-th building.

题意翻译

给定一个长度为 nn 的序列 \{a_n\}{an},对于每个 i\in [1,n]i[1,n] ,求出一个最小的非负整数 pp ,使得 \forall j\in[1,n]j[1,n],都有 a_j\le a_i+p-\sqrt{|i-j|}ajai+pij

1 \le n \le 5\times 10^{5}1n5×105,0 \le a_i \le 10^{9}0ai109 。

输入输出样例

输入 #1复制
6
5
3
2
4
2
4
输出 #1复制
2
3
5
3
5
4
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 typedef long long ll;
 6 int q[500005],R[500005],a[500005],n,b[500005];
 7 double sq[500005],f1[500005],f2[500005];
 8 double getValue(int x,int y)
 9 {
10     return a[y]+(sq[x-y]);
11 }
12 int binary(int x,int y)
13 {
14     int l=x,r=n+1;
15     while (l<r)
16     {
17         int mid=(l+r)/2;
18         if (getValue(mid,y)<=getValue(mid,x)) r=mid;
19         else l=mid+1;
20     }
21     return l;
22 }
23 int main()
24 {int i,j;
25     scanf("%d",&n);
26     for (i=1;i<=n;i++)
27     {
28         scanf("%d",&a[i]);
29         sq[i]=sqrt((double)i);
30     }
31     int h=1,t=0;
32     for (i=1;i<=n;i++)
33     {
34         while (h<t&&R[t-1]>=binary(i,q[t])) t--;
35         R[t]=binary(i,q[t]);
36         q[++t]=i;
37         while (h<t&&R[h]<=i) h++;
38         f1[i]=getValue(i,q[h]);
39     }
40     h=1,t=0;
41     for (i=1;i<=n;i++)
42     b[i]=a[n-i+1];
43     for (i=1;i<=n;i++)
44     a[i]=b[i];
45     for (i=1;i<=n;i++)
46     {    
47         while (h<t&&R[t-1]>=binary(i,q[t])) t--;
48         R[t]=binary(i,q[t]);
49         q[++t]=i;
50         while (h<t&&R[h]<=i) h++;
51         f2[n-i+1]=getValue(i,q[h]);
52     }
53     for (i=1;i<=n;i++)
54     printf("%.0lf %.0lf\n",f1[i],f2[i]);
55     for (i=1;i<=n;i++)
56     printf("%d\n",(int)ceil(max(f1[i],f2[i]))-a[n-i+1]);
57 }
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int a[500005],n,b[500005];
 8 double sq[500005],f1[500005],f2[500005];
 9 void solve1(int l,int r,int L,int R)
10 {int i,Mid=0;
11     if (l>r) return;
12     int mid=(l+r)/2;
13     double s=0;
14     f1[mid]=a[mid];Mid=mid;
15     for (i=L;i<=min(R,mid);i++)
16     {
17         s=a[i]+sq[mid-i];
18         if (s>f1[mid]) f1[mid]=s,Mid=i; 
19     }
20     solve1(l,mid-1,L,Mid);
21     solve1(mid+1,r,Mid,R);
22 }
23 void solve2(int l,int r,int L,int R)
24 {int i,Mid=0;
25     if (l>r) return;
26     int mid=(l+r)/2;
27     double s=0;
28     f2[n-mid+1]=a[mid];Mid=mid;
29     for (i=L;i<=min(R,mid);i++)
30     {
31         s=a[i]+sq[mid-i];
32         if (s>f2[n-mid+1]) f2[n-mid+1]=s,Mid=i; 
33     }
34     solve2(l,mid-1,L,Mid);
35     solve2(mid+1,r,Mid,R);
36 }
37 int main()
38 {int i,j;
39     scanf("%d",&n); 
40     for (i=1;i<=n;i++)
41     {
42         scanf("%d",&a[i]);
43         sq[i]=sqrt((double)i);
44     } 
45     solve1(1,n,1,n);
46     for (i=1;i<=n;i++)
47     b[i]=a[n-i+1];
48     for (i=1;i<=n;i++)
49     a[i]=b[i];
50     solve2(1,n,1,n);
51     for (i=1;i<=n;i++)
52     printf("%d\n",(int)ceil(max(f1[i],f2[i]))-a[n-i+1]);
53 }