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 

  1. 2
  2. 3 2
  3. 1 2 2
  4. 3 2
  5. 1 2 3

输出样例 1

  1. 2
  2. 3

思路

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

状态转移方程 :

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

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #include <set>
  12. #include <string>
  13. #define ll long long
  14. #define ms(a) memset(a,0,sizeof(a))
  15. #define pi acos(-1.0)
  16. #define INF 0x7f7f7f7f
  17. const double E=exp(1);
  18. const int maxn=1e3+10;
  19. const int mod=1e9+7;
  20. using namespace std;
  21. int dp[maxn][maxn];//表示到第i个位置的递增子序列长度为j的个数
  22. int a[maxn];
  23. int main(int argc, char const *argv[])
  24. {
  25. ios::sync_with_stdio(false);
  26. int t;
  27. int n,k;
  28. cin>>t;
  29. while(t--)
  30. {
  31. ms(dp);
  32. cin>>n>>k;
  33. for(int i=1;i<=n;i++)
  34. {
  35. cin>>a[i];
  36. dp[i][1]=1;
  37. }
  38. for(int i=1;i<=n;i++)
  39. {
  40. for(int j=2;j<=i;j++)
  41. {
  42. for(int k=1;k<i;k++)
  43. // 如果a[i]>a[k],那么dp[i][j]的值加上在k位置的时候长度为j-1的值并取模
  44. if(a[i]>a[k])
  45. dp[i][j]=(dp[i][j]%mod+dp[k][j-1]%mod)%mod;
  46. }
  47. }
  48. int ans=0;
  49. for(int i=1;i<=n;i++)
  50. ans=(ans+dp[i][k])%mod;
  51. cout<<ans<<endl;
  52. }
  53. return 0;
  54. }

HPU第三次积分赛-D:Longest Increasing Subsequence(DP)的更多相关文章

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

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

  2. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  3. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

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

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

  5. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. Leetcode 300 Longest Increasing Subsequence

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

  8. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  9. The Longest Increasing Subsequence (LIS)

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

随机推荐

  1. localStorage 设置本地缓存

    var timestamp = parseInt(Date.parse(new Date()));var btn = document.getElementById("close" ...

  2. Mysql 用户ip访问根据省份查询

    表名:shop_interview_customer 表结构:customerId空为游客模式 interviedId customerId interviewIP iPdetail 1 1001 1 ...

  3. 转 /etc/ld.so.conf.d/目录下文件的作用

    在了解/etc/ld.so.conf.d/目录下文件的作用之前,先介绍下程序运行是加载动态库的几种方法:第一种,通过ldconfig命令    ldconfig命令的用途, 主要是在默认搜寻目录(/l ...

  4. react router @4 和 vue路由 详解(一)vue路由基础和使用

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...

  5. flask-admin fileadmin 上传文件,中文名的解决方案 重写部分secure_filename

    class upload_view(FileAdmin): def _save_form_files(self, directory, path, form): super() filename = ...

  6. Ubuntu16.04 安装Tensorflow-CPU

    最近我开始学习深度学习框架Tensorflow,一开始在windows平台下的anaconda下安装,由于anaconda安装几次后navigator打开老是出现闪退的问题,所以决定换个ubuntu下 ...

  7. elk之kibana

    环境: centos7 jdk8 参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.htmlhttp:// ...

  8. java 设计模式参考资料

    参考博客 http://www.cnblogs.com/lin3615/p/3783272.html 设计模式之责任链模式http://www.cnblogs.com/draem0507/p/3784 ...

  9. Node.js 回调函数 1) 阻塞 ,同步 2) 非阻塞 ,异步.

    1.阻塞. 同步. 1) 读取的文件: input.txt 菜鸟教程官网地址:www.runoob.com 2) main.js var fs = require("fs"); / ...

  10. js继承中,原型属性的继承探究

    最近研究了js的继承,看了幻天芒的文章http://www.cnblogs.com/humin/p/4556820.html#3947420,明白了最好是使用apply或call方法来实现继承. 已知 ...