codeforces 629D D. Babaei and Birthday Cake (线段树+dp)
2 seconds
256 megabytes
standard input
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.
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.
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
.
2
100 30
40 10
942477.796077000
4
1 1
9 7
1 4
10 7
3983.539484752
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)的更多相关文章
- Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP
题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...
- 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 ...
- 【20.19%】【codeforces 629D】Babaei and Birthday Cake
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
随机推荐
- CLR内存回收机制
代龄机制. 通过递归构建可达对象图,不可达的对象会被回收,然后CLR会矫正对象指针. 对于终止化/Finalize对象, 一开始时这些对象指针/根/引用会被放到终止化链表中,当CLR垃圾收集开始时,那 ...
- vue面试题,知识点汇总(有答案)
一. Vue核心小知识点 1.vue中 key 值的作用 key 的特殊属性主要用在 Vue的虚拟DOM算法,在新旧nodes对比时辨识VNodes.如果不使用key,Vue会使用一种最大限度减少动态 ...
- hdu1573(线性同余方程组)
套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 实现asp.net mvc页面二级缓存,提高访问性能
实现的mvc二级缓存的类 //Asp.Net MVC视图页面二级缓存 public class TwoLevelViewCache : IViewLocationCache { private rea ...
- Count(二维树状数组)
[bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12 HINT 题解:对于每一个颜色建一 ...
- You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
[root@localhost dreamstart]# composer installLoading composer repositories with package informationI ...
- NSAttributedStringKey
NSFontAttributeName; //字体,value是UIFont对象 NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),valu ...
- 《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris
题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3; /** * @Description:遍历二叉树的神级方法 morris * @ ...
- python3 内置常用函数系列一
python3 内置了一系列的常用函数, python英文官方文档详细说明:点击查看, 为了方便查看,将内置常用的函数的记录一下来. Python3版本所有的内置函数: 1.abs() print(a ...
- CSS3自定义Checkbox特效
在线演示 本地下载