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 ...
- 将ecshop中的session机制重写,从DB移植到Memcache中去
<?php if (!defined('IN_ECS')) { die('Hacking attempt'); } /*------------------------------------- ...
- mysql 查看数据库大小
select table_schema, concat(truncate(sum(data_length)/1024/1024,2),' mb') as data_size,concat(trunca ...
- django1.6之mysql配置
# Database# https://docs.djangoproject.com/en/1.6/ref/settings/#databases setting 配置修改 INSTALLED_APP ...
- vi 在行首尾添加字符串
在行首尾添加字符串 用下列命令在文本每行的行首添加字符串“NewString”,请注意空格的存在. :g/^/s//NewString/g :%s/^/NewString/g ...
- 正则表达式的秘籍-b
一. 正则表达式和其他方法的比较 1.我们一般将谓词和正则表达式配合使用,这是最常用的方法. - (BOOL)validateNumber:(NSString *) textString { ...
- iOS runloop 资源汇总-b
RunLoop 是 iOS 和 OSX 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何利 ...
- Python标准库与第三方库详解(转载)
转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b ...
- python 统计单词个数
根据一篇英文文章统计其中单词出现最多的10个单词. # -*- coding: utf-8 -*-import urllib2import refrom collections import Coun ...
- Citect:How do I translate Citect error messages?
http://www.opcsupport.com/link/portal/4164/4590/ArticleFolder/51/Citect To decode the error messag ...