题目: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+线段树的更多相关文章

  1. Codeforces 629D Babaei and Birthday Cake(线段树优化dp)

    题意: n个蛋糕编号从小到大编号,j号蛋糕可以放在i号上面,当且仅当j的体积严格大于i且i<j,问最终可得的最大蛋糕体积. 分析: 实质为求最长上升子序列问题,设dp[i]从头开始到第i位的最长 ...

  2. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  3. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  4. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  5. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  7. 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 ...

  8. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  9. Codeforces 833B The Bakery dp线段树

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 2019年度SAP项目实践计划

    2019年度SAP项目实践计划 一晃2018年过去了,而新的一年说来就来了. 对于新的一年,笔者也难免有所畅想.早在上个月下旬就开始制定新年的诸多计划,比如写作计划,比如人工智能学习计划,比如新年度旅 ...

  2. 如何正确使用Espresso来测试你的Android程序

    UI测试在Android平台上一直都是一个令人头痛的事情, 由于大家平时用的很少, 加之很多文档的缺失, 如果很多东西从头摸索,势必踩坑无数. 自Android24正式淘汰掉了Instrumentat ...

  3. Centos7 安装Tomcat并运行程序

    运行环境:Centos7 jdk:1.8.0_171  Tocmcat:8.5.31 下载地址 :https://tomcat.apache.org/download-80.cgi#8.5.31 Ap ...

  4. 多层json的构造,取值,还有使用bootstrap的tree view在前端展示的相关问题

    bootstrap-tree view是一款非常好用的插件,它可以添加任意多层节点,效果如下所示: 使用之前需要在HTML页面添加依赖文件: <link href="bootstrap ...

  5. SQL Sever AlwaysOn的数据同步原理

    1. SQL Server AlwaysOn数据同步基本工作 AlwaysOn 副本同步需要完成三件事: 1.把主副本上发生的数据变化记录下来. 2.把这些记录传输到各个辅助副本. 3.把数据变化在辅 ...

  6. DataReader的使用

    public List<Student> GetList()        {            string sql = "select * from Student&qu ...

  7. Clickhouse v18编译记录

    简介 ClickHouse是"战斗民族"俄罗斯搜索巨头Yandex公司开源的一个极具"战斗力"的实时数据分析数据库,是面向 OLAP 的分布式列式DBMS,圈内 ...

  8. python开发规范和(configparser、random模块)

    目录结构: bin:存放程序入口,程序启动文件. conf:存放配置文件,配置文件主要是一些全局变量,路径信息等. core:程序核心文件,不涉及到业务逻辑. app:存放和系统业务相关的逻辑. db ...

  9. GCD多线程的一个全面的题目

    GCD多线程的一个全面的题目  

  10. China Tightens Recycling Import Rules

    China Tightens Recycling Import Rules We have all seen the pictures of cities in China with air poll ...