Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)
来源:互联网 发布:网络用语古是什么意思 编辑:程序博客网 时间:2024/05/22 05:23
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.
Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.
However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.
Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.
Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.
Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
2100 3040 10
942477.796077000
41 19 71 410 7
3983.539484752
In first sample, the optimal way is to choose the cake number 1.
In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
题意:有n个圆柱形蛋糕,给你r和h,然后每个蛋糕可以放在前面的比它小的蛋糕上面,求最大总体积。这是上升子序列最大和。
思路:先把体积离散化,对于每个蛋糕,可以知道肯定是放在它前面比它小的最大体积额的蛋糕上,这样可以保证以i结尾的和最大,于是dp[i]表示以i结尾的最大蛋糕体积和,线段树每次查询离散化后的区间[1, num[i]-1]的最大值,就是找到那个比它小最大的,然后加上去就可以了,更新线段树,最后过一遍就可以得到ans。
#include<iostream>#include<cstdio>#include<algorithm>#include<map>#include<cstring>#include<map>#include<set>#include<queue>#include<stack>#include<cmath>using namespace std;const double eps = 1e-10;const int inf = 0x3f3f3f3f, N = 1e5 +10;typedef long long ll;const double pi = acos(-1);using namespace std;struct Node{ double r, h;} p[N];double V(Node a){ return a.r*a.r*a.h*pi;}double dp[N], now[N], v[N], f[N];int num[N];struct node{ int l, r; double maxn;} s[100010 * 4];int n, m;void pushup(int cnt){ s[cnt].maxn = max(s[cnt<<1].maxn, s[cnt<<1|1].maxn);}void build_tree(int l, int r,int cnt){ s[cnt].l = l; s[cnt].r = r; if(l == r) { s[cnt].maxn = 0.0; return ; } int mid = (l + r)>>1; build_tree(l, mid, cnt*2); build_tree(mid+1, r, cnt*2+1); pushup(cnt);}void update(int x, double numx, int cnt){ if(s[cnt].l == x && s[cnt].r == x) { s[cnt].maxn = max(s[cnt].maxn, numx); return ; } int mid = (s[cnt].l + s[cnt].r)>>1; if(x<=mid) update(x, numx, cnt*2); if(x>mid) update(x, numx, cnt*2+1); pushup(cnt);}double query(int l , int r, int cnt){ if(s[cnt].l >= l && s[cnt].r <= r) return s[cnt].maxn; int mid = (s[cnt].l + s[cnt].r)>>1; double res = 0.0; if(l<=mid) res = max(query(l, r, cnt*2), res); if(r>mid) res = max(query(l, r, cnt*2+1), res); return res;}int main(){ int n; while(cin>>n) { build_tree(1, n, 1); memset(dp, 0, sizeof(dp)); for(int i = 0; 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 = 0; i<n; i++) num[i] = lower_bound(f , f+n, v[i]) - f + 1; for(int i = 0; i<n; i++) { if(num[i]-1==0) dp[i] = v[i]; else dp[i] = query(1, num[i]-1, 1) + v[i]; //cout<<dp[i]<<endl; update(num[i], dp[i], 1); } double ans = 0.0; for(int i = 0; i<n; i++) ans = max(dp[i], ans); printf("%.10f\n", ans); } return 0;}
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+离散化优化DP)
- Codeforces Round #343 (Div. 2) 629D. Babaei and Birthday Cake 线段树
- Codeforces Round #343 (Div. 2)D - Babaei and Birthday Cake 线段树
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+dp)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake
- codeforces #343 div2 D. Babaei and Birthday Cake(DP+离散化+线段树优化)
- Codeforces 629D Babaei and Birthday Cake【dp+线段树+离散化】
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(dp、BIT)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(dp + BIT)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake (复杂的DP)
- codeforces 629D-Babaei and Birthday Cake(dp && 线段树或树状数组离散优化)
- codeforces 629 D. Babaei and Birthday Cake dp + 线段树
- Codeforces 629D - Babaei and Birthday Cake 线段树
- CF #343 div2 D Babaei and Birthday Cake dp+线段树维护+(离散)
- #Codeforces 343 [div2] D. Babaei and Birthday Cake 【线段树优先队列查询优化】
- Java NIO 同步非阻塞Socket DEMO
- 面向对象三大特性
- 在Ubuntu上安装Wine
- CodeForces 630 E. A rectangle(水~)
- 【Linux 基础篇】之常用命令
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)
- C++ Primer阅读心得(第十六章)
- HDOJ 5630Rikka with Chess(游戏)
- [LeetCode] Same Tree
- Struts2 路径问题
- 1040. 有几个PAT
- PPT突然不能翻页了
- 【iOS】数据持久化
- CodeForces 630 F. Selection of Personnel(组合数学)