Codeforces 629D Babaei and Birthday Cakes DP+线段树
题目:http://codeforces.com/contest/629/problem/D
题意:有n个蛋糕要叠起来,能叠起来的条件是蛋糕的下标比前面的大并且体积也比前面的大,问能叠成的最大体积
思路:DP[i]表示拿到第i个蛋糕时最多能叠成的体积,转移就是这样DP[i]=max(DP[1...i])+V[i];如果直接做的话复杂度是n^2的,用线段树维护比它小的蛋糕的能叠的最大体积,事先离散化,
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define PI acos(-1.0)
const int maxn=1e5+;
struct cake{
int r,h;
double V;
cake(){};
cake(int r_,int h_):r(r_),h(h_){V=PI*r*r*h;};
};
struct node{
int l,r;
double maxx;
int mid(){return (l+r)>>;}
};
node tree[maxn<<];
void pushup(int rt){
tree[rt].maxx=max(tree[rt<<].maxx,tree[rt<<|].maxx);
}
void build(int l,int r,int rt){
tree[rt].l=l,tree[rt].r=r;
tree[rt].maxx=;
if(l==r) return ;
int mid=tree[rt].mid();
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
void update(int index,double C,int L,int R,int rt){
if(L==index&&R==index){
tree[rt].maxx=max(tree[rt].maxx,C);
return ;
}
int mid=tree[rt].mid();
if(index<=mid) update(index,C,L,mid,rt<<);
else update(index,C,mid+,R,rt<<|);
pushup(rt);
}
double query(int l,int r,int L,int R,int rt){
if(L>=l&&R<=r) {
return tree[rt].maxx;
}
double maxx=-1.0;
int mid=tree[rt].mid();
if(l<=mid) maxx=max(maxx,query(l,r,L,mid,rt<<));
if(r>mid) maxx=max(maxx,query(l,r,mid+,R,rt<<|));
return maxx;
}
cake a[maxn];
double v[maxn];
int n;
void solve(){
sort(v+,v++n);
int size=unique(v+,v++n)-v;
double ans=;
build(,size,);
double tmp;
for(int i=;i<=n;i++){
int pos=lower_bound(v+,v+size,a[i].V)-v;
tmp=;
tmp=query(,pos-,,size,);
if(pos-==) tmp=;
tmp+=a[i].V;
ans=max(ans,tmp);
update(pos,tmp,,size,);
}
printf("%.10f\n",ans);
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
int r,h;
scanf("%d %d",&r,&h);
a[i]=cake(r,h);
v[i]=a[i].V;
}
solve();
}
return ;
}
Codeforces 629D Babaei and Birthday Cakes DP+线段树的更多相关文章
- Codeforces 629D Babaei and Birthday Cake(线段树优化dp)
题意: n个蛋糕编号从小到大编号,j号蛋糕可以放在i号上面,当且仅当j的体积严格大于i且i<j,问最终可得的最大蛋糕体积. 分析: 实质为求最长上升子序列问题,设dp[i]从头开始到第i位的最长 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces 833B 题解(DP+线段树)
题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- 华为有AI,这场转型战有点大
华为有AI,这场转型战有点大 https://mp.weixin.qq.com/s/qnUP5cgbNxXcAT82NQARtA 李根 发自 凹非寺 量子位 报道 | 公众号 QbitAI 华为有AI ...
- word中字体大小(pt)和网页中css设置font-size时用的px大小对应关系
pt与px转换关系为 1px= 0.75pt. 所以word中五号字体(10.5pt)在网页中对应的大小为font-size:14px.(10.5 / 0.75 = 14) 初号44pt 小初36pt ...
- asp.net core 2.1 部署IIS(win10/win7)
asp.net core 2.1 部署IIS(win10/win7) 概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器 ...
- SQL Server Log Shipping学习总结
SQL Server的日志传送(log shipping)技术一直比较鸡肋,尤其当SQL Server 推出了Always On技术以后,估计使用日志传送(log shipping)这种技术方案的 ...
- C++17剖析:string_view的实现,以及性能
主要内容 C++17标准发布,string_view是标准新增的内容.这篇文章主要分析string_view的适用范围.注意事项,并分析string_view带来的性能提升,最后从gcc 8.2的li ...
- Python列表之班荆道故
列表list初识 列表是python的基础数据类型之一 ,它是以[ ]括起来, 每个元素用' , '隔开而且可以存放各种数据类型: list列表的定义: list_ = []list_1 = [&qu ...
- 【转载】关于generate用法的总结【Verilog】
原文链接: [原创]关于generate用法的总结[Verilog] - nanoty - 博客园http://www.cnblogs.com/nanoty/archive/2012/11/13/27 ...
- 使用mybatis操作AS400数据库
先简单说一下怎么使用[jt400.jar]连接AS400上的DB2数据库. ※ jt400.jar资源,如果有安装AS400客户端的话,参考IBM官网 ※ http://www-01.ibm.com/ ...
- 我的第一个python web开发框架(26)——定制ORM(二)
弄完底层数据库操作模块后,接下来要做的是ORM的正式设计.在开始之前,我们需要思考一下怎么来设计一个ORM呢?这个类它能帮助我们处理什么样的问题?需要有哪些功能模块?怎么做到针对不同的数据库与表单进行 ...
- MySQL 数据库的创建&修改
-- 创建数据库 CREATE DATABASE [IF NOT EXISTS]<数据库名> DEFAULT CHARACTER SET utf8; -- 默认字符集为utf8 -- 指定 ...