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],对于每一个查 ...
随机推荐
- Python读取word文档(python-docx包)
最近想统计word文档中的一些信息,人工统计的话...三天三夜吧 python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.read ...
- WPF 员工卡条形码
大家都知道条形码(Barcode)是一种可以由机器识别的特殊编码,在生产.生活中也常常会见到并使用它.条形码的类型和种类很多感兴趣的朋友可以详细了解一下.其中Code 39 可以说是一种最为常见并广泛 ...
- PhotoKit详解
Photokit介绍 这篇主要介绍如何通过 Photokit获取数据 photokit.jpg 1,基类 PHObject Photos 框架中的根类PHObject只有一个公开接口 localIde ...
- python基础-第五篇-5.1冒泡排序
几个月过去了,小白逐渐对公司的后端服务熟悉了,不过这天小白又接到一封神秘邮件,是景女神发来的:公司急需一批对语言算法有些了解的优秀员工,鉴于你在公司的表现很不错,现在给到你一个培训机会,请速到开发部报 ...
- antd-mobile的例子--cnode
antd-mobile 简单的例子 预览地址 https://shenggen1987.github.io/antd-mobile-roadhog/#/crm/pages/users githu ...
- CKeditor插件开发流程(二)SyntaxHighlighter
CKEditor整合SyntaxHighlighter实现代码高亮显示 1,版本说明 CKEditor:ckeditor_4.0.1_standard.zipSyntaxHighlighter:syn ...
- python有哪些关键字?让他自己“吐”出来!
通过调用库来输出!for循环控制! 源代码: import keyword c = 0 for i in keyword.kwlist: print(i) c += 1 代码截图: 哈哈,关键字: F ...
- 第三篇 css属性
一.颜色属性 颜色属性有下面四种方式 <div style="color:blueviolet">ppppp</div> <div style=&qu ...
- mini2440移植uboot 2014.04(二)
我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440.git 参考文章: <u-boot-2011 ...
- Node教程
本人的博客写了node的教程,从零开始,一步一步的通过例子讲解,希望喜欢的同学给我的github上加颗星,谢谢! github地址:https://github.com/manlili/node_le ...