CF 629 D. Babaei and Birthday Cake 线段树+DP
来源:互联网 发布:淘宝主播招聘 编辑:程序博客网 时间:2024/05/29 12:14
虽然没有开dp数组,但满满的dp思想。
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 jonly 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.
#include<bits/stdc++.h>#define all(x) (x).begin(), (x).end()#define rep(i,n) for(int i=0 ;i<(n) ;i++)#define lson ind<<1,le,mid#define rson ind<<1|1,mid+1,ri#define MID int mid=(le+ri)>>1#define eps 1e-10using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn= 100000 ;const double PI=4.0*atan(1.0);int n;struct SegmentTree{ double sum[4*maxn]; /* 线段树不能这样初始化,当初很天真的这样写了 原因是不同的n值的情况下,数组1或者2对应的区间是不同的, 如果只是这样,会有上次残留的记录。 void clear() { for(int i=0;i<=4*n;i++) { sum[i]=0; } } */ void build(int ind,int le,int ri) { if(le==ri) { sum[ind]=0; return; } MID; build(lson); build(rson); sum[ind]=0; } void update(int ind,int le,int ri,int pos,double val) { if(le==ri) { sum[ind]=val; return; } MID; if(pos<=mid) update(lson,pos,val); else update(rson,pos,val); sum[ind]=max(sum[ind<<1],sum[ind<<1|1]); } double query(int ind,int le,int ri,int x,int y) { if(x<=le&&ri<=y)//我居然写出这么错误的代码 : if(le==ri) { return sum[ind]; } MID; double ans=0; if(x<=mid) ans=max(ans,query(lson,x,y) ); if(y>mid) ans=max(ans,query(rson,x,y) ); return ans; }}sgt;struct Node{ int ind; double vol; bool operator<(const Node& y)const//题目要求,放在上面的圆柱体体积要严格大于底下的 { if(fabs(vol-y.vol)>eps ) return vol<y.vol; return ind>y.ind;//这个就是配合底下的处理,防止出现放在上面的圆柱体体积等于底下的 }} a[maxn+10];void work(){ sgt.build(1,1,n); double ans=0; for(int i=1;i<=n;i++) { int ind=a[i].ind; double ret=a[i].vol+sgt.query(1,1,n,1,ind); sgt.update(1,1,n,ind,ret); ans=max(ans,ret); } printf("%.9f\n",ans);}int main(){ while(~scanf("%d",&n)) { double r,h; for(int i=1;i<=n;i++) { scanf("%lf%lf",&r,&h); a[i].vol= r*r*PI*h; a[i].ind=i; } sort(a+1,a+1+n);//更好的排序方法是利用pair,将ind赋值为-ind,再直接sort work(); } return 0;}
- CF 629 D. Babaei and Birthday Cake 线段树+DP
- cf - 629D Babaei and Birthday Cake(DP+线段树维护)
- codeforces 629 D. Babaei and Birthday Cake dp + 线段树
- CF #343 div2 D Babaei and Birthday Cake dp+线段树维护+(离散)
- CF 629D Babaei and Birthday Cake(线段树单点更新)
- Codeforces 629D - Babaei and Birthday Cake 线段树
- codeforces 629D-Babaei and Birthday Cake(dp && 线段树或树状数组离散优化)
- Codeforces 629D Babaei and Birthday Cake 【线段树优化dp】
- Codeforces 629D Babaei and Birthday Cake(线段树优化dp)
- CodeForces 629D Babaei and Birthday Cake(线段树维护DP)
- Codeforces 629D Babaei and Birthday Cake【dp+线段树+离散化】
- CodeForces 629 D.Babaei and Birthday Cake(dp+线段树)
- codeforces #343 div2 D. Babaei and Birthday Cake(DP+离散化+线段树优化)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+dp)
- #342Div2-625D Babaei and Birthday Cake线段树优化DP
- codeforces #334 D. Babaei and Birthday Cake (线段树或者dp+二分)
- Codeforces 629D Babaei and Birthday Cake(DP)
- CODEFORCES 629 D Babaei and Birthday Cake
- 原码补码反码的关系
- 5个随机数排序
- 2014 地宫取宝
- HDOJ 1271 整数对(数学)
- 内置Content Provider
- CF 629 D. Babaei and Birthday Cake 线段树+DP
- ConcurrentHashMap、synchronized与线程安全
- java编程中常见的拼写错误
- leetcode之house Robber
- mapreduce运行机制
- cf 345-B - Beautiful Paintings 暴力什么的太水了
- Docker安装(在Ubuntu中安装Docker)
- 拦截器和过滤器的区别?
- 方法的覆盖和方法的重载的比较