求最长上升子序列方案数。

转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我。

数组A包含N个整数(可能包含相同的值)。设S为A的子序列且S中的元素是递增的,则S为A的递增子序列。如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS)。A的LIS可能有很多个。例如A为:{1 3 2 0 4},1 3 4,1 2 4均为A的LIS。给出数组A,求A的LIS有多少个。由于数量很大,输出Mod 1000000007的结果即可。相同的数字在不同的位置,算作不同的,例如 {1 1 2} 答案为2。 
Input 
第1行:1个数N,表示数组的长度。(1 <= N <= 50000) 
第2 - N + 1行:每行1个数A[i],表示数组的元素(0 <= A[i] <= 10^9) 
Output 
输出最长递增子序列的数量Mod 1000000007。 
Input示例 






Output示例 
2

必须用nlogn算法,否则超时,那么我们如何计算LIS的个数呢?

先开始我想到的是o(n^2)的做法,很容易理解

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M = 500000+100; int a[M];
int c[M];
int dp[M];
long long cent[M]; int INF = 1e9 + 1000;
const int mod =1000000007; int input()
{
int ans=0;
char a;
while((a=getchar())<'0'||a>'9');
ans=a-'0';
while((a=getchar())>='0'&&a<='9')
{
ans=ans*10+a-'0';
}
return ans;
} int main()
{
int n;
#ifdef xxz
freopen("in.txt","r",stdin);
#endif // xxz while(~scanf("%d",&n))
{
for(int i = 0; i < n; i++) a[i] = input() , cent[i] = 1;
int Max = 0; fill(dp,dp+n,0);
long long ans = 0;
for(int i = 0; i < n; i++)
{
dp[i] = 1;
for(int j = 0; j < i; j++)
{
if(a[j] < a[i])
{
if(dp[i] < dp[j] + 1)
{
dp[i] = dp[j] + 1;
cent[i] = cent[j];
}
else if(dp[i] == dp[j] + 1) cent[i] = (cent[i] +cent[j])%mod;
}
} Max = max(Max,dp[i]); } for(int i = 0; i < n; i++)
{
if(dp[i] == Max) ans = (ans + cent[i]) % mod;
} printf("%d\n",ans%mod);
} return 0;
}

然后从网上搜nlogn的算法没搜到,然后问了好多大神,九爷,鸟神,rabbit,都说用线段树或者树状数组搞,好吧,没搞出来。

然后问tyh,他搜到了一篇国外高手写的思路,看完以后直接转换为代码 
二分+前缀和,orz….膜拜田博士…….. 
果然搜索姿势要正确呀 
思路地址: 
http://stackoverflow.com/questions/22923646/number-of-all-longest-increasing-subsequences

我用中文解释下: 
就是取二元组(i,j),i表示以i元素结尾的序列,j表示方案数 
比如: 
add 1 
len1: (1,1);

add 2:

len1(1,1); 
len2(2,1);

add 5 
len1 (1,1); 
len2 (2,1); 
len3 (5,1);

add 4 
len1 (1,1); 
len2 (2,1); 
len3 (5,1) (4,1); 
……

我们可以找到规律,就是没一行j都是从达到小减少 
新插入一个数,我们先找它应该处于哪一行,用 
就是用LIS的nlogn算法找,它的方案数就等于它上一行比这个数小的所有方案和


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std; typedef long long LL;
const int MOD = 1e9 + 7;
const int INF = 0x7fffffff;
const int N = 50000 + 10; vector <int> val[N]; // val[i]: 最大长度为i+1的序列的最后一个元素组成的序列
vector <int> sum[N]; // sum[i]: 对应val中每个序列数量的组成的前缀和。
vector <int> last(N, INF); // last[i]: val[i].back() int input()
{
int ans=0;
char a;
while((a=getchar())<'0'||a>'9');
ans=a-'0';
while((a=getchar())>='0'&&a<='9')
{
ans=ans*10+a-'0';
}
return ans;
} void add(int x, int len, int v)
{
val[len].push_back(x);
if(sum[len].size() == 0)
{
sum[len].push_back(v);
}
else
{
sum[len].push_back((sum[len].back() + v) % MOD);
}
last[len] = x;
} int main()
{ int n, x;
while (scanf("%d", &n) != EOF)
{
int Max = 0;
for(int i = 0; i < n; i++)
{
x = input();
int len = lower_bound(last.begin(), last.end(), x) - last.begin();
Max = max(Max, len);
if(len == 0)
{
add(x, len, 1);
}
else
{
int pos = upper_bound(val[len - 1].begin(), val[len - 1].end(), x,greater<int>() ) - val[len - 1].begin();
int cnt;
if(pos == 0)
{
cnt = sum[len - 1].back();
}
else
{
cnt = (sum[len - 1].back() - sum[len - 1][pos - 1] + MOD) % MOD;
}
add(x, len, cnt);
}
}
printf("%d\n", sum[Max].back());
} return 0;
}

【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences的更多相关文章

  1. 【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

    题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i ...

  2. SnackDown Longest Increasing Subsequences 构造题

    Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...

  3. Longest Increasing Subsequences(最长递增子序列)的两种DP实现

    一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn).     二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...

  4. 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  7. 【LeetCode】673. Number of Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...

  8. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  9. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

随机推荐

  1. [Openwrt 扩展下篇] Openwrt搭建私有云Owncloud 9

    网上很多资料讲用Linux打造owncloud构建私有云 ,花了些时间研究了下,我将之前的需求打造成了Openwrt下的Owncloud 9.其实网上还有Seafile.大家对比来看下知乎的评论,其实 ...

  2. 关于onConfigurationChanged

    这两天测试app时,发现一个奇怪问题,app在启动后,Activity会onCreate两次,起初以为是横屏设置导致,随即在Activity中添加了android:configChanges=&quo ...

  3. nanosleep()

    函数原型 #include <time.h> int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);   描述 ...

  4. CEC2017 benchmark function调用接口

    CEC2017 benchmark function可以从这里下载. 导师最近给了个课题让我自己研究,跟智能优化算法相关的,必不可免的要用到最新的CEC2017 benchmark function, ...

  5. js数据绑定(模板引擎原理)

    <div> <ul id="list"> <li>11111111111</li> <li>22222222222< ...

  6. [扩展推荐] Laravel 中利用 GeoIP 获取用户地理位置信息

    我最近需要一个用户地域检测来设置用户的默认区域和货币.由 Daniel Stainback 创建的 torann/geoip  很好地满足为Laravel 5 项目提供 GeoIP 服务的要求. 这个 ...

  7. HDU 2819 Swap(行列式性质+最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...

  8. Jenkins在Linux环境安装

    Jenkins介绍 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 安装环境 操作系统:lin ...

  9. 函数fgets和fputs、fread和fwrite用法小结(转)

    字符串读写函数fgets和fputs: 1.fgets()函数:原型char *fgets(char *s, int n, FILE *stream);从流中读取n-1(n默认1024)个字符之前,如 ...

  10. 最邻近规则分类KNN算法

    例子: 求未知电影属于什么类型: 算法介绍: 步骤:  为了判断未知实例的类别,以所有已知类别的实例作为参照      选择参数K      计算未知实例与所有已知实例的距离      选择最近K个已 ...