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

转载自: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. UNIX网络编程 第6章 I/O复用:select和poll函数

    UNIX网络编程 第6章 I/O复用:select和poll函数

  3. USB设备被识别流程【转】

    转自:http://blog.csdn.net/myarrow/article/details/8286876 USB模块包括usb core,host,hub,device驱动,其中hub会启动一个 ...

  4. Tomcat实现多域名之间session共享

    最近启用二级域名后,面临一个主域名与二级域名之间 session 不能共享的问题,带来的麻烦就是用户在主域名登陆,但由于二级域名 session 不能共享 ,因此无法进行登陆的操作,对一些功能有一些影 ...

  5. Android页面之间进行数据回传

    要求:页面1跳转到页面2,页面2再返回页面1同时返回数据 页面1添加如下代码: Intent intent = new Intent(); intent.setClass(页面1.this, 页面2. ...

  6. java基础74 XML解析中的SAX解析相关知识点(网页知识)

    1.SAX解析工具 SAX解析工具:是Sun公司提供的,内置JDK中.org.xml.sax.*         点击查看: DOM解析相关知识:以及DOM和SAX解析的原理(区别) 2.SAX解析的 ...

  7. python_selenium自动化测试框架

    设计思路 本文整理归纳以往的工作中用到的东西,现汇总成基础测试框架提供分享. 框架采用python3 + selenium3 + PO + yaml + ddt + unittest等技术编写成基础测 ...

  8. 入门ROS教程与视频汇总(kinetic)

    参考网址: Richard Wang    3 Shawn Chen 部分视频网址: http://v.youku.com/v_show/id_XMjUxMTc5MzE5Mg http://i.you ...

  9. CentOS7.5搭建ELK6.2.4集群及插件安装

    一 简介 Elasticsearch是一个高度可扩展的开源全文搜索和分析引擎.它允许您快速,近实时地存储,搜索和分析大量数据.它通常用作支持具有复杂搜索功能和需求的应用程序的底层引擎/技术. 下载地址 ...

  10. php判断是否是ajax提交 方法

    /** * 判断是否是AJAX提交 * @return bool */ function is_ajax() { if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ...