题意:做蛋糕,给出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的更多相关文章

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

  2. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

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

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

  5. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

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

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

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

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

随机推荐

  1. json在线校验

    弄了一个在线校验,清爽无广告,欢迎大家收藏   http://www.zhhoney.com/

  2. ASP.NET 学习小记 -- “迷你”MVC实现(2)

    Controller的激活 ASP.NET MVC的URL路由系统通过注册的路由表对HTTO请求进行解析从而得到一个用户封装路由数据的RouteData对象,而这个过程是通过自定义的UrlRoutin ...

  3. 【JTA】JTA允许应用程序执行分布式事务处理

    JTA,即Java Transaction API,JTA允许应用程序执行分布式事务处理——在两个或多个网络计算机资源上访问并且更新数据.JDBC驱动程序的JTA支持极大地增强了数据访问能力. htt ...

  4. javascript高级编程笔记01(基本概念)

    1.在html中使用JavaScript 1.  <script> 元素 <script>定义了下列6个属性: async:可选,异步下载外部脚本文件. charset:可选, ...

  5. 用python实现k近邻算法

    用python写程序真的好舒服. code: import numpy as np def read_data(filename): '''读取文本数据,格式:特征1 特征2 -- 类别''' f=o ...

  6. <一> ASP.NET Html 表单

    把客户端数据发送到服务器端用<form>标签 htmlpage.html Default2.aspx.cs

  7. MongoDB 覆盖索引查询

    MongoDB 覆盖索引查询 官方的MongoDB的文档中说明,覆盖查询是以下的查询: 所有的查询字段是索引的一部分 所有的查询返回字段在同一个索引中 由于所有出现在查询中的字段是索引的一部分, Mo ...

  8. Jquery.Validate验证CheckBoxList,RadioButtonList,DropDownList是否选中

    http://blog.csdn.net/fox123871/article/details/8108030 <script type="text/javascript"&g ...

  9. ASP.NET MVC 入门1、简介

    什么是MVC模式 MVC(Model-View-Controller,模型-视图-控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Co ...

  10. *[topcoder]LittleElephantAndIntervalsDiv1

    http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...