lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/
最长上升连续子序列 II
给定一个整数矩阵(其中,有 n 行, m 列),请找出矩阵中的最长上升连续子序列。(最长上升连续子序列可从任意行或任意列开始,向上/下/左/右任意方向移动)。
样例
给定一个矩阵
[
[1 ,2 ,3 ,4 ,5],
[16,17,24,23,6],
[15,18,25,22,7],
[14,19,20,21,8],
[13,12,11,10,9]
]
返回 25
思路:记忆化搜索 + dp
设Lics(num)表示以num开头的最长上升子连续序列的长度, 则Lics(A[x][y]) = max(Lics(A[x-1][y]), Lics(A[x][y-1]), Lics(x+1,y), Lics(x, y+1))+1;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<deque>
#include<map>
using namespace std; class Solution {
public:
/**
* @param A an integer matrix
* @return an integer
*/
int n, m, maxL;
int lics[][];
int vis[][];
int dir[][] = {{, }, {, }, {,-}, {-,}};
int dfs(vector<vector<int>>& A, int x, int y){
int maxLics = ;
vis[x][y] = ;
for(int i=; i<; ++i){
int xx = x+dir[i][];
int yy = y+dir[i][];
if(xx< || yy< || xx>=n || yy>=m) continue;
if(A[x][y] >= A[xx][yy]) continue;
if(!vis[xx][yy])
maxLics = max(maxLics, dfs(A, xx, yy));
else
maxLics = max(maxLics, lics[xx][yy]);
}
lics[x][y] = maxLics+;
if(maxL < lics[x][y]) maxL = lics[x][y];
return lics[x][y];
} int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) {
n = A.size();
if(n == ) return ;
m = A[].size();
memset(lics, , sizeof(lics));
memset(vis, , sizeof(vis));
maxL = ;
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
if(!vis[i][j])
dfs(A, i, j);
return maxL;
}
};
/*
1 2 3 4 5
16 17 24 23 6
15 18 25 22 7
14 19 20 21 8
13 12 11 10 9
*/
lintcode 最长上升连续子序列 II(二维最长上升连续序列)的更多相关文章
- SGU 199 Beautiful People 二维最长递增子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- [poj1088]滑雪(二维最长下降子序列)
解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- java二维不定长数组测试
package foxe; import javax.swing.JEditorPane;import javax.swing.JFrame; /** * @author fooxe * * @see ...
- 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)
最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...
- codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)
题目描述 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y ...
- UVALive - 7374 Racing Gems 二维非递减子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/356795 Racing Gems Time Limit: 3000MS 问题描述 You are playi ...
- 最长非降子序列的O(n^2)解法
这次我们来讲解一个叫做"最长非下降子序列"的问题及他的O(n^2)解法. 首先我们来描述一下什么是"最长非下降子序列". 给你一个长度为n的数组a,在数组a中顺 ...
随机推荐
- db2定界符
在DB2数据库中,在导出DEL文件时,默认的字符分隔符是".字段分隔符是, (逗号).有一个需求是要为Oracle数据库提供数据,因此就想使用"|"作为数据的字段分隔符. ...
- mac 之 jmeter下载、解压、启动
1:下载地址:http://jmeter.apache.org/download_jmeter.cgi 2:双击下载的zip文件,即可解压 3:打开终端,cd 到解压的目录下 例如:cd /User ...
- sql2000分享 批量建表dev_编号
批量建表dev_3970000000014到dev_3970000000035 declare @i bigint declare @j int ) ) ) ) set @sql = '' set @ ...
- 常用的107条Javascript
1. document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >( ...
- python 通用 修饰器
import functools def log(option): def dec(func): def swapper(*arg, **karg): functools.update_wrapper ...
- mysql 连接慢的问题(超过了1秒)
http://www.cnblogs.com/isenhome/p/5133547.html 症状描述 本机连接mysql速度很快 远程ping mysql主机速度正常 远程连接mysql速度需要等待 ...
- android——handler机制原理
在android版本4.0及之后的版本中多线程有明确的分工,子线程可以写所有耗时的代码(数据库.蓝牙.网络服务),但是绝对不能碰UI,想碰UI跟着主线程走,那么我们如何才能让主线程知道我们要对 UI进 ...
- java 心得
11. 最后的笑声 package javaBookPractice; public class LastLaugh { public static void main(String[] args) ...
- Powershell 十个常见任务
学习Powershell的时候,基本的语法也了解了一些,但是就是不知道要写些什么?作为一个过来者,和大家一起分享下常见的几个管理任务脚本. 1.更改本地Administrator账号密码 [ADSI] ...
- SQL SERVER 2016 AlwaysOn 无域集群+负载均衡搭建与简测
之前和很多群友聊天发现对2016的无域和负载均衡满心期待,毕竟可以简单搭建而且可以不适用第三方负载均衡器,SQL自己可以负载了.windows2016已经可以下载使用了,那么这回终于可以揭开令人憧憬向 ...