Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP
题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和。
思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况下的体积最大值dp[i]。每次查询比蛋糕i小且最大体积的蛋糕,然后更新线段树。注意此题查询的技巧!!查询区间不变l,r,才能保证每次查到的是小且最大体积。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define clc(a,b) memset(a,b,sizeof(a))
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1e5 + ;
const int inf=0x3f3f3f3f;
const double pi=acos(-);
double dp[maxn];
double v[maxn],f[maxn];
int num[maxn];
struct node
{
double r,h;
} p[maxn]; double V(node a)
{
return a.r*a.r*a.h*pi;
} struct Node
{
int l,r;
double maxx;
} tree[*]; void pushup(int cnt)
{
tree[cnt].maxx=max(tree[cnt<<].maxx,tree[cnt<<|].maxx);
} void b_tree(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
if(l==r)
{
tree[rt].maxx=0.0;
return ;
}
int mid=(l+r)>>;
b_tree(l,mid,rt<<);
b_tree(mid+,r,rt<<|);
pushup(rt);
} double query(int l,int r,int rt)
{
if(tree[rt].l>=l&&tree[rt].r<=r)
{
return tree[rt].maxx;
}
int mid=(tree[rt].l+tree[rt].r)>>;
double ans=0.0;
if(l<=mid)
ans=max(ans,query(l,r,rt<<));
if(r>mid)
ans=max(ans,query(l,r,rt<<|));
return ans;
} void update(int x,double val,int rt)
{
if(tree[rt].l==x&&tree[rt].r==x)
{
tree[rt].maxx=max(tree[rt].maxx,val);
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(x<=mid)
update(x,val,rt<<);
else
update(x,val,rt<<|);
pushup(rt);
} int main()
{
int n;
while(cin>>n)
{
b_tree(,n,);
clc(dp,);
for(int i=; i<n; i++)
{
scanf("%lf%lf",&p[i].r,&p[i].h);
v[i]=V(p[i]);
f[i]=v[i];
}
sort(f,f+n);
for(int i=; i<n; i++)
{
num[i]=lower_bound(f,f+n,v[i])-f+;
}
for(int i=; i<n; i++)
{
if(num[i]-==)
dp[i]=v[i];
else
dp[i]=query(,num[i]-,)+v[i];
update(num[i],dp[i],);
}
double ans=0.0;
for(int i=; i<n; i++)
ans=max(ans,dp[i]);
printf("%.10f\n",ans);
}
return ;
}
Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP的更多相关文章
- 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 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树
C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- codeforces 629D D. Babaei and Birthday Cake (线段树+dp)
D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp
E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
随机推荐
- PHP开发圣经读书笔记01
从今天开始,以“圣经”这本书为教材,系统的温习一下php,之前都是看视频学的. 1.访问表单变量--php变量名称必须与表单域的名称一致 例:$_POST['uname']; //表示把表单域中na ...
- NodeJS+ExpressJS+SocketIO+MongoDB应用模板
OS:Win8.1 with update 关键字:NodeJS,ExpressJS,SocketIO,MongoDB. 1.源代码下载:https://github.com/ldlchina/ESM ...
- Oracle的rowid结构解析
SQL> select rowid,deptno from dept; ROWID DEPTNO ------------------ ---------- A ...
- 找工作ing
找工作已经一个月多了,这一个月跑来跑去,挺累的,倒也不是身体有多累,关键是心累!作为一名控制工程专业的研究生,想找一份软件开发类的工作,没想到这么难!在起初的时候,觉得自己C++和JAVA都会,找哪个 ...
- vs2015Update2的一个坑
最近更新了vs2015到update2,然后,蛋疼的事情就来了. 首先发现QT不能用了 boost编译也出问题了 查找了2天,发现问题所在,在于windows sdk更新 10.0.10586.0 了 ...
- oracle srvctl 命令
SRVCTL命令可以控制RAC数据库中的instance,listener以及services. 通常SRVCTL在ORACLE用户下执行.下面我们来介绍srvctl命令. 1.通过SRVCTL命令来 ...
- Java Web 前端高性能优化(二)
一.上文回顾 上回我们主要从图片的合并.压缩等方面介绍前端性能优化问题(详见Java Web 前端高性能优化(一)) 本次我们主要从图像BASE64 编码.GZIP压缩.懒加载与预加载以及 OneAP ...
- 练习--LINUX进程间通信之信号SIGNAL
同样的,信号也不要太迷信可靠信号及不及靠信号,实时或非实时信号. 但必须要了解这些信号之间的差异,函数升级及参数,才能熟练运用. ~~~~~~~~~~~~~~~~ 信号本质 信号是在软件层次上对中断机 ...
- 为tomcat启用nio机制
tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...
- [string]Codeforces158C Cd and pwd commands
题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...