HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
来源:互联网 发布:c语言二维数组例子 编辑:程序博客网 时间:2024/06/05 18:02
HDU - 2227
Find the nondecreasing subsequences
Time Limit: 5000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u
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
31 2 3
Sample Output
7
题意:给予一个序列求解所构成的不下降子序列的个数
通过离散化,将0->2^32分散到0->1e5之中
到达j处的不下降子序列的个数dp[j] = sum{dp[i]} + 1 {i < j && S[i] <= S[j] && i > 0};
到达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
那么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}便是我们的难题了
而求和问题解决的话,可以基本能分为三类:
一:树状数组
二:线段树
三:前缀和
此处则是用了树状数组求解满足条件的数据和
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long LL;const int MAXN = 1e5 + 5;const LL mod = 1e9 + 7;int N, A[MAXN], S[MAXN];LL C[MAXN];int Crete(LL x) { int lb = 0,ub = N; while(ub - lb > 1) { int mid = (lb + ub) >> 1; if(A[mid] >= x) ub = mid; else lb = mid; } return ub;}int lowbit(int x) { return x & (-x);}void add(int x,LL c) { while(x <= N) { C[x] += c; C[x] %= mod; x += lowbit(x); }}int sum(int x) { LL ret = 0; while(x > 0) { ret += C[x]; ret %= mod; x -= lowbit(x); } return ret;}int main() { while(~ scanf("%d", &N)) { for(int i = 1; i <= N; i ++) { scanf("%d", &S[i]); A[i] = S[i]; } sort(A + 1, A + N + 1); LL ans = 0; memset(C, 0, sizeof(C)); for(int i = 1; i <= N; i ++) { int ret = lower_bound(A + 1, A + N + 1, S[i]) - A; //int ret = Crete(S[i]); int res = sum(ret) + 1; ans += res; ans %= mod; add(ret, res); } printf("%I64d\n", ans % mod); } return 0;}
1 0
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
- hdu 2227 Find the nondecreasing subsequences【离散化+树状数组+DP思路】
- hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
- hdu 2227Find the nondecreasing subsequences(树状数组+dp+离散化)
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- HDU 2227 Find the nondecreasing subsequences 树状数组
- hdu 2227 Find the nondecreasing subsequences 树状数组+简单DP
- hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
- hdu 2227 Find the nondecreasing subsequences(树状数组+DP)
- HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
- HDU2227 Find the nondecreasing subsequences(树状数组+DP+离散化)
- hdu-Find the nondecreasing subsequences(树状数组)
- 树状数组 hdu2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences--(树状数组)
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- HDU 2227 Find the nondecreasing subsequences(线段树+离散化+DP)
- hdu 2227 Find the nondecreasing subsequences
- JTextFiled
- Android与Java服务端加密解密
- Linux安装VMWare虚拟机(Kali)
- PCA的数学原理Matlab演示
- virtual override new之派生
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
- Java多线程系列--“JUC锁”04之 公平锁(二)
- fsi event loop
- 好的开始
- 基于Zxing的二维码、条形码的扫描以及二维码的生成(简洁高效)
- 如何取得系统时间
- 我的高效编程的秘诀--开发环境的重要性(IOS)
- H264语法分析之-slice
- NoSQL架构实践(二)——以NoSQL为主