HPU第三次积分赛-D:Longest Increasing Subsequence(DP)
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;
}
HPU第三次积分赛-D:Longest Increasing Subsequence(DP)的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 300最长上升子序列 · Longest Increasing Subsequence
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
随机推荐
- China’s movie heroes 《红海行动》展现中国英雄本色
Recent years have seen a trend for big military movies. Whether it was last year’s British hit Dunki ...
- css 中的grid布局基础
CSS Grid Layout为CSS引入了一个二维网格系统.网格可用于布局主要页面区域或小型用户界面元素. 网格是一组交叉的水平和垂直线 - 一组定义列,其他行.元素可以放在网格上,以行或者列为标准 ...
- day11 第一类对象 闭包 迭代器
今日主要内容: 1 . 第一类对象 -->函数名--> 变量名 2. 闭包 -->函数的嵌套 3. 迭代器 --> 固定的思想 for 循环 第一类对象 : 函数对象介意向变 ...
- PropertiesUtil 获取文件属性值
有时候不要把一些属性值写死在代码中,而是写在配置在文件中,方便更改 PropertiesUtil工具类:读取key-value形式的配置文件,根据key获得value值 1.测试类 public c ...
- Ubuntu server 网络配置中遇到的问题
Ubuntu server 网络配置中遇到的问题 图片中ip地址有可能和文字不符,请不要在意太多,知道原理即可 - 1.首先就是要配置ip地址 vim /etc/network/interfaces ...
- 6.2 C++ string类型字符串的连接
参考:http://www.weixueyuan.net/view/6391.html 总结: 对于string类型变量,我们可以直接用“+”或者“+=”进行字符串的连接,操作符非常方便. 用“+”风 ...
- python笔记4-if..elif-else条件语句
python中条件判断使用if else来判断,多分支的话使用if elif ... else,也就是如果怎么怎么样就怎么怎么样,否则就怎么怎么这样,格式如下: #if是布尔类型判断,1个是真1个是假 ...
- MPX
Pre-processing: MPX使用的滤波器是自适应性的‘Wiener’ filter.有时甚至不用MPX,因为尽管Wiener 所引起的扰乱是可以忽略的,有时发现滤波并不能很好地提高拾取的精度 ...
- C++中数组定义及初始化
一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...
- shell脚本实例-脚本批量创建用户
#!/usr/bin/bash read -p "Please input number: " num if [[ ! "$num" =~ ^[0-9]+$ | ...