Codeforces 629D Babaei and Birthday Cake

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|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
//head
const 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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值