最长递增子序列问题---动态规划

对于最长递增子序列

递推方程的意思是,在求以ai为末元素的最长递增子序列时,找到所有序号在L前面且小于ai的元素aj,即j<i且aj<ai。如果这样的元素存在,那么对所有aj,都有一个以aj为末元素的最长递增子序列的长度f(j),把其中最大的f(j)选出来,那么f(i)就等于最大的f(j)加上1,即以ai为末元素的最长递增子序列,等于以使f(j)最大的那个aj为末元素的递增子序列最末再加上ai;如果这样的元素不存在,那么ai自身构成一个长度为1的以ai为末元素的递增子序列。

也就是

for (int i=1; i<=n; i++)
        {
            for (int j=0; j<=i; j++)
            {
                if (a[i]>a[j])
                    dp[i]=max(dp[i],dp[j]+a[i]);
            }
        }

 

这样求得的dp[i]的值是每个出现的数前面的最大递增子序列值的和。

只需最后再进行比较就能求得最大值了。

 

以前在牛客网上就遇到过这样的问题:

 

链接:https://ac.nowcoder.com/acm/contest/321/I
来源:牛客网
 

「深い深い森の中,ほのか香る,爱しい

日々の面影,探してみれば,ふいにあなたが笑う

触れて见たくて,手を伸ばしても,儚く宙を舞ったのです.」

_____________________

「啊啊,你选了一条艰辛的道路呢,不过奇迹是需要代价的,亚瑟王啊。你将要以你最重要的事物去交换」

「我问你。你是我的master吗?」

「我要去达成身为servant的责任。想传达之事,就留待之后吧」

「——总算注意到了,原来士郎 ,就是我的剑鞘呢。」

「对不起————士郞……」

「…」

多年以后,Emiya游历了Altria生前待过的地区.

一天,他到了Altria获得湖之精灵祝福,并拿到湖中剑[Excalibur]的地方

湖边有很多不同的花,据说是Altria 的导师Merlin当时种下的.

_______________________

Emiya面前的nn朵花按直线排列,一共m种花朵,花的种类用1,2,3…m-1,m的数字进行编号..

Emiya想知道的是,最少连续多少朵花,可以包含前1~k种类花朵每种至少一次.

 

输入描述:

 

第一行包含一个整数T,表示测试的组数, 1≤T≤10

接下来是T组数据:

每组的第一行包含3个整数n,m,k 其中1 <= n,m <= 105,1 <= k <= m

接下来一行包含n个整数,表示按直线排列的n朵花.

输出描述:

每一组数据输出一行,

包含一个整数包含,即为所求的答案.,如果不存在答案 则输出-1

示例1

输入

 

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

输出

 

3
-1
4

定义变量x=0,如果说最后x==k那么输出ans,反之,输出的是-1;

就像上面给出的解释,现在从第一个数开始判断,定义变量r,r<n是一个大循环

用数组q[i],记录这个数出现的次数,数组a[i]存放输入的数字。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const int maxx=1e9;
int a[maxn];
int q[maxn];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        memset(q,0,sizeof(q));
        int ans=maxx;
        int x=0;
        int l=0,r=0;
        while(r<n)
        {
            if(!q[a[r]]&&a[r]<=k)x++;
            q[a[r]]++;
            while(true)
            {
                if(a[l]>k)l++;
                else if(q[a[l]]>1&&a[l]<=k)q[a[l]]--,l++;
                else break;
            }
            if(x>=k){
                ans=min(ans,r-l+1);
            }
            r++;
        }
        if(x<k)ans=-1;
        printf("%d\n",ans);
    }
}

 

 

不过,这样写是不是有点不好想?

下面再给一个例子。

Super Jumping! Jumping! Jumping!

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

 

 

这个就不解释了,直接给出代码!

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
int a[1005];
int dp[1005];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));

        for(int i=1; i<=n; ++i)
            scanf("%d",&a[i]);
        for (int i=1; i<=n; i++)
        {
            for (int j=0; j<=i; j++)
            {
                if (a[i]>a[j])
                    dp[i]=max(dp[i],dp[j]+a[i]);
            }
        }
        int maxx=dp[1];
        for(int i=2;i<=n;++i)
        {
            maxx=max(dp[i],maxx);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值