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],对于每一个查 ...
随机推荐
- mysql ODBC连接配置
1.软件包安装 [root@server1 mysql]# rpm -ivh MySQL-server-5.1.72-1.glibc23.i386.rpm [root@server1 mysql]# ...
- linux的文件夹权限
r:读取文件夹结构清单的权限,可以列出该文件夹下的所有文件. w:更改目录结构清单的能力,可以新建文件和目录,删除文件和目录(不管这个文件是否属于你),对文件和目录更名,移动文件和目录. x:具有x权 ...
- 【python】-- socketserver
socketserver SocketServer服务端内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求.即:每个客户端请求连接到服务器时,Socket服务端 ...
- centos7 使用postgres
1: http://www.cnblogs.com/think8848/p/5877076.html 2:http://blog.csdn.net/xuaa/article/details/52262 ...
- 计算机网络协议层次(转发:http://blog.csdn.net/gavin_john/article/details/53186570)
计算机网络学习的核心内容就是网络协议的学习.网络协议是为计算机网络中进行数据交换而建立的规则.标准或者说是约定的集合.计算机网络协议同我们的语言一样,多种多样. 为了给网络协议的设计提供一个结构,网络 ...
- python实例1:创建一个登陆模块
实现功能: 1.用户输入账户密码 2.验证账户是否存在于黑名单,如果存在于黑名单,则执行1,否则往下执行 3.验证用户名和密码. 3.1.如果验证成功,则打印欢迎信息并退出程序: 3.2.如果用户名存 ...
- LeetCode:加一【66】
LeetCode:加一[66] 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外 ...
- html5 css3 进度条特效
https://www.html5tricks.com/tag/css3%E8%BF%9B%E5%BA%A6%E6%9D%A1/page/3
- 小学生都能看懂的数位dp
前言 数位dp其实很久前就知道了,也做过几道和其他算法混在一起的题目,其实通过手玩是能做的 但毕竟是种算法,还是系统学下比较好(节省手玩时间) 模板题 P2602 [ZJOI2010]数字计数 化简题 ...
- inline-block元素的4px空白间距解决方案
http://www.jb51.net/css/68785.html inline-block元素的4px空白间距解决方案