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 ...
随机推荐
- 在eclipse激活maven profile配置
profile简介 profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同 ...
- Java Code Examples for org.codehaus.jackson.map.DeserializationConfig 配置
The following code examples are extracted from open source projects. You can click to vote up the e ...
- 字符串和数组----vector
vector能容纳绝大多数类型的对象作为其元素,但是因为引用不是对象,所以不存在包含引用的vector. 使用vector需要包含头文件vector. 1.初始化vector对象的方法 1)vecto ...
- JavaBean理解
Java语言欠缺属性.事件.多重继承功能.所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码.Java Bean正是编写这套胶水代码的惯用模式或约定.这些约定包括getX ...
- learning at command AT+CSUB
[Purpose] Learning how to get mobile module info [Eevironment] Shell terminal, base on gcom command ...
- 容错机制和熔断(Hystrix)
雪崩效应 在微服务架构中,由于服务众多,通常会涉及多个服务层级的调用,而一旦基础服务发生故障,很可能会导致级联故障,进而造成整个系统不可用,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者 ...
- [HDU2475]Box
Problem 先告诉你每个盒子在哪个盒子的内部 接下来有M个操作: 可以把一个盒子及里面的盒子移到另外一个盒子的内部 或者询问你某个盒子最外面的盒子是哪个 Solution 首先可以建成一个图,然后 ...
- DevExpress WPF v18.2新版亮点(一)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- BBbacktrace installation
1: get the installation package https://backtrackbb.readthedocs.io/en/latest/Method.html#overview ht ...
- Problem B: 重载函数:max
Description 编写两个名为max的函数,它们是重载函数 ,用于求两个整数或实数的最大值.它们的原型分别是: int max(int a,int b); double max(double a ...