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.

思路:一般的dp写法会tle,需要线段树进行维护,由于条件a[i]<a[j]&&i<j,故要sort先一下,可以保证a[i]<a[j],而这些值插入线段树是按照i<j的顺序插入的,所以答案才正确,一开始总想不通这点,后来在纸上找了一个数组演绎了一下就明白了;

AC代码:

#include <bits/stdc++.h>
#define ll long long
const double PI=acos(-1.0);
using namespace std;
const int N=1e5+;
double r[N],h[N],v[N],a[N],dp[N];
struct nod
{
  int l,r;
  double sum;
};
nod tree[*N];
int build(int node,int le,int ri)
{
  tree[node].l=le;
  tree[node].r=ri;
  if(le==ri)
  {
    tree[node].sum=;
    return ;
  }
  int mid=(le+ri)/;
  build(*node,le,mid);
  build(*node+,mid+,ri);
  tree[node].sum=max(tree[*node].sum,tree[*node+].sum);
}
double query(int node,int le,int ri)
{
  if(tree[node].l>=le&&tree[node].r<=ri)return tree[node].sum;
  else
  {
    int mid=(tree[node].l+tree[node].r)/;
    if(ri<=mid)return query(*node,le,ri);
    else if(le>mid)return query(*node+,le,ri);
    else return max(query(*node,le,ri),query(*node+,le,ri));
  }
}
int update(int node,int posi,double x)
  {
    if(tree[node].l==posi&&tree[node].r==posi)
    {
         tree[node].sum=max(tree[node].sum,x);
        return ;
    }
  int mid=(tree[node].l+tree[node].r)/;
  if(posi<=mid)update(*node,posi,x);
  else update(*node+,posi,x);
  tree[node].sum=max(tree[*node].sum,tree[*node+].sum);
}
int main()
{
  int n;
  scanf("%d",&n);
  for(int i=;i<=n;i++)
  {
    scanf("%lf%lf",&r[i],&h[i]);
    a[i]=v[i]=r[i]*r[i]*h[i];
  }
  sort(a+,a+n+);
  build(,,n);
  for(int i=;i<=n;i++)
  {
    int pos=lower_bound(a+,a+n+,v[i])-a;
    if(pos==)dp[i]=v[i];
    else
    {
      dp[i]=query(,,pos-)+v[i];
    }
    update(,pos,dp[i]);
}
printf("%.12lf",query(,,n)*PI);
return ;
}
 

codeforces 629D D. Babaei and Birthday Cake (线段树+dp)的更多相关文章

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

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

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

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  3. 【20.19%】【codeforces 629D】Babaei and Birthday Cake

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  5. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  6. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  9. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

随机推荐

  1. shell 遍历所有文件包括子目录

    1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error :  syntax er ...

  2. spring;maven;github;ssm;分层;timestamp;mvn;

    [说明]本来还想今天可以基本搭建一个合适的ssm环境呢,结果发现,,太特么复杂了,网上的例子有好多,看了好多,下面的评论或多或少都有说自己运行产生问题的,搞的我也不敢好好下载运行 [说明]没办法,将目 ...

  3. 【BZOJ4375】Selling Tickets 随机化

    [BZOJ4375]Selling Tickets Description 厨师在一次晚宴上准备了n道丰盛的菜肴,来自世界各地的m位顾客想要购买宴会的门票.每一位顾客都有两道特别喜爱的菜,而只要吃到了 ...

  4. 【CodeM初赛B轮】A 贪心

    [CodeM初赛B轮]A 题目大意:给你一棵树,起初所有点都是白色的,你每次都能选择一个白点i,将这个点i到根路径上的所有到i的距离<k[i]的点都染成黑色(根和i也算,已经被染成黑色的点还是黑 ...

  5. 【BZOJ3672】[Noi2014]购票 树分治+斜率优化

    [BZOJ3672][Noi2014]购票 Description  今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会.       ...

  6. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

  7. vue-cli (vue脚手架)

    vue-cli(脚手架):它可以自动生成目录 1.在网速不佳的情况下可以安装cnpm(淘宝镜像)如果网速快可以不用安装cnpm直接进行下一步操作 第一步:在命令行执行(全局安装cnpm) npm in ...

  8. AOP原理及其实现

       AOP 是 Aspect-Oriented programming 的缩写,中文翻译为面向切面编程,它是OOP(Object-Oriented Programing,面向对象编程)的补充和完善. ...

  9. plsql 详细安装及汉化步骤

    方法/步骤   双击运行plsqldev715 安装完成后我们装中文补丁: 双击运行‘Chinese’应用程序 找到PLSQL的安装目录添加进来 中文补丁安装完成后我们需要进行orcl的配置,配置好才 ...

  10. 什么是GIL锁以及作用

    全局解释锁,每次只能一个线程获得cpu的使用权:为了线程安全,也就是为了解决多线程之间的数据完整性和状态同步而加的锁,因为我们知道线程之间的数据是共享的.