码迷,mamicode.com
首页 > 编程语言 > 详细

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

时间:2018-01-25 00:12:05      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:down   code   source   ber   second   else   space   include   ==   

题意

给定$n$个数,求最长上升子序列的方案数


根据数据范围要求是$O(n\log n)$

朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数

从通酱博客里还看到更详尽的解释:stackoverflow

时间复杂度$O(n\log n)$

代码

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,m,readin;
pii f[100005];
pii query(int x) {
    pii ret=make_pair(0,1);
    while(x) {
        if(f[x].first>ret.first)ret=f[x];
        else if(f[x].first==ret.first)ret.second=(ret.second+f[x].second)%m;
        x-=x&(-x);
    }
    return ret;
}
void add(int x,pii v) {
    while(x<=n) {
        if(f[x].first<v.first)f[x]=v;
        else if(f[x].first==v.first)f[x].second=(f[x].second+v.second)%m;
        x+=x&(-x);
    }
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i) {
        scanf("%d",&readin);
        pii t=query(readin);
        t.first++;
        add(readin,t);
    }
    printf("%d\n",query(n).second);
    return 0;
}

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

标签:down   code   source   ber   second   else   space   include   ==   

原文地址:https://www.cnblogs.com/ogiso-setsuna/p/8343885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!