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 ...
随机推荐
- struct--file_operations
struct--file_operations----------------------------------------- struct file_operations是一个字符设备把驱动 ...
- Linux下GPIO驱动(四) ----gpio_request();gpio_free();
//gpio_request申请gpio口 int gpio_request(unsigned gpio, const char *label) { struct gpio_desc *desc; s ...
- jquery 验证插件 validate
1)required:true 必输字段(2)remote:"check.php" 使用ajax方法调用check.php验证输入值(3)email:true 必须输入正确格式的电 ...
- Java学习--final与static
final是java的关键字,它所表示的是“这部分是无法修改的”. 编译期常量,它在类加载的过程就已经完成了初始化,所以当类加载完成后是不可更改的,编译期可以将它代入到任何用到它的计算式中,也就是说可 ...
- C# zip/unzip with ICSharpCode.SharpZipLib
download ICSharpCode and add reference using System; using System.Collections.Generic; using System. ...
- 【转载】Java 升级到jdk7后DbVisualizer 6 启动空指针的处理方案
将JDK从6升级到了7(或从其他电脑移植DBV文件夹后),每当启动DbVisualizer 6的时候都会报空指针异常 在官网上找到了相关的方案,如下: In the DbVisualizer inst ...
- java单例模式使用及注意事项
1. 说明 1)单例模式:确保一个类只有一个实例,自行实例化并向系统提供这个实例 2)单例模式分类:饿单例模式(类加载时实例化一个对象给自己的引用),懒单例模式(调用取得实例的方法如getInstan ...
- mongodb Install the MongoDB service
在用到mongodb时,首先要运行mongod.exe以启动mongo,这样就会出现命令框( command prompt),为了避免出现这种情况.要以服务的形式来启动mongo,这样就不会出现命令框 ...
- 新增tab页无法获取到数据,原来是URL的rewrite配置文件忘了修改
昨天怎么也不明白为什么就是不能短网址访问,而且更迷惑的是居然数据也获取不到. 今天早上在网上搜ThinkPHP,想学习下这个框架.虽然知道是MVC模式,但是URL数据传递到底有什么阀门是我尚未了解到的 ...
- 用AjaxPro实现二级联动
在实际asp.net项目中经常会遇到无刷新二级或者N级(N>=2)联动情况,其实N级联动和二级联动的原理都是一样的,实现这种办法有很多,一种是纯脚本实现(动态生成Array数组),一种 是采用微 ...