题目描述
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
to
.
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
located on the roof of the building
(of height
) protects the building
(of height
) if the following inequality holds:
where
denotes the absolute value of the difference between
and
.
Byteasar, the mayor of Byteburg, asks your help.
Write a program that, for every building
, determines the minimum height of a lightning conductor that would protect all the buildings if it were put on top of the building
.
输入格式
In the first line of the standard input there is a single integer
(
) that denotes the number of buildings in Byteburg.
Each of the following
lines holds a single integer
(
) that denotes the height of the
-th building.
输出格式
Your program should print out exactly
lines to the standard output.
The
-th line should give a non-negative integer
denoting the minimum height of the lightning conductor on the
-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|}aj≤ai+p−∣i−j∣
1 \le n \le 5\times 10^{5}1≤n≤5×105,0 \le a_i \le 10^{9}0≤ai≤109 。
输入输出样例
6 5 3 2 4 2 4
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 }
来源:https://www.cnblogs.com/Y-E-T-I/p/12243143.html