uva 11499 Find the Strictly Increasing Submatrix

BabbleDay posted @ 2013年4月19日 19:20 in 刷题防身 , 1299 阅读

真高兴又可以写解题心得了~因为上次经过不懈努力解决了自己的第一道动态规划所以又找来一道题练习,各种弯弯绕中获得了:

1. 动态规划的状态记录不要局限于一维:就是不要总想着建个struct来存各种信息,倒不如直接多维来的清晰

2. 记得每个变量都要初始话: 以前用无脑的java各种默认为零,现在不初始化的结果就是core dump,重点是还总是找不到错

3. 关于core dump:先查数组越界的问题,然后再看看栈溢出和指针神马的。。

4. 滚动数组实现: t 和 1-t,就能永远在0、1中间循环了

 

接下来是这道题:要求找到递增的子矩阵
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2494

算法核心是:
关注第i行从第j1列到第j2列是否递增,
    若否,break到j1+1的循环;
    若是,检查(i,j1)是否比(i-1,j2)大,
                    若是,当前滚动数组[j1][j2] = 原滚动数组[j1][j2] + j2 - j1 + 1;
                                                             // 原滚动数组[j1][j2]记录上一行及其之前行严格递增矩阵元素个数
                    若否,当前滚动数组[j1][j2] = j2 - j1 + 1;
                    将当前滚动数组[j1][j2] 和 输出结果比较取大

注意点:j2==j1时的情况, i==0时的情况

#include <iostream>
#include <cstring>

#define rep(i, s, t) for(int i=s; i<t; i++)

using namespace std;

void the_case(int n, int m)
{
    int in[n][m];
    int a[2][m][m];
    int t=0; //勿忘初始化
    int ans = 0;
    memset(a, 0, sizeof(a));
    rep(i, 0, n)
    {
        rep(j, 0, m)
        {
            cin >> in[i][j];
        }
    }
    rep(i, 0, n)
    {
        memset(a[t], 0, sizeof(a[t]));
        for (int j1=0; j1<m; j1++) 
        {
            if (i==0 || in[i][j1] > in[i-1][j1])
                a[t][j1][j1] = a[1-t][j1][j1] + 1;
            else a[t][j1][j1] = 1;
            if(ans<a[t][j1][j1]) ans = a[t][j1][j1];
            rep(j2, j1+1, m)
            {
                if(in[i][j2]>in[i][j2-1]) // j2==0越界
                {
                    if(i==0 || in[i][j1]>in[i-1][j2])
                        a[t][j1][j2] = a[1-t][j1][j2]+j2-j1+1;
                    else
                        a[t][j1][j2] = j2-j1+1;
                    if(ans<a[t][j1][j2]) ans = a[t][j1][j2];
                }
                else break;
            }
        }
        t = 1-t;
    }
    cout << ans << endl;
}

int main()
{
    int n,m;
    while(true)
    {
        cin >> n >> m;
        if(n==0 && m==0) break;
        else the_case(n,m);
    }
    return 0;
}

算法优化:

增加预处理:构建二维矩阵记录每个数从自身开始的连续最大递增序列长度(含自身)
遍历所有数, if (L[i][j1] > 1)
                         j2=j1+1 ----> j1+L[i][j1]
                              if (i!=0 && N[i][j1]>N[i-1][j2])
                                    a[t][j1][j2] = a[1-t][j1][j2] + j2 - j1 + 1;
                              else a[t][j1][j2] = j2 - j1 + 1;
                    else
                         if(i!=0 && N[i][j1] > N[i-1][j1]) a[t][j1][j1] = a[1-t][j1][j1] + 1;
                         else a[t][j1][j1] = 1;

Avatar_small
SEBA 10th Question 说:
2023年9月26日 22:19

SEVA Board Students can Download Their Subject wise PDF Files it will be Beneficial for Study Material Practice and Regular Check Bit Bank and Question Bank, Important Questions, Students who are Attending Assam 10th Annual Exam SEBA 10th Question Paper 2024 can make use of this Information which we have Provided here. This Website Available Assam HSLC Last year Exam Question Paper for Students can make a better Performance in Exam 2024.Students who have completed their Preparation can Start Practicing the SEBA HSLC Previous Question Paper 2024.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter