LightOJ 1085

All Possible Increasing Subsequences

Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu


Description

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

3
3
1 1 2
5
1 2 1000 1000 1001
3
1 10 11

Sample Output

Case 1: 5
Case 2: 23
Case 3: 7


题目大意:给出一个序列,找出所有的递增序的数量,可包括其本身
题目分析:递增的序列即i<j&&a[i]<a[j],假设dp[i]中储存的是以a[i]结尾的所有序列和,dp[i]=sum(dp[j])+1(j<i&&a[i]<a[j]),在这里可以用到树状数组来储存每个节点的值即为以它为结尾的序列和。
我们把初始值存入a[i],dis[i]用来储存它的序列号,按照从小到大的顺序排列dis,当两个值相等时,将序列号较大的放在前面,这样我们按照dis中的序列号顺序来更新tree中每个节点的值(即此时tree中的到这个点的sum),这样相等的情况并不会对求和造成影响,最后更新完之后,sum(n)即为我们所求的值。

ps:初次接触到树状数组,被这个搞晕了好长时间,现在也许有那么一丢丢理解了吧,再刷一下其他的题目也许会好一点哈哈哈= ̄ω ̄=


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define mod  1000000007

using namespace std;
typedef long long ll;
const int MAX=1e5+100;
int tree[MAX],dis[MAX],a[MAX],n;

int lowbit(int x)
{
    return x&(-x);
}
void add(int pos,int x)
{
    while(pos<=n)
    {
        tree[pos]=(tree[pos]+x)%mod;
        pos+=lowbit(pos);
    }
}
int sum(int pos)
{
    int ans=0;
    while(pos>0)
    {
        ans=(ans+tree[pos])%mod;
        pos-=lowbit(pos);
    }
    return ans;
}
bool cmp(int x,int y)
{
    if(a[x]!=a[y]) return a[x]<a[y];
    return x>y;
}
int main()
{
    int t,cas=0,i;
    cin>>t;
    while(t--)
    {
        SI(n);
        for(i=1;i<=n;i++) SI(a[i]),dis[i]=i;
        sort(dis+1,dis+1+n,cmp);
        mem(tree,0);
        for(i=1;i<=n;i++)
            add(dis[i],sum(dis[i])+1);
        printf("Case %d: %d\n", ++cas,sum(n));
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值