51Nod - 1376 最长递增子序列的数量(树状数组+DP)*

探讨如何利用树状数组和动态规划(DP)解决51Nod上的1376题,详细解析最长递增子序列问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://cn.vjudge.net/problem/51Nod-1376

#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int  maxn =5e4+5;
const int mod=1e9+7;
/*
题目大意:给定一个序列,统计最长递增子序列的个数。

51nod上的题目感觉思维性好强啊,思路也是网上学习来的。
如果单单要求出最长递增子序列的长度,这道题已经可以用树状数组解决了,
但是现在又多了一层关系,计数位置i要统计前缀中长度最大的递增数量,
首先把序列连同下标记录排序吧,按数值排序,遍历时就可以更新下标位置,
已更新的都是比当前值小的,这道题架子跟求长度差不多,
但现在思考一个问题,如果更新点的后面有一个已经更新过的数值答案,
如何保证以后再维护答案时让这个已更新过的不影响,想想以前求前缀和,这种情况可以考虑成
答案贡献的增益,但现在准确来说是答案中要产生竞争,
所以要封装下树状数组中的加法。
如果长度不一样,只能有一个答案活下来,长度大的优先,
长度一样的答案产生累加,把这个逻辑丢到树状数组中。
(虽然具体证明我好像也说不清楚,,这种运算在树状数组中为什么
可以完美的刷新出答案我也没想法。。。)
*/
///结构体
struct node
{
    int cnt,len;
    node(){}
    node(int x,int y):cnt(x),len(y){}
    node operator+(const node& y) const
    {
        if(len<y.len) return y;
        if(len>y.len) return (*this);
        return node((y.cnt+cnt)%mod,len);
    }
};
struct d
{
    int num,pos;
};
bool cmp(d &x,d &y)
{
    if(x.num==y.num) return x.pos>y.pos;
    return x.num<y.num;
}
 d dat[maxn];
 node tree[maxn<<1];///结构体树状数组
 int lowbit(int x)
 {
     return x&-x;
 }
 node sum(int x)
 {
     node ret(0,0);
     for(;x>0;ret=ret+tree[x],x-=lowbit(x));
     return ret;
 }
 void add(int x,node y)
 {
     for(;x<maxn;tree[x]=tree[x]+y,x+=lowbit(x));
 }
int n;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int x;scanf("%d",&dat[i].num);
        dat[i].pos=i+1;
    }
    sort(dat,dat+n,cmp);
    node ans(0,0);
    for(int i=0;i<n;i++)
    {
        node t=sum(dat[i].pos);
        if(!t.len) t.cnt=1;
        t.len++;
        ans=ans+t;
        add(dat[i].pos,t);
    }
    printf("%d\n",ans.cnt);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值