HDU - 2227
Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
Sample Input
Sample Output
题意:给予一个序列求解所构成的不下降子序列的个数
通过离散化,将0->2^32分散到0->1e5之中
到达j处的不下降子序列的个数dp[j] = sum{dp[i]} + 1 {i < j && S[i] <= S[j] && i > 0};
状态方程推导:
假设dp[j]为S[j]为结尾的序列的个数
那么if(S[j] < S[i])那么 dp[i] += dp[j] 为什么呢,举个简单的例子 如果以S[j]为结尾的序列是一下: 最原始的序列:S = { 1, 5, 3, 6, 7, 5, 2, 1, 8, 9}; 如果j = 4的话 那么以S[j]为结尾的不下降子序列如下: 6 1 6 5 6 3 6 1 3 6 1 5 6 那么如果我们取i = 5时 S[i] >= S[j]可以得到 6 7 1 6 7 5 6 7 3 6 7 1 3 6 7 1 5 6 7 就是dp[i] += dp[j]如此将所有S[j] <= S[i]都加起来 然后加上本身7加上那么状态转移方程极为dp[i] = sum{dp[j]} + 1
如此计算sum{dp[j],i < j && S[i] <= S[j] && i > 0}便是我们的难题了
而求和问题解决的话,可以基本能分为三类:
一:树状数组
二:线段树
三:前缀和
此处则是用了树状数组求解满足条件的数据和
|
HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
最新推荐文章于 2020-02-25 18:47:49 发布