Codeforces 629D Babaei and Birthday Cake

来源:互联网 发布:软件开发报告 编辑:程序博客网 时间:2024/04/29 08:29

http://codeforces.com/problemset/problem/629/D


题意是说蛋糕i只能放在最底下或是放在一个编号比i小、体积也比i小的蛋糕上,求叠在一起的蛋糕体积和的最大值。类似于LIS。直接用dp肯定会超时,这里用线段树维护。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <vector>#include <stack>#include <queue>#define fi first#define se second#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;//headconst double PI = acos(-1.0);const int maxn = 100005;LL t[maxn<<2];LL dp[maxn];LL v[maxn];LL vs[maxn];void pushup(int rt){    t[rt] = max(t[rt<<1], t[rt<<1|1]);}void build(int l, int r, int rt){    rt[t] = 0;    int m = (l+r)>>1;    if(l!=r)    {        build(lson);        build(rson);    }}LL query(int L, int R, int l, int r, int rt){    if(L<=l && r<=R)return t[rt];    int m = (l+r)>>1;    LL ret = -1;    if(L<=m)        ret = max(ret, query(L, R, lson));    if(R>m)        ret = max(ret, query(L, R, rson));    return ret;}void update(int l, int r, int rt, int pos, LL tgt){    if(l==r)    {        t[rt] = max(tgt, t[rt]);        return;    }    int m = (l+r)>>1;    if(pos<=m)        update(lson, pos, tgt);    else        update(rson, pos, tgt);    pushup(rt);}int main(){    int n;    scanf("%d",&n);    for(int i=1; i<=n; i++)    {        LL a,b;        scanf("%I64d%I64d",&a,&b);        v[i] = vs[i] = a*a*b;    }    build(1, n, 1);     sort(vs+1, vs+n+1);    LL ans = 0;    for(int i=1; i<=n; i++)    {        int pos = lower_bound(vs+1, vs+n+1, v[i]) - vs;        if(pos == 1)            dp[i] = v[i];        else            dp[i] = query(1, pos-1, 1, n, 1) + v[i];        update(1, n, 1, pos, dp[i]);        ans = max(ans, dp[i]);    }    printf("%.9f\n", 1.0*PI*ans);    return 0;}
0 0