zoukankan      html  css  js  c++  java
  • dp --- 二维dp + 最大上升子序列

    <传送门>

    滑雪

    Time Limit: 1000MS

     

    Memory Limit: 65536K

    Total Submissions: 74477

     

    Accepted: 27574

    Description

    Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 

     1  2  3  4 5


    16 17 18 19 6


    15 24 25 20 7


    14 23 22 21 8


    13 12 11 10 9


    一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

    Input

    输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

    Output

    输出最长区域的长度。

    Sample Input

    5 5

    1 2 3 4 5

    16 17 18 19 6

    15 24 25 20 7

    14 23 22 21 8

    13 12 11 10 9

    Sample Output

    25

     

    开始遇到这道题的时候感觉无从下手,什么二维矩阵dp想了好半天,最后也是百度了下才做出来的,主要书不知道怎么和最长上升子序列联系在一起,当明白了思路后才发现原来就是一道水题,但是也给这类型的题目提供了一种思路,所以还是值得总结一下。

    【题目分析】

    他给我们的是一个矩阵,矩阵里面的数值相当于高中地理里面学的海拔,让你找一条最长的下降线路出来,每次只可以往上下左右四个方向走。

    收哦先这题是建立在dp里面的最长下降子序列的基础上的,现在的问题是怎么处理二维这样的情况。

    如果二维矩阵中的某个坐标是(x,y),那么它上下两个坐标就是(x±1,y),左右两个坐标就是(x,y±1),我们在遍历的时候可以通过这个条件来判断是否是该点的上下左右四个坐标。

    既然可以知道是否是相邻坐标了,那么怎么走就只是遍历的问题了。

    首先按照高度递增排一下序,然后就是求最长上升子序列了。

    //Memory   Time
    //  392K      360MS
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<iomanip>
    #include<string>
    #include<climits>
    #include<cmath>
    #define MAX 110
    #define LL long long
    using namespace std;
    struct Node{
    int x,y,h;
    };
    Node a[MAX*MAX];
    int dp[MAX*MAX];
    int n,m;
    bool cmp(Node a,Node b)
    {
        return a.h<b.h;
    }
    int main()
    {
    //    freopen("cin.txt","r",stdin);
    //    freopen("cout.txt","w",stdout);
        while(cin>>n>>m){
            int cnt=0;
            for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>a[cnt].h,a[cnt].x=i,a[cnt].y=j,cnt++;
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            sort(a,a+cnt,cmp);
            int ans=1;
            for(int i=1;i<cnt;i++){
                    int tmp=0;
                for(int j=0;j<i;j++){
                    if(abs(a[i].x-a[j].x)==1&&abs(a[i].y-a[j].y)==0&&a[j].h<a[i].h&&dp[j]>tmp)
                    tmp=dp[j];
                    if(abs(a[i].x-a[j].x)==0&&abs(a[i].y-a[j].y)==1&&a[j].h<a[i].h&&dp[j]>tmp)
                        tmp=dp[j];
                }
                dp[i]=tmp+1;
                if(dp[i]>ans)
                    ans=dp[i];
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    quick cocos naja
    quick cocos sprite 3 zhongfangshi
    quick cocos moveto
    quick cocos animate2
    quick cocos animate
    quick cocos schedule
    quick cocos scheduler update
    【leetcode】括号的最大嵌套深度
    【leetcode】删除某些元素后的数组均值
    【leetcode】二叉树的最小深度
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3838373.html
Copyright © 2011-2022 走看看