1085 - All Possible Increasing Subsequences[树状数组]
2014-09-01 22:23
411 查看
1085 - All Possible Increasing Subsequences
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.
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.
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
注意上式有前缀和
因此我们可以先把数组进行离散化,然后用树状数组进行维护
代码如下:
![]() | PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 64 MB |
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; }
相关文章推荐
- lightoj 1085 - All Possible Increasing Subsequences 【树状数组优化dp】
- LightOJ 1085 All Possible Increasing Subsequences (DP&离散化&树状数组)
- lightoj1085 - All Possible Increasing Subsequences(树状数组)
- [树状数组]LightOJ 1085 - All Possible Increasing Subsequences
- Light OJ 1085 - All Possible Increasing Subsequences
- LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)
- LightOJ 1085 - All Possible Increasing Subsequences(DP + 线段树 + 离散)
- Lightoj 1085 All Possible Increasing Subsequences (树状数组+DP)
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
- lightoj1085 All Possible Increasing Subsequences
- HDU 1085(暴力;找规律;树状数组;母函数)
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- lightoj 1085【离散化+树状数组】
- HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
- lightoj 1085【离散化+树状数组】
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)