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

转载自: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. 【多视图几何】TUM 课程 第3章 透视投影

    课程的 YouTube 地址为:https://www.youtube.com/playlist?list=PLTBdjV_4f-EJn6udZ34tht9EVIW7lbeo4 .视频评论区可以找到课 ...

  2. 初涉sqlmap

    1.基本注入(这个工具kali或者bt下面有集成的,这里附加一个window免py版,提取码:3ldv) sqlmap -u http://url/xx.php?id=1  判断注入 sqlmap - ...

  3. 2016.6.17——Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array 本题收获: 1.“删除”数组中元素 2.数组输出 题目: Given a sorted array, remove the du ...

  4. Count 1 in Binary

    Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, r ...

  5. mysql -> 简介&体系结构_01

    数据库简介 数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据运行新增.截取.更新.删除等操作. 所谓“数据库”系以一定方式储存在一起.能予多个用户共享.具有尽可能小的 ...

  6. MySQL -- JDBC

    一 . JDBC的开发步骤 1.准备四大参数 2.注册驱动 3.获得连接 4.获得语句执行者 5.执行sql语句 6.处理结果 7.释放资源 1.准备四大参数 /* * jdbc四大配置参数 * &g ...

  7. MongoDB安全:内置角色概览

    官文列举如下(文档不长,过英语四级者可以轻松阅读,就不需要看本文额)(基于MongoDB 3.6的文档,4.0也没有变化): Role-Based Access Control Built-In Ro ...

  8. java基础43 IO流技术(输入字节流/缓冲输入字节流)

    通过File对象可以读取文件或者文件夹的属性数据,如果要读取文件的内容数据,那么我们就要使用IO技术. 一.输入字节流 输入字节流的体系:  -------| InputStream:所有输入字节流的 ...

  9. java基础42 File类的构造函数及其方法

    本文知识点(目录): 1.IO流(Input Output)    2.IO流技术的运用场景    3.File类的构造函数(方法)    4.File类的一些常用方法    5.实例(解析File类 ...

  10. CxGrid 表格列内容居中

    首先每一列 Cxgrid 都不知道要当成什么来出来,所以每一列都有个properties 让你来设置,告诉cxgrid 这列的内容是什么,然后根据你给出的内容 再来决定用什么居中方式: 就是说 官方再 ...