Longest Increasing Subsequence

描述

给出一组长度为n的序列,a1​,a2​,a3​,a4​...an​, 求出这个序列长度为k的严格递增子序列的个数

输入

第一行输入T组数据 T(0≤T≤10)
第二行输入序列大小n(1≤n≤100),长度k(1≤k≤n)
第三行输入n个数字ai​(0≤ai​≤1e9)

输出

数据规模很大, 答案请对1e9+7取模

输入样例 1 

2
3 2
1 2 2
3 2
1 2 3

输出样例 1

2
3

思路

用dp[i][j]数组记录在i位置,严格递增子序列长度为j的子序列的个数。

状态转移方程 :

在每个i位置j长度的时候遍历前i的位置(不包括第i的位置),去寻找小于a[i]的数字,方案数变成当前i位置长度为j的个数+k位置长度为j-1的方案数

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e3+10;
const int mod=1e9+7;
using namespace std;
int dp[maxn][maxn];//表示到第i个位置的递增子序列长度为j的个数
int a[maxn];
int main(int argc, char const *argv[])
{ios::sync_with_stdio(false);int t;int n,k;cin>>t;while(t--){ms(dp);cin>>n>>k;for(int i=1;i<=n;i++){cin>>a[i];dp[i][1]=1;}for(int i=1;i<=n;i++){for(int j=2;j<=i;j++){for(int k=1;k<i;k++)// 如果a[i]>a[k],那么dp[i][j]的值加上在k位置的时候长度为j-1的值并取模if(a[i]>a[k])dp[i][j]=(dp[i][j]%mod+dp[k][j-1]%mod)%mod;}}int ans=0;for(int i=1;i<=n;i++)ans=(ans+dp[i][k])%mod;cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/Friends-A/p/10324396.html

HPU第三次积分赛-D:Longest Increasing Subsequence(DP)相关推荐

  1. Rosalind Java|Longest Increasing Subsequence动态规划算法

    Rosalind编程问题之计算集合中最长的递增元素子集. Longest Increasing Subsequence Problem: A subsequence of a permutation ...

  2. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  3. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  4. C++longest increasing subsequence 最长递增子序列的实现之二(附完整源码)

    C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...

  5. C++longest increasing subsequence 最长递增子序列的实现之一(附完整源码)

    C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...

  6. leetcode 300. Longest Increasing Subsequence | 300. 最长递增子序列(动态规划)

    题目 https://leetcode.com/problems/longest-increasing-subsequence/ 题解 难得有官方题解的一道题. 参考:https://leetcode ...

  7. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

  8. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  9. Dynamic Programming之Longest Increasing Subsequence (LIS)问题

    Longest Increasing Subsequence(LIS)问题是一类常见的可使用Dynamic Programming解决的算法问题.这个问题是指在一个数字序列中,找到最大个数升序排列的子 ...

最新文章

  1. java并发编程——并发容器类介绍
  2. 生产环境一次诡异的NPE问题,反转了4次
  3. linux ping程序设计与实现,一步步学Linux网络编程--ping命令的实现分析
  4. 基于人工智能智商研究的智能定律初探
  5. mysql存储过程写法—动态参数运用
  6. MOQ TIP1:简介加基础
  7. python unittest断言大全_Python-unittest框架 断言使用
  8. 渴望尽快能找到工作,开始上班
  9. kali查看共享 linux_在Linux下访问Windows共享文件夹
  10. 信息学奥赛一本通(1407:笨小猴)
  11. 找不到该项目,请确认该项目的位置的解决办法
  12. 电子计算机之争的想法,模拟意识的通俗讲解
  13. 谷歌推出 GKE 开源依赖关系漏洞奖励计划
  14. activiti工作流 php,码云社 | 砺锋科技-SpringBoot整合Activiti工作流(附源码) - 用代码改变世界...
  15. Avast高级版的cleanup激活码
  16. php laravel实战项目,Laravel框架应用:7个实战项目
  17. Boost基础篇——安装
  18. 中国供应链金融行业全景调研与发展战略研究规划报告2022-2028年版
  19. 极限编程XP 的12个最佳实践
  20. LeetCode算法之蓄水池算法

热门文章

  1. 03.LoT.UI 前后台通用框架分解系列之——多样的表格
  2. LSASRV事件ID:40960
  3. Oracle truncate、 delete、 drop区别
  4. 易企cms文章引用地址写法
  5. SP 短信开发-基础知识篇
  6. appcan 上/下拉刷新函数的一个简单封装
  7. [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.21
  8. 一个功能齐全的DataGrid分页例子
  9. 技术人员如何转型为产品经理
  10. 检索数据表中重复的记录