Codeforces 629D Babaei and Birthday Cake【dp+线段树+离散化】

46 篇文章 0 订阅

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.


题目大意:

给你N个蛋糕,让你堆成一个大蛋糕,两个蛋糕可以堆叠在一起的条件是:i<j&&Vi<Vj,Vi表示i号蛋糕的体积。

问你堆成的最大体积蛋糕的体积是多大。


思路:


1、求极值,那么考虑dp,设定dp【i】表示到第i个蛋糕堆叠成的最大蛋糕的体积。

那么有:dp【i】=max(dp【j】+V【i】,dp【i】),同时有:【1<=j<i&&Vj<Vi】;

但是因为N比较大,直接Dp时间复杂度是O(n^2)的,显然超时,那么考虑套一个线段树优化时间复杂度。


2、那么设定dp【i】仍然表示到第i个蛋糕堆叠成的最大蛋糕的体积。

那么有:dp【i】=max(Max(比i号蛋糕小的所有dp值)+V【i】,dp【i】);

那么假设s【i】表示i号蛋糕是所有蛋糕中第i大的蛋糕,那么可以转化为:dp【i】=max(QueryMax(1,s[i]-1,1,n,1)+V【i】,dp【i】);

过程维护一下即可。

预处理离散化,那么ans=max(ans,dp【i】);


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
const double PI=acos(-1);
double val[108000];
double tmp[108000];
double tree[1000050*8];
double dp[1000050];
void pushup(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
double Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        int m=(l+r)>>1;
        double ans=0;
        if(L<=m)
        {
            ans=max(Query(L,R,lson),ans);
        }
        if(m<R)
        {
            ans=max(Query(L,R,rson),ans);
        }
        return ans;
    }
}
void build( int l ,int r , int rt )
{
    if( l == r )
    {
        tree[rt]=0;
        return ;
    }
    else
    {
        int m = (l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
    }
}
void update(int p,double c,int l,int r,int rt)//p阵营c数据.
{
    if(l==r)
    {
        tree[rt]=max(tree[rt],c);
    }
    else
    {
        int m=(l+r)>>1;
        if(p<=m) update(p,c,lson);
        else  update(p,c,rson);
        pushup(rt);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        map<double ,int >s;
        double output=0;
        for(int i=1;i<=n;i++)
        {
            double r,h;
            scanf("%lf%lf",&r,&h);
            val[i]=r*r*h*PI;
            tmp[i]=val[i];
            output+=val[i];
        }
        build(1,n,1);
        sort(tmp+1,tmp+1+n);
        memset(dp,0,sizeof(dp));
        double ans=0;
        for(int i=1;i<=n;i++)s[tmp[i]]=i;
        for(int i=1;i<=n;i++)
        {
            if(s[val[i]]-1>=1)dp[i]=max(dp[i],Query(1,s[val[i]]-1,1,n,1)+val[i]);
            else dp[i]=max(dp[i],val[i]);
            update(s[val[i]],dp[i],1,n,1);
            ans=max(ans,dp[i]);
        }
        printf("%.9lf\n",ans);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值