Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake

题目链接:点这里!!!


题意:(我直接简化了)给你一个长度为n的序列,然后叫你求一个单调递增的序列且他们的和最大为多少。(所有数都是正数)

数据范围:n<=100000


题解:直接线段树加离散化。假设当前的数为v[i](离散化后),我们去线段树里找小于v[i]中最大的值为vmax。

然后在v[i]这个位置上直接更新v[i]+vmax,注意一下如果v[i]之前存在值,取max更新就可以了,然后取最大值就可以了。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL  MOD = 1000000007;
const int N = 100000+15;
const int maxn = 8e3+15;
const int letter = 130;
const int INF = 2e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
double v[N],f[N];
double ans[N<<2];
void pushup(int lr){
    ans[lr]=max(ans[lr<<1],ans[lr<<1|1]);
}
void build(int lr,int l,int r){
    ans[lr]=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int id,double val,int lr,int l,int r){
    if(l==r){
        ans[lr]=val;
        return;
    }
    int mid=(l+r)>>1;
    if(id<=mid) update(id,val,lson);
    else update(id,val,rson);
    pushup(lr);
}
double query(int ll,int rr,int lr,int l,int r){
    if(ll>rr) return 0;
    if(ll<=l&&r<=rr) return ans[lr];
    int mid=(l+r)>>1;
    double max1=0;
    if(ll<=mid) max1=max(max1,query(ll,rr,lson));
    if(rr>mid)  max1=max(max1,query(ll,rr,rson));
    return max1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        double r,h;
        scanf("%lf%lf",&r,&h);
        f[i]=v[i]=r*r*pi*h;
    }
    int cnt=n;
    sort(f+1,f+cnt+1);
    cnt=unique(f+1,f+n+1)-(f+1);
    build(1,1,cnt);
    double max1=0;
    for(int i=1;i<=n;i++){
        int vs=lower_bound(f+1,f+cnt+1,v[i])-f;
        double pc=query(1,vs-1,1,1,cnt);
        max1=max(max1,v[i]+pc);
        if(v[i]+pc>query(vs,vs,1,1,cnt))update(vs,v[i]+pc,1,1,cnt);
    }
    printf("%.10f\n",max1);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值