[树状数组]LightOJ 1085 - All Possible Increasing Subsequences

本文探讨了如何通过动态规划(DP)算法找到一个序列中所有可能的上升子序列,并详细解释了算法实现过程,包括状态空间构造、转移方程以及优化方法。通过实例输入输出展示了算法的具体应用。
1085 - All Possible Increasing Subsequences
Time Limit: 3 second(s)Memory Limit: 64 MB

An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold

1.      i1 < i2 < i3 < ... < ik and

2.      Ai1 < Ai2 < Ai3 < ... < Aik

Now you are given a sequence, you have to find the number of all possible increasing subsequences.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.

Output

For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.

Sample Input

Output for Sample Input

3

3

1 1 2

5

1 2 1000 1000 1001

3

1 10 11

Case 1: 5

Case 2: 23

Case 3: 7

Notes

1.      For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.

2.      Dataset is huge, use faster I/O methods.

 

题目大意:找出一个序列中的所有上升序列,很容易想到DP,直接给代码,注释里有说明:

/**
**  构造n的状态空间, dp[i]表示以i结尾的所有上升序列的个数
**  转移为 dp[i] = 1+segma(dp[k])| 0<=k<i && arr[k]<arr[i]
**  不用树状数组优化的话转移是O(n)的
**  用树状数组优化以后转移就是O(logn)的,需要离散化
**/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN = 100010;
int c[MAXN],arr[MAXN],pt[MAXN],t,n,m;
LL ans,MOD = 1000000007ll,dp[MAXN];
void init(){
    memset(c,0,sizeof(c));
    ans = 0;
}
int lowbit(int x){
    return x&(-x);
}
int sum(int pos){
    int s = 0;
    while(pos>0){
        s = (s+c[pos])%MOD;//不取模的话会溢出
        pos-=lowbit(pos);
    }
    return s;
}
void add(int pos,int val,int n){
    while(pos<=n){
        c[pos] = (c[pos]+val)%MOD;//不取模的话会溢出
        pos+=lowbit(pos);
    }
}
//离散化
void disc(){
    memcpy(pt,arr,sizeof(arr));
    sort(pt,pt+n);
    int i;
    for(i=1,m=1;i<n;i++){
        if(pt[i]!=pt[i-1])pt[m++]=pt[i];
    }
}
int search(int k){
    int l=0,r=m-1,m;
    while(l<=r){
        m = (l+r)>>1;
        if(pt[m]==k)return m;
        else if(k<pt[m])r=m-1;
        else l=m+1;
    }
    return -1;
}
int main(){
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%d",&arr[i]);
        disc();
        dp[0] = 1;ans = 1;
        add(search(arr[0])+1,dp[0],m);
        for(int i=1;i<n;i++){
            int pos = search(arr[i])+1;
            dp[i] = (sum(pos-1)+1)%MOD;
            ans = (ans+dp[i])%MOD;
            add(pos,dp[i],m);
        }
        printf("Case %d: %lld\n",cas,ans);
    }
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值