题目: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. Dynamics 365-RetrieveDependenciesForDeleteRequest

    不少人在使用Dynamics 365的时候,或多或少都会遇到Delete a Component的情况,比如Unregister a Plugin/Workflow. 想象这么一个常见的情形:你定制了 ...

  2. 安卓开发:UI组件-Button和EditText

    2.3Button Button继承自TextView,除了通过属性设置按钮样式,还可以通过绑定drawable文件的方式来实现不同样式. 2.3.1按钮样式 新建Activity:ButtonAct ...

  3. 9102 IT人保持记忆力及健康的方法

    做技术时间久了,我们会发现有的时候我们会感觉记忆力衰减太快,前脚刚忙完的事或者刚做完计划任务没多久就遗忘了,或者是以前轻车熟入的方法死活都记不起来了,亦或者之前学习一门技术很快就掌握真谛,现在即便花N ...

  4. Codechef Bear and Clique Distances

    题目:Bear and Clique Distances 描述:共有N个点,前1—K个点任意两点之间有一条无向边,边的权值为X,再任意给M条边(u,v,w)(不重复),求任意一点到其余各点的最短路. ...

  5. Bootstrap -- 下拉菜单、输入框组、导航菜单

    Bootstrap -- 下拉菜单.输入框组.导航菜单 1. 下拉菜单 可以使用带有各种大小按钮的下拉菜单:.btn-lg..btn-sm 或 .btn-xs. 实现下拉菜单: <!DOCTYP ...

  6. LeetCode算法题-Design LinkedList(Java实现)

    这是悦乐书的第300次更新,第319篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第168题(顺位题号是707).设计链表的实现.您可以选择使用单链表或双链表.单链表中的 ...

  7. 做为一个Python程序员的基本素养

    今天在学习的过程中,明白了一些不是Python标准所必须要做的事情,二是做为一个合格的Python程序员应该所遵从的一些规范 分享给大家,有不足的地方请大家指正,此下是我学习的一点心得: 1.在给变量 ...

  8. .NET MVC全局异常处理(一)

    目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关的配置,但没有实际做过,以为改下设定就 ...

  9. ESP8266当中继

    WiFi推原理(转) http://jb.tongxinmao.com/Article/Detail/id/412 https://www.anywlan.com/thread-409913-1-1. ...

  10. 两台主机,ssh端口不同,如何拷贝文件

    A主机ip:172.26.225.199 ssh端口12995 B主机ip:172.26.225.200 ssh端口12991 将B主机的文件拷贝到A主机 [root@test2019030517 s ...