【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences
求最长上升子序列方案数。
转载自: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示例
5
1
3
2
0
4
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的更多相关文章
- 【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 ...
- SnackDown Longest Increasing Subsequences 构造题
Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- 利用SSLStrip截获https协议--抓取邮箱等密码
1.SSL解析 SSL 是 Secure Socket Layer 的简称, 中文意思是安全套接字层,由 NetScape公司所开发,用以保障在 Internet 上数据传输的安全,确保数据在网络的传 ...
- python 面试题4
Python面试题 基础篇 分类: Python2014-08-08 13:15 2071人阅读 评论(0) 收藏 举报 最近,整理了一些python常见的面试题目,语言是一种工具,但是多角度的了解工 ...
- imperva-代理安装
首先创建网关上面的监听端口
- 【技巧总结】Penetration Test Engineer[3]-Web-Security(SQL注入、XXS、代码注入、命令执行、变量覆盖、XSS)
3.Web安全基础 3.1.HTTP协议 1)TCP/IP协议-HTTP 应用层:HTTP.FTP.TELNET.DNS.POP3 传输层:TCP.UDP 网络层:IP.ICMP.ARP 2)常用方法 ...
- [NOI2014]购票 「树上斜率优化」
首先易得方程,且经过变换有 $$\begin{aligned} f_i &= \min\limits_{dist_i - lim_i \le dist_j} \{f_j + (dist_i - ...
- iOS 里const在修饰对象时候的用法
玩iOS的小伙伴对const应该很不陌生, 在声明全局常量的时候很多时候都会用到, 但是有时候修饰对象很迷惑下面是个人总结, 下面的地址都是模拟的 1. const NSString *str1 = ...
- 【本地服务器】用nodejs搭建最简单、轻量化的http server
1. 引言 前端程序猿主要关注的是页面,你可能根本就用不到.net,java,php等后台语言. 但是你制作出来的网页总要运行.总要测试吧?——那就免不了用到http server.我先前都是用vis ...
- ls和cd命令详解
ls命令 命令功能: 列出当前目录下或者指定目录下的所有文件和目录,ls是list的缩写. 命令语法: ls [选项][目录名 ] #注:[ ]中的内容为非必选项 命令选项: | 选项 | 含义 ...
- android studio 解决avd启动问题 ----waiting for target device come online
android studio 模拟器打不开,一直停留在第三方.waiting for target device come online 问题解决方法 方法1.Android Emulator 未 ...
- No.1 selenium学习之路之浏览器操作
selenium基础,首先就是浏览器的相关操作 下面描述几种浏览器的常用操作 1.打开浏览器 webdriver后面添加想要打开的浏览器 Ie或者Chrome 2.打开指定页面(百度) 3.休眠时间 ...