Portal
$ N $ balloons are given, and $ X_ I $ and $ r_ I $ are given from left to right. They are now inflated from left to right. Each balloon is always tangent to the ground at the $ X_ I $ point during the inflation process, and the maximum radius is $ r_ I $. If the balloon is tangent to a previous balloon during inflation, stop inflation. Ask the radius of each balloon. $ N \ Leq 2 \ times 10 ^ 5, X_ I, r_ I \ Leq 10 ^ 9 $, to ensure that $ X_ I $ increases monotonically.
First, we can calculate that if a balloon $ I $ is tangent to the previous balloon $ J $, the radius of the balloon $ I $ is $ \ frac {(x_ I-x_j) ^ 2} {4r_j} $
Then we can find a method to reduce the complexity: if the current half diameter of a balloon is larger than the radius of some previous balloons, these balloons will not contribute, in a certain inflation process, if the current maximum inflation value is smaller than the radius of a ball, then the current balloon cannot contribute. Therefore, we can maintain a monotonous stack with an increment of $ x $ and a decrease of $ r$ for decision making. This reduces the complexity to $ O (n) $.
1 #include<bits/stdc++.h> 2 #define ld long double 3 using namespace std; 4 5 const int MAXN = 200010; 6 int Stack[MAXN] , x[MAXN] , R[MAXN]; 7 ld r[MAXN]; 8 9 int main(){10 int N , hd = 0;11 cin >> N;12 for(int i = 1 ; i <= N ; i++){13 cin >> x[i] >> R[i];14 ld minN = R[i];15 while(hd){16 minN = min(minN , (x[i] - x[Stack[hd]]) / r[Stack[hd]] * (x[i] - x[Stack[hd]]) / 4);17 if(minN > r[Stack[hd]])18 hd--;19 else20 break;21 }22 cout << fixed << setprecision(5) << (r[i] = minN) << endl;23 Stack[++hd] = i;24 }25 return 0;26 }
Luogu4697 ceoi2011 balloons monotonous Stack