D. Babaei and Birthday Cake
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.
------------------------------------------------------editorial-----------------------------------------------------------------------------
this is a simple question we need to find maximum sum in an array and all included elements must be strictly increasing,
means we have to find maximum sum in increasing sub sequence ..
first come with a brute dp approach , dp[i]=max(dp[i], dp[j]+val[i]) for all j<i and val[j]< val[i] ,
this can be easily computed in o(n^2) , but we have to it in the o(nlogn) complexity ,
for that we have to use a segment tree approach ,
sort all the values in pair ((r*r*h) , index) means minimum volume first , before now start computing from the first, before computing ans for a volume r*r*h we need to computed value of all volume < than its volume and is in the left of it , since we have sort according to the r*r*h than it is sure that all volume < its volume computed ,
now we have to find best answer from all volumes < its volume and is in the right of it , this can be done using range max query easily, but main problem is that some of the vol < its vol can be in the right of this index also , how to not consider that , for that at the time of updating value of any vol update it at its original index not at the sorted index position , and also for query find according to the original index not at the sorted index ,,, its confusing see the code for clarity ,,.. or read next editorial..
--------------------------------------------EDITORIAL-----------------------------------------------------------------------------
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)
-------------------------------------------------------code---------------------------------------------------
#include<bits/stdc++.h>
typedef long long int lli;
using namespace std;
lli dp[1000000];
#include<iostream>
using namespace std;
#define inf -9999999999999999
bool comp(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;
}
lli query(lli node,lli start,lli end,lli r1,lli r2)
{
if(start>end || r1>end || r2<start) return 0;
if(r1<=start && r2>=end)
{
return dp[node];
}
else
{
lli q1=query(2*node,start,(start+end)/2,r1,r2);
lli q2=query(2*node+1,((start+end)/2)+1,end,r1,r2);
return max(q1,q2);
}
}
void update(lli node ,lli start,lli end,lli r1,lli r2,lli val)
{
if(r1>end || r2<start || start>end) return ;
if(r1<=start && r2>=end)
{
dp[node]+=val;
return ;
}
update(2*node, start,(start+end)/2,r1,r2,val);
update(2*node+1, ((start+end)/2)+1,end,r1,r2,val);
dp[node]=max(dp[2*node],dp[2*node+1]);
}
int main()
{
lli n;
scanf("%lld",&n);
vector<pair<lli,lli> > v;
for(lli i=0;i<n;i++)
{
lli h,r;
scanf("%lld %lld",&r,&h);
v.push_back({r*r*h,i});
}
sort(v.begin(),v.end(),comp);
lli ans=0;
for(lli i=0;i<n;i++)
{
lli vol=v[i].first;
lli index=v[i].second;
lli maxi=query(1,0,n-1,0,index-1);
// cout<<" maxi is "<<maxi<<endl;
update(1,0,n-1,index,index,maxi+vol);
}
ans=query(1,0,n-1,0,n-1);
double dd=3.1415926535897932384626433*(double)ans;
printf("%.12lf",dd);
}
-----------------------------------------ANOTHER SIMILAR PROB--------------------------------
-----------------------------------------ANOTHER SIMILAR PROB--------------------------------
Susobhan in BorrowLand |
All submissions for this problem are available.
Mr Susobhan lives in the city of Borrow-Land. He is looking to borrow as much money he can before he catches a flight to London. The city of Borrow-Land can be considered as an infinite grid. There are n lenders in the city located at a point (xi , yi ) who can lend at most Wi money. But Mr Susobhan borrows all the money the lender has.
No two lenders share the same location and no lender is present at point (0,0). Initially Mr Susobhan has no money and he starts at point (0, 0) in his expedition of borrowing money. Mr Susobhan is lazy and he can move only in two directions.
- up from point (x,y) to (x,y+1)
- right from point (x,y) to (x + 1, y)
Input
The first line of input contains an integer n denoting the number of lenders.
The next n lines contain three space separated numbers xi, yi, and wi denoting the xth coordinate , yth coordinate and the money of the lender respectively.
The next n lines contain three space separated numbers xi, yi, and wi denoting the xth coordinate , yth coordinate and the money of the lender respectively.
Output
Print the maximum amount in a new line.
Constraints
- 1 ≤ n ≤ 100000
- 0 ≤ xi,yi ≤ 1000000000
- 1 ≤ wi ≤ 1000000000
Example
Input: 3 1 2 20 2 6 10 1 8 5 Output: 30
---------------------------------------------------------CODE-----------------------------------------------------------------------------------------
#include<bits/stdc++.h> typedef long long int lli; using namespace std; lli dp[1000000]; #include<iostream> #define ff first #define ss second using namespace std; #define inf -9999999999999999 #define mp make_pair #define pb push_back lli query(lli node,lli start,lli end,lli r1,lli r2) { if(r1>r2) return 0; if(start>end || r1>end || r2<start) return 0; if(r1<=start && r2>=end) { return dp[node]; } else { lli q1=query(2*node,start,(start+end)/2,r1,r2); lli q2=query(2*node+1,((start+end)/2)+1,end,r1,r2); return max(q1,q2); } } void update(lli node ,lli start,lli end,lli r1,lli r2,lli val) { if(r1>end || r2<start || start>end) return ; if(r1<=start && r2>=end) { dp[node]=val; return ; } update(2*node, start,(start+end)/2,r1,r2,val); update(2*node+1, ((start+end)/2)+1,end,r1,r2,val); dp[node]=max(dp[2*node],dp[2*node+1]); } int main() { lli n; vector<pair<pair<lli,lli>,lli> > qq,vv; vector<pair<lli,lli> > v; vector<lli> comp; for(lli i=0;i<n;i++) { lli h,r,val; qq.push_back(mp(mp(r,h),val)); comp.pb(r); comp.pb(h); } sort(comp.begin(),comp.end()); vector<lli>::iterator it; for(int i=0;i<n;i++) { int val1=qq[i].ff.ff; int val2=qq[i].ff.ss; it=lower_bound(comp.begin(),comp.end(),val1); int pos1=it-comp.begin(); it=lower_bound(comp.begin(),comp.end(),val2); int pos2=it-comp.begin(); vv.pb(mp(mp(pos1,pos2),qq[i].ss)); } sort(vv.begin(),vv.end()); // for(int i=0;i<n;i++) // { // cout<<vv[i].ff.ff<<" "<<vv[i].ff.ss<<" "<<vv[i].ss<<endl; //} for(int i=0;i<n;i++) { v.pb(mp(vv[i].ff.ss,vv[i].ss)); } lli ans=0; for(lli i=0;i<n;i++) { lli vol=v[i].ss; lli index=v[i].ff; // cout<<" idx "<<index<<" "<<vol<<endl; lli maxi=query(1,0,2*n,0,index); // cout<<" maxi is "<<maxi<<endl; update(1,0,2*n,index,index,maxi+vol); } ans=query(1,0,2*n,0,2*n); cout<<ans<<endl; return 0; }
No comments:
Post a Comment