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 ...
随机推荐
- PHPOffice下PHPWord生成Word2007(docx)使用方法
要正常使用,下载依赖包: PhpOffice/Common:https://github.com/PHPOffice/Common Zend/Escaper:https://github.com/ze ...
- C# 使用SkinSharp皮肤库
SkinSharp是Windows环境下一款强大的通用换肤库. SkinSharp作为通用换肤库,只需要在您的程序中添加一行代码,就能让您的界面焕然一新,并拥有多种主题风格和色调的动态切换功能以及Ae ...
- Sqrt算法
转自原文:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 一个Sqrt函数引发的血案 2010-10 ...
- MySQL Server 5.7.13
如何安装MySQL,MySQL两种安装方式_百度经验 http://jingyan.baidu.com/article/cd4c2979033a17756f6e6047.html "C:\P ...
- JavaScript匿名函数和回调函数
匿名函数的自调函数格式: (function(){ //代码 })(); <script type="text/javascript"> (function(){ al ...
- phpcms v9如何更改分页显示条数?
默认显示页码数有10条,比如想更改成显示3条,例如这样 上一页 1 2 3...34 下一页 更改phpcms\libs\functions\global.func.php,找到分页函数,大概在665 ...
- angular学习(十五)——Provider
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/60966263 Provider简单介绍 每一个web应用都是由多个对象协作完毕 ...
- Mac下,如何把Github上的仓库删除掉
这个虽然简单,但是还是做个记录,当初也是找不到地方,最终还是去百度了,步骤很简单: 如下: 1.进入Github主页,选中你要删除的仓库,点击进入到如下页面:
- 【MySQL】查询时强制区分大小写的方法
MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名重复,又会浪费很多资源.再者,李逵.李鬼的多起来,侦辨起来很困难.要做到这一点,要么在建表时,明确大小写敏感(字段明确大小写敏感) sql ...
- Sass-学习笔记【进阶篇】
特别说明: 没有sass基础请移步:[Sass-学习笔记[基础篇]]http://www.cnblogs.com/padding1015/articles/7056323.html 最底部附结构图(实 ...