Babaei and Birthday Cake 线段树维护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 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

Copy

2
100 30
40 10

Output

Copy

942477.796077000

Input

Copy

4
1 1
9 7
1 4
10 7

Output

Copy

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个圆柱体的底面圆半径,和高,当且仅当后面的圆的体积大于前面的圆的体积,就可以把他们合并成一堆,求这n个圆柱体可以组成的最大圆柱体的体积。

思路:首先,我们算出所有圆柱体的体积,设dp [ i ]  =  dp [ j ]  +  a[ i ],其中j < i && a[ j ] < a[ i ],并且dp[ j ]的值应该尽可能的大。那么建一棵最大值线段树维护dp,怎么保证 j < i 呢,我一个一个添加到线段树中,那么当添加 i的dp值 之前,线段树中的元素都是位于 i 前面的,那怎么保证a[ j ] < a[ i ]呢,还记得权值线段树吗,我离散化圆柱体的体积,把它作为dp值在线段树中的位置,这样就保证了a[ j ] < a[ i ]了。

以上讲得非常有道理  

叶子节点tree(node)   [L,R] (L=R)  维护的是:以离散化数组vv[L] 为结尾的 上升子序列最大和。


————————————————
版权声明:本文为CSDN博主「OerUUU」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44499508/article/details/104141478

#include<bits/stdc++.h>
#define ll long long 
#define pi acos(-1)
#define mid (l+r)/2
#define ls o*2
#define rs o*2+1
using namespace std;
int n;
ll v[100005],vv[100005];
ll tree[4*100005];
void up(int o,int l,int r,int p,ll v)
{
	if(l==r)
	{
		tree[o]=max(tree[o],v);
		return ;
	}
	if(p<=mid) up(ls,l,mid,p,v);
	else up(rs,mid+1,r,p,v);
	tree[o]=max(tree[ls],tree[rs]);
}

ll qu(int o,int l,int r,int ql,int qr) 
{
	if(ql<=l&&r<=qr)
	{
		return tree[o];
	}
	ll ans=0;
	if(ql<=mid) ans=max(ans,qu(ls,l,mid,ql,qr));
	if(mid+1<=qr) ans=max(ans,qu(rs,mid+1,r,ql,qr));
	return ans;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		ll a,b;
		cin>>a>>b;
		vv[i]=v[i]=a*a*b;
	}
	sort(vv+1,vv+1+n);
	int N=unique(vv+1,vv+1+n)-vv-1;
	for(int i=1;i<=n;i++)
	{
		int j=lower_bound(vv+1,vv+1+N,v[i])-vv;
		ll V=v[i];
		if(j-1>=1) V+=qu(1,1,N,1,j-1);
		up(1,1,N,j,V);
	}
	printf("%.9f\n",pi*tree[1]);	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值