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],对于每一个查 ...
随机推荐
- 程序员之---C语言细节19(来找茬,由/* */ 引起的凝视错误)
主要内容:由/* */ 引起的凝视错误 有4处凝视错误 #include <stdio.h> #define N 10 //使用以下宏定义的凝视 #define BSC // #defin ...
- 快捷标签和ajax、json返回数据
<if 判断条件>标签</if><import>标签可以链接外部的样式表,和js<import file="js.util.Array" ...
- Spring JDBC查询返回对象代码跟踪
在封装方法的时候突然发现通过 ResultSetMetaData的getColumnCount()获取到的列明会多一列(ROWSTAT),而且每次的值都是1,目前没有找到相关信息,在国外网站上看到有类 ...
- 序列DP(输出有要求)
DP Time Limit:10000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status De ...
- 注册会计师带你用Python进行探索性风险分析(一)
https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/78519378
- ubuntu下操作端口的方法
最简单的一个操作:sudo ufw status可检查防火墙的状态,我的返回的是:不活动 sudo ufw version防火墙版本: ufw 0.29-4ubuntu1 Copyright 2008 ...
- Modeling of Indoor Positioning Systems Based on Location Fingerprinting
Kamol Kaemarungsi and Prashant Krishnamurthy Telecommunications Program School of Information Scienc ...
- 【python】-- 面向对象引子、概念
面向过程编程 1.编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马 ...
- nginx + uWSGI 为 django 提供高并发
django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发 nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地 ...
- Vue学习-基础语法
Vue v-if指令 Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTM ...