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.
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.
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 .
2
100 30
40 10
942477.796077000
4
1 1
9 7
1 4
10 7
3983.539484752
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.
题意:有n个圆柱形的蛋糕,已知它们的底面半径r和侧面高h。把这n个蛋糕叠起来,第i个蛋糕放在第j个蛋糕上面(1<=j<i)的条件是第i个蛋糕的体积必须大于第j个蛋糕的体积。求叠放起来的蛋糕体积最大能是多少?
题解:看题后感觉是求LIS的最大和,其实不是这样的非最长上升子序列的叠放选择的总体积是可能大于最长上升子序列叠放的和的。 DP解法中记录每一个以i结尾的最大和,对于每个蛋糕si先找到上方顺序第一个大于等于其体积的蛋糕s,然后加入蛋糕s前面一个蛋糕s-1所在符合条件的且和最大的序列,此时sum+=si,并且删去蛋糕s及其后面那些叠放和都不大于sum的连续区间。这样做是保证留下的最底层一个蛋糕所在序列的和一定是最大值。
看到有用线段树维护,可是我并不会/(ㄒoㄒ)/~~,这里用到了map维护,每一个体积S对应当其在最下方时的和。set也可以维护,STL的东西还是不会,准备回炉重造中。
代码如下:
#include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; #define LL __int64 double pi=acos(-1); int main() { int n,i; LL r,h,s; while(scanf("%d",&n)!=EOF) { map<LL,LL> list; list[0]=0; for(i=0;i<n;++i) { scanf("%I64d%I64d",&r,&h); s=r*r*h; map<LL,LL>::iterator left=list.lower_bound(s);//在c++里面是键值,我特么开始弄成了地址指针,我好傻/(ㄒoㄒ)/~~ map<LL,LL>::iterator right=left; LL cnt=(--right)->second+s; right=left; for(;right!= list.end() && right->second<=cnt;++right); list.erase(left,right); list[s]=cnt; } printf("%.7lf\n",(--list.end())->second * pi); } return 0; }