D. Babaei and Birthday Cake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.
Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.
However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.
Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.
Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.
Output
Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Examples
input
2 100 30 40 10
output
942477.796077000
input
4 1 1 9 7 1 4 10 7
output
3983.539484752
Note
In first sample, the optimal way is to choose the cake number 1.
In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
Explanation
First of all, we calculate the volume of each cake: vi = π × hi × ri2.
Now consider the sequence v1, v2, v3, ..., vn : The answer to the problem is the maximum sum of elements between all increasing sub-sequences of this sequence. How do we solve this? First to get rid of the decimals we can define a new sequence a1, a2, a3, ..., ansuch that
We consider dpi as the maximum sum between all the sequences which end with ai and
dpi =
The answer to the problem is: π × maxi = 1ndp[i]
Now how do we calculate ? We use a max-segment tree which does these two operations: 1. Change the i't member to v. 2. Find the maximum value in the interval 1 to i.
Now we use this segment tree for the array dp and find the answer.
Consider that a1, a2, a3, ..., an is sorted. We define bi as the position of ai. Now to fill dpi we find the maximum in the interval [1, bi) in segment and we call it x and we set the bi th index of the segment as ai + x. The answer to the problem would the maximum in the segment in the interval [1,n]
Time complexity: O(nlgn)
Thanks to AlirezaT who helped a lot for writing the editorial of problem D.
Explanation
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long int
int male[400];
int female[400];
ll tree[600010];
bool cmp(pair<long long, int> a, pair<long long, int> b)
{
if (a.first != b.first)
return a.first < b.first;
return a.second>b.second;
}
ll query(int node,int s,int end ,int qs,int qh)
{
if(qs>qh)
return 0;
if(s>end || qs>end || qh<s)
return 0;
if(s>=qs && end<=qh)
return tree[node];
int mid=(s+end)/2;
ll q1=query(2*node,s,mid,qs,qh);
ll q2=query(2*node+1,mid+1,end,qs,qh);
return max(q1,q2);
}
void update(int node,int s,int end,int upl,int uph,ll val)
{
//cout<<"upl "<<" uph "<<upl<<" "<<uph<<endl;
if(s>end || upl>end || uph<s)
return ;
if(s>=upl && end<=uph)
{
tree[node]=val;
return ;
}
int mid=(s+end)/2;
update(2*node,s,mid,upl,uph,val);
update(2*node+1,mid+1,end,upl,uph,val);
tree[node]=max(tree[2*node],tree[2*node+1]);
}
int main()
{
int n;
vector<pair<ll,int> > v;
cin>>n;
for(int i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
v.pb(mp(a*a*b,i));
}
sort(v.begin(),v.end(),cmp);
// cout<<"her e"<<endl;
for(int i=0;i<n;i++)
{
int idx=v[i].ss;
// cout<<"idc "<<idx<<endl;
ll ans=query(1,0,n-1,0,idx-1);
// cout<<"ans "<<ans<<endl;
update(1,0,n-1,idx,idx,ans+v[i].ff);
// for(int i=1;i<=11;i++)
// cout<<"i "<<i<<tree[i]<<endl;
}
ll ans=query(1,0,n-1,0,n-1);
// cout<<ans<<endl;
double res=3.14159265*1.00*(double)ans;
printf("%.13lf",res);
return 0;
}
No comments:
Post a Comment