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 ...
随机推荐
- Pacbio 纯三代组装复活草基因组
对于植物等真核生物基因组来说,重复序列, 多倍体,高杂合度等特征在利用二代数据进行组装的时候都会有很大的问题: 利用二代数据组装出来的基因组,大多达不到完成图的水准,通常只是覆盖到编码蛋白的基因区域, ...
- [mysql] 先按某字段分组再取每组中前N条记录
From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/33002126 ...
- Python 安装环境
一.setuptools安装 1.下载ez_setup.py(https://bootstrap.pypa.io/ez_setup.py),并放到Python目录之中(版本相互一致): 2.使用CMD ...
- Centos安装Memcache
Memcache概述 官方 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据.简单的说就是将数据调用到内存中,然后从内存 ...
- 如何使用微信小程序制作banner轮播图?
在前端工程师的工作中,banner是必不可少的,那缺少了DOM的小程序是如何实现banner图的呢?如同其他的框架封装了不同的banner图的方法,小程序也封装了banner的方法,来让我一一道来: ...
- filter IE滤镜(Internet Explorer)CSS
收集一些IE滤镜,留作之后开发用. 透明度 #myElement { opacity: .; /* other browsers */ filter: progid: DXImageTransform ...
- split()有个坑
刚才在做DBMS课程设计的时候遇到了一个以前遇到过的问题不过这次我没有一眼认出来,想了好一会才想起来. 就是在用split()方法来分割路径名字符串的时候,比如 String path = “E:\s ...
- struts2 中redirectAction如何传递参数!
在struts2中,初学者因为参数传递的问题往往会出现一些错误. 比如页面跳转的问题,在用户注册中,以一下代码作为案例: <struts> <constant name=" ...
- 关于用phonegap 3.0+ 打包后sencha touch按钮点击切换动画延迟接近一秒的以及界面闪烁的解决方案
android的webview对硬件加速的支持貌似很不理想,在开启硬件加速的情况下,css3这些需要调用硬件加速的样式会大幅拖慢html5的webapp,在htc的部分手机上还会因开启硬件加速而导致闪 ...
- Nginx的安装和配置文件
一.什么是Nginx 反向代理的高手,可以做web服务器.smtp服务器.ftp服务器,也可以做waf等等.原理,反向代理,收集client请求然后转发给自己lan内的服务器,将请求到的资源回转给客户 ...