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

题目链接: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;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/82775840
今日推荐