题意:给你一个数列,问你有几个递增子序列(单个数字也算一个)
a1, a2, a3....an以ai结尾的递增子序列数量为在此之前出现的并且ak(k<i&&ak<ai)比他小的ak结尾的递增子序列数量之和+1(它本身)
这样就可以用树状数组做了,不断插入并且计算在这之前比他小的子序列和.
因为ai可能很大,所以此题需要离散化一下。
还有别忘了取模。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int mod = 1e9+7;
int tree[maxn], b[maxn];
struct node
{
int index, val;
bool operator < (const node &a) const
{
return val < a.val;
}
}a[maxn];
int lowbit(int x) { return x&(-x); }
void update(int pos, int val)
{
while(pos < maxn)
{
tree[pos] = (tree[pos]+val)%mod;
pos += lowbit(pos);
}
}
int query(int pos)
{
int sum = 0;
while(pos)
{
sum = (sum+tree[pos])%mod;
pos -= lowbit(pos);
}
return sum;
}
int main(void)
{
int n, t, ca = 1;
cin >> t;
while(t--)
{
memset(tree, 0, sizeof(tree));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i].val), a[i].index = i;
sort(a+1, a+1+n);
for(int i = 1; i <= n; i++)
{
b[a[i].index] = i;
if(a[i].val == a[i-1].val)
b[a[i].index] = b[a[i-1].index];
}
int temp, sum = 0;
for(int i = 1; i <= n; i++)
{
temp = (query(b[i]-1)+1)%mod;
sum = (sum+temp)%mod;
update(b[i], temp);
}
printf("Case %d: %d\n", ca++, sum);
}
return 0;
}
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.