Luogu4697 ceoi2011 balloons monotonous Stack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.