Codeforces Round #343 (Div. 2)D - Babaei and Birthday Cake 线段树

本文探讨了一种独特的蛋糕制作策略,旨在通过合理堆叠不同大小的蛋糕来最大化生日派对蛋糕的总体积。通过离散化、线段树与动态规划的应用,实现蛋糕体积的最大化。实例分析了如何选择合适的蛋糕进行堆叠,从而达到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

对于当前的第i个,肯定是找到容量比他小的中能得到的总容量最大的那个放上去,所以离散化一下,线段树保存容量排序后第i个能得到的最佳答案,dp[i]表示以第i个结尾能得到的最佳答案。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
int N,nx;
LL X[maxn];
struct Node{
    int r,h;
}cyl[maxn];
LL dp[maxn];
LL ans;
struct IntervalTree{
    LL maxv[maxn<<2];
    void build(int o,int l,int r){
        maxv[o]=0;
        if(l==r)return ;
        int mid=(l+r)>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
    }
    void pushup(int o){
        maxv[o]=max(maxv[o<<1|1],maxv[o<<1]);
    }
    void query(int o,int l,int r,int q1,int q2){
        if(l>=q1&&q2>=r){
            ans=max(ans,maxv[o]);
            return ;
        }
        int mid=(l+r)>>1;
        if(q1<=mid)query(o<<1,l,mid,q1,q2);
        if(q2>mid) query(o<<1|1,mid+1,r,q1,q2);
    }
    void update(int o,int l,int r,int pos,LL val){
        if(l==r){
            maxv[o]=val;
            return;
        }
        int mid=(l+r)>>1;
        if(pos<=mid)update(o<<1,l,mid,pos,val);
        else update(o<<1|1,mid+1,r,pos,val);
        pushup(o);
    }
}tree;

int main(){
    while(scanf("%d",&N)!=EOF){
        for(int i=1;i<=N;i++){
            scanf("%d%d",&cyl[i].r,&cyl[i].h);
            X[i]=1LL*cyl[i].r*cyl[i].r*cyl[i].h;
        }
        sort(X+1,X+1+N);
        nx=unique(X+1,X+1+N)-X-1;
        tree.build(1,1,nx);
        for(int i=1;i<=N;i++){
            LL vol=1LL*cyl[i].r*cyl[i].r*cyl[i].h;
            int pos=lower_bound(X+1,X+nx+1,vol)-X;
            if(pos-1==0){
                dp[i]=vol;
            } else {
                ans=0;
                tree.query(1,1,nx,1,pos-1);
                dp[i]=ans+vol;
            }

            tree.update(1,1,nx,pos,dp[i]);
        }
        ans=0;
        for(int i=1;i<=N;i++){
            ans=max(ans,dp[i]);
        }
        printf("%.9f\n",1.0*PI*ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值