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 ...
随机推荐
- C# 生成随机索引列表
/// <summary> /// 生成随机索引列表 /// </summary> /// <param name="maxNumber">&l ...
- 坚定关于考研或者工作的决定:work
转眼之间,我已经夸过了大二结束的节点,已经是一个准大三了: 在这个岔路口,首要的选择就是考研和工作的选择:我也有过犹豫要不要考研,最终还是放弃了考研的想法,从考研的利弊两个方面来谈: 首 ...
- 理解java的三种代理模式
代理模式是什么 代理模式是一种设计模式,简单说即是在不改变源码的情况下,实现对目标对象的功能扩展. 比如有个歌手对象叫Singer,这个对象有一个唱歌方法叫sing(). 1 public class ...
- DVWA 黑客攻防演练(一) 介绍及安装
原本是像写一篇 SELinux 的文章的.而我写总结文章的时候,总会去想原因是什么,为什么会有这种需求.而我发觉 SELinux 的需求是编程人员的神奇代码或者维护者的脑袋短路而造成系统容易被攻击.就 ...
- SQL根据细粒度为天的查询
当我们集成了一些前端框架,在某些展示页面上往往具有某些查询条件.而这其中日期查询的处理又较为麻烦,此处,我罗列了一种当前台上传了一种默认的date格式的日期查询数据至后台未经Controller或Se ...
- Class.isAssignableFrom与instanceof的区别
isAssignableFrom 假设有两个类Class1和Class2.Class1.isAssignableFrom(Class2)表示: 类Class1和Class2是否相同. Class1是否 ...
- Linux 自动化部署DNS服务器
Linux 自动化部署DNS服务器 1.首先配置主DNS服务器的IP地址,DNS地址一个写主dns的IP地址,一个写从dns的地址,这里也可以不写,在测试的时候在/etc/resolv.conf中添加 ...
- Python爬虫之pyquery库的基本使用
# 字符串初始化 html = ''' <div> <ul> <li class = "item-0">first item</li> ...
- bat(批处理)命令(tomcat 7.0.75 startup.bat 命令集)
本文主要介绍tomcat 7.0.75中startup.bat(位置:tomcat目录\bin)中涉及到的bat命令,为tomcat源码研究做准备. startup.bat中涉及到的bat命令如下: ...
- 转:[kipmi0]进程导致系统负载高
最近一个用户这边服务器出现服务器负载很高的情况,原本正常是0.3~0.5左右 不正常的时候会达到3,重启机器就正常,开始以为是程序问题,后来在观察的时候把程序给杀掉了 然后重启,结果负载还是很高,于 ...