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

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.


PROBLEM SETTER: JANE ALAM JAN

题目大意:给你n个数,问你其中的单调上升子区间的个数
可以使用dp的想法
我们假设dp[i]代表:以数字i结尾的单调上升子区间的个数
那么结尾数字为i的单调上升子区间就是所有结尾数字小于i的单调上升子区间后面加上一个i,然后就是一个i也可以单独成为一个单调上升子区间
那么dp[i] = dp[1]+dp[2]+...+dp[i-1]+1
注意上式有前缀和
因此我们可以先把数组进行离散化,然后用树状数组进行维护

代码如下:
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5+100;
const int MOD = 1000000007;
int bit[MAX_N],n,T,a[MAX_N],b[MAX_N];

void add(int i,int x){
    while(i<=n){
        bit[i] += x;
        while(bit[i]>MOD) bit[i] -= MOD;
        i+=i&-i;
    }
}

int sum(int i){
    int res = 0;
    while(i>0){
        res += bit[i];
        i -= i&-i;
        while(res>MOD) res -= MOD;
    }
    return res;
}

int main(){
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        memset(bit,0,sizeof(bit));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        
        // 以下代码为离散化操作
        sort(a,a+n);
        int N = unique(a,a+n)-a;
        for(int i=0;i<n;i++){
            b[i] = lower_bound(a,a+N,b[i])-a;
            b[i]+=1;
        }
        
        // 树状数组维护值
        for(int i=0;i<n;i++){
            int s = sum(b[i]-1);
            s++;
            add(b[i],s);
        }
        int ans = sum(n);
        printf("Case %d: %d\n",t,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值