线性DP之最长上升子序列模型 (LIS)

线性DP之最长上升子序列模型 (LIS)

1.导弹拦截(bumb)
【问题描述】

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统,但是这种导弹拦截系统有一 个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都要求高于前一发 的高度。某天,雷达捕捉到敌国的导弹来袭,由于该系统还在试用阶段,不能拦截所有的导 弹,输入敌国导弹依次飞来的高度(雷达给出的高度数据是不大于30000 的正整数),请聪 明的你帮忙计算这套系统最多能拦截多少导弹。

【输入】

第一行:n,敌国导弹的数量。
第二行:n个整数,用空格分隔,依次是敌国导弹的高度h(h<30000)。

【输出】

该拦截系统最多拦截敌国导弹的数量。

【样例输入】

8 2 7 1 9 10 1 2 3

【样例输出】

4

题目分析:

根据题意,可以知道此题就是求导弹高度的最长上升子序列。
定义状态dp[i]:前i个数的最长上升子序列长度。则不难写出递推式:
dp[i]=max(dp[j])+1,其中j < i&&h[i] > h[j]
其中初始状态dp[0]=1;
所以最终答案res=max(res[i])


#include<iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        int dp[30],h[30],ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>h[i];

        //dp
        for(int i=0;i<n;i++)
        {
            int maxdp=0;
            for(int j=0;j<i;j++)
                if(h[i]<h[j])
                    maxdp=max(maxdp,dp[j]);
            dp[i]=maxdp+1;
            ans=max(dp[i],ans);
        }
        cout<<ans<<endl;
    }
}

2.合唱队形(chorus)
【问题描述】

N 位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的 K 位同学排成 合唱队形。 合唱队形是指这样的一种队形:设 K 位同学从左到右依次编号为 1,2,„„,K,他们 的身高分别为 T1,T2,„„,Tk,则他们的身高满足 T1 < T2<„„Ti,Ti>Ti+1>„„>Tk (1<=i<=K)。 你的任务是,已知有 N 位同学的身高,计算最少需要几位同学出列,可以使得剩下的同 学排成合唱队形。

【输入】

第一行是一个整数 N(2<=N<=1000),表示同学的总数。 第二行有 N 个整数,用空格分隔,第 i 个整数 Ti(130<=Ti<=230)是第 i 位同学的身高(厘 米)。

【输出】

一个整数,表示最少需要几位同学出列,可以使得剩下的同学排成合唱队形。

【样例输入】

8 186 186 150 200 160 130 197 220

【样例输出】

4

题目分析:

根据题意,合唱队的高度是先单调增加,后单调减少。因此我们可以求最长上升子序列和最长下降子序列,假设分别为dp_left[i]和dp_right[i],则合唱队的长度为len=dp_left[i]+dp_right[i]-1,从而求出剩下的人数为n-len,从而答案为res=min(n-len[i])

#include<stdio.h>

const int maxn=5005;

inline int max(int a,int b)
{
    return a>b?a:b;
}

int main()
{
    int h[maxn],dp_left[maxn],dp_right[maxn];
    int n;

    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&h[i]);
        }

        dp_left[0]=dp_right[n-1]=1;

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


        for(int i=n-1;i>=0;i--)
        {
            int maxans=0;
            for(int j=n-1;j>i;j--)
            {
                if(h[i]>h[j])
                {
                    maxans=max(maxans,dp_right[j]);
                }
            }
            dp_right[i]=maxans+1;
        }

        int res=0;

        for(int i=0;i<n;i++)
        {
            res=max(res,dp_left[i]+dp_right[i]-1);
        }

        printf("%d\n",n-res);
    }

}

3.Super Jumping! Jumping! Jumping! HDU - 1087

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

题目分析:

题目的意思就是求最大递增子序列和,做法和求LIS基本一样。

#include<stdio.h>

const int maxn=1005;

int dp[maxn],num[maxn];
//dp[i]=max(dp[j])+num[i]   j<i&&num[i]>num[j]

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);

        dp[0]=num[0];

        int res=-0x999999;

        for(int i=1;i<n;i++)
        {
            dp[i]=num[i];
            int maxSum=0;
            for(int j=0;j<i;j++)
            {
                if(num[i]>num[j])
                {
                    maxSum=maxSum>dp[j]?maxSum:dp[j];
                }
            }
            dp[i]+=maxSum;
            res=res>dp[i]?res:dp[i];
        }

        printf("%d\n",res);
    }
}

4.上帝选人(select)
【问题描述】

世界上的人都有智商和情商。我们用两个数字来表示人的智商和情商,数字大就代表其 相应的属性(智商或情商)高。现在你面前有 N 个人,这 N 个人的智商和情商均已知。 请你选择出尽量多的人,满足选出的人中不存在任意两人 i 和 j,i 的智商大于 j 的智 商而 i 的情商小于 j 的情商。

【输入】

第一行一个正整数 N,表示人的数量。 第二行至第 N+1 行,每行两个正整数,分别表示每个人的智商和情商。

【输出】

仅一行,为最多选出的人的个数。

【样例输入】

3
100 100
110 80
120 90

【样例输出】

2

【数据规模】

对于 100%的数据,N<=1000

题目分析:

我们可以把iq,和eq放在一个pair中,然后调用sort函数,使得iq或者eq单调递增,然后求eq或者iq的最长递增子序列

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=1005;

int main()
{
    int n;
    pair<int,int> people[maxn]; //first:iq,second:eq
    int dp[maxn];

    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            int iq,eq;
            scanf("%d%d",&iq,&eq);
            people[i].first=iq;
            people[i].second=eq;
        }

        sort(people,people+n);

        int res=0;
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            int maxans=0;
            for(int j=0;j<i;j++)
            {
                if(people[i].second>people[j].second)
                {
                    maxans=max(maxans,dp[j]);
                }
            }
            dp[i]=maxans+1;
            res=max(res,dp[i]);
        }

        printf("%d\n",res);
    }
}

猜你喜欢

转载自blog.csdn.net/QingyingLiu/article/details/80548261