D. Babaei and Birthday Cake---cf629D(LIS线段树优化)
题目链接:http://codeforces.com/problemset/problem/629/D
题意就是现有n个蛋糕,蛋糕的形状是圆柱体,每个蛋糕的体积就是圆柱体的体积,每个蛋糕的编号是1---n,可以把蛋糕 i 放到蛋糕 j 上面,前提是 j<i 并且 Vj<Vi;最后求最大的体积是多少;
实质就是求上升子序列的最大和,但是由于n的范围是10w所以不能用n^2的复杂度,所以可以用线段树进行优化,时间复杂度变为nlogn;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define PI 4*atan(1.0)
#define N 302000
#define met(a, b) memset(a, b, sizeof(a)) #define Lson r<<1
#define Rson r<<1|1 double v[N], b[N]; struct node
{
int L, R;
double Max;///表示这个区间内的最大值,
int Mid(){ return (L+R)/; }
}a[N*]; void Build(int r, int L, int R)
{
a[r].L = L, a[r].R = R;a[r].Max = ;
if(L == R)return;
Build(Lson, L, a[r].Mid());
Build(Rson, a[r].Mid()+, R);
} double Query(int r, int L, int R)
{
if(L > R) return ; if(a[r].L == L && a[r].R == R)
return a[r].Max; if(R <= a[r].Mid())
return Query(Lson, L, R);
else if(L > a[r].Mid())
return Query(Rson, L, R);
else
{
double ans1 = Query(Lson, L, a[r].Mid());
double ans2 = Query(Rson, a[r].Mid()+, R);
return max(ans1, ans2);
}
} void Update(int r, int pos, double num)
{
if(a[r].L == a[r].R && a[r].L == pos)
{
a[r].Max = num;
return ;
}
if(pos <= a[r].Mid())
Update(Lson, pos, num);
else
Update(Rson, pos, num); a[r].Max = max(a[Lson].Max, a[Rson].Max);
} int main()
{
int n;
LL r, h;
while(scanf("%d", &n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%I64d %I64d", &r, &h);
v[i] = b[i] = PI*h*r*r;
}
sort(b, b+n);
int len = unique(b, b+n) - b; Build(, , len); double ans = ;
for(int i=; i<=n; i++)
{
int pos = lower_bound(b, b+len, v[i]) - b;///每次找到当前这个数能放的位置;
double res = Query(, , pos-) + v[i];///找到这个位置之前的最大值
Update(, pos, res);///找到之后把这个值加到那个位置上
ans = max(ans, res);
}
printf("%.12f\n", ans);
}
return ;
}
D. Babaei and Birthday Cake---cf629D(LIS线段树优化)的更多相关文章
- SGU 521 North-East ( 二维LIS 线段树优化 )
521. "North-East" Time limit per test: 0.5 second(s)Memory limit: 262144 kilobytes input: ...
- D. Babaei and Birthday Cake---cf629D(最长上升子序列和+线段树优化)
http://codeforces.com/problemset/problem/629/D 题目大意: 我第一反应就是求最长上升子序列和 但是数值太大了 不能直接dp求 可以用线段树优化一下 ...
- UVA-1322 Minimizing Maximizer (DP+线段树优化)
题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)
题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...
- Weak Pair---hud5877大连网选(线段树优化+dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...
- CodeForces 558E(计数排序+线段树优化)
题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...
- HDU4719-Oh My Holy FFF(DP线段树优化)
Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) T ...
随机推荐
- jquery.attach附件上传jquery插件
html: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv=&qu ...
- app服务端server端数据库设计
- wm_concat函数 用法
首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用 准备测试数据 SQL> ...
- Trie树(字典树)(1)
Trie树.又称字典树,单词查找树或者前缀树,是一种用于高速检索的多叉树结构. Trie树与二叉搜索树不同,键不是直接保存在节点中,而是由节点在树中的位置决定. 一个节点的全部子孙都有同样的前缀(pr ...
- HQL的执行过程
解释器.编译器.优化器完成HQL查询语句从词法分析.语法分析.编译.优化以及查询计划(Plan)的生成.生成的查询计划存储在HDFS中,并在随后有mapreduce调用执行. 举个例子: 第一步:输入 ...
- Discuz! X2验证码的产生及验证
http://www.mcqyy.com/wenku/jiaocheng/jzjc/cjc/106729.html http://blog.sina.com.cn/s/blog_4acbd39c010 ...
- spring AOP底层原理实现——jdk动态代理
spring AOP底层原理实现——jdk动态代理
- 【GIT】Git Flow最佳实践
Git Flow 工作流一共包含五种分支: 两个长期分支: 主分支 master:用于存放对外发布的版本,任何时候在这个分支拿到的,都是稳定的分布版 开发分支 develop:用于日常开发,存放最新的 ...
- JS-缓冲运动:菜单栏型悬浮框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Android 判断当前是否在跑monkey测试
/** * Returns true if Monkey is running. */ public static boolean isMonkeyRunning() { ...