CF 629 D. Babaei and Birthday Cake 线段树+DP

来源:互联网 发布:淘宝主播招聘 编辑:程序博客网 时间:2024/05/29 12:14

虽然没有开dp数组,但满满的dp思想。


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 jonly 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
2100 3040 10
output
942477.796077000
input
41 19 71 410 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 12 and 4.



#include<bits/stdc++.h>#define all(x) (x).begin(), (x).end()#define rep(i,n)  for(int i=0 ;i<(n) ;i++)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define eps   1e-10using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn=  100000  ;const double PI=4.0*atan(1.0);int n;struct SegmentTree{    double sum[4*maxn];    /*    线段树不能这样初始化,当初很天真的这样写了    原因是不同的n值的情况下,数组1或者2对应的区间是不同的,    如果只是这样,会有上次残留的记录。    void clear()    {        for(int i=0;i<=4*n;i++)        {            sum[i]=0;        }    }    */    void build(int ind,int le,int ri)    {        if(le==ri)        {            sum[ind]=0;            return;        }        MID;        build(lson);        build(rson);        sum[ind]=0;    }    void update(int ind,int le,int ri,int pos,double val)    {        if(le==ri)        {            sum[ind]=val;            return;        }        MID;        if(pos<=mid) update(lson,pos,val);        else update(rson,pos,val);        sum[ind]=max(sum[ind<<1],sum[ind<<1|1]);    }    double query(int ind,int le,int ri,int x,int y)    {        if(x<=le&&ri<=y)//我居然写出这么错误的代码 : if(le==ri)        {            return sum[ind];        }        MID;        double ans=0;        if(x<=mid) ans=max(ans,query(lson,x,y) );        if(y>mid)  ans=max(ans,query(rson,x,y) );        return ans;    }}sgt;struct Node{    int ind;    double vol;    bool operator<(const Node& y)const//题目要求,放在上面的圆柱体体积要严格大于底下的    {        if(fabs(vol-y.vol)>eps )  return vol<y.vol;        return ind>y.ind;//这个就是配合底下的处理,防止出现放在上面的圆柱体体积等于底下的    }} a[maxn+10];void work(){    sgt.build(1,1,n);    double ans=0;    for(int i=1;i<=n;i++)    {        int ind=a[i].ind;        double ret=a[i].vol+sgt.query(1,1,n,1,ind);        sgt.update(1,1,n,ind,ret);        ans=max(ans,ret);    }      printf("%.9f\n",ans);}int main(){    while(~scanf("%d",&n))    {        double r,h;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf",&r,&h);            a[i].vol= r*r*PI*h;            a[i].ind=i;        }        sort(a+1,a+1+n);//更好的排序方法是利用pair,将ind赋值为-ind,再直接sort        work();    }   return 0;}


0 0
原创粉丝点击