poj1088 滑雪(簡單dp+搜尋+二維最長下降子序列)
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
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。遍歷一下陣列,當遍歷到某個點時候,搜尋它的上下左右四個點是否滿足小於當前點,如果滿足的話,繼續遞迴下去進行搜尋,遞迴結束後, 返回上下左右四個點的最大值,然後將最大值+1即為當前點的最長下降子序列的值,記錄到len陣列中,找出len陣列的最大值,就是所求;
#include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cstring> #include<map> #define INF 0x3f3f3f3f #define N 110 using namespace std; int next[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; int len[N][N],a[N][N]; int m,n; int dp(int x,int y) { int tx,ty,k,s,ms; ms=0; if(len[x][y]!=0)//遞迴出口; return len[x][y]; for(k=0; k<=3; k++) { tx=next[k][0]+x; ty=next[k][1]+y; if(tx<0||ty<0||tx>m-1||ty>n-1) continue; if(a[tx][ty]<a[x][y]) { s=dp(tx,ty); ms=max(ms,s); } } len[x][y]=ms+1; return len[x][y]; } int main() { int i,j,Max; while(~scanf("%d%d",&m,&n)) { memset(a,0,sizeof(a)); memset(len,0,sizeof(len)); for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d",&a[i][j]); Max=-1; for(i=0; i<m; i++) for(j=0; j<n; j++) Max=max(Max,dp(i,j)); printf("%d\n",Max); } return 0; }
相關推薦
poj1088 滑雪(簡單dp+搜尋+二維最長下降子序列)
Description Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想
[poj1088]滑雪(二維最長下降子序列)
ostream def pan || poj type name 記憶化搜索 ring 解題關鍵:記憶化搜索 #include<cstdio> #include<cstring> #include<algorithm> #in
POJ 1836 Alignment(DP max(最長上升子序列 + 最長下降子序列))
mission weight ring limit problem stream [0 sin ++ Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14486
【ZOJ1108】FatMouse's Speed(最長下降子序列)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 FatMouse's Speed Time Limit: 2 Seconds Memo
SDNUOJ 1292 動態規劃之LDS(最長下降子序列)
Description 昨天是平安夜,聖誕老人從房頂上的煙囪裡爬到小朋友床邊把禮物送給夢鄉中的小朋友,但是今年的聖誕老人是處女座的,他有很嚴重的強迫症,他從一條街的一端開始,每次送禮物進的煙囪都不能比之前進的煙囪高,而且他還想要送出最多的禮物。 Input 輸入資料只有一行,該行包含若干個資
101889F Fundraising (二維帶權最長上升子序列)
題意:二維帶權最長上升子序列。 解題思路:先對一維排序,然後在第二維做最長上升子序列的NlogN做法,同時維護好權值即可。記得離散化。 這裡相同的點最好合並一下,方便處理。 #incl
DP—最長下降子序列(程式碼)
原理和最長上升子序列一樣,排序和二分時比較方法改一下即可 #include <iostream> #include <cstring> int arr[1003],ans[1003]; using namespace std; int
poj——1159(dp之最長公共子序列)
注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include
Common Subsequence (DP最長公共子序列)
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ...,
【dp優化】LIS(最長上升子序列)長度的nlogn演算法
這道題第一反應就想到了 [CEOI96]渡輪問題 就是一個非常裸的求最長上升子序列的長度,還不要方案,非常的水。然而,常規的dp複雜度是 O(n^2) ,這道題會愉快地TLE,所以要進行nlogn級別的優化。 //O(n^2) TLE #include
筆試題學習(dp,重疊子問題,卡特蘭數,手電過橋,最長公共子序列)
卡特蘭數:https://blog.csdn.net/doc_sgl/article/details/8880468 dp,重疊子問題:https://www.cnblogs.com/hapjin/p/5572483.html 美團2016校招筆試題:https://zhuanlan.zhihu.com/p/
【HDU】1159 Common Subsequence(DP、最長公共子序列)
【HDU】1159 Common Subsequence (DP、最長公共子序列) 題目內容 Problem Description A subsequence of a given sequence is the given sequence
SCU - 4441 Necklace(樹狀數組求最長上升子數列)
arranged art 樹狀 max pri nsis names for each cnblogs Necklace frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2,
HDU 1513 Palindrome:LCS(最長公共子序列)or 記憶化搜索
ble urn size rom str 個數 blog using reverse 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 題意: 給你一個字符串s,你可以在s中的任意位置添加任意字符,問你將s變成一個回
POJ 1458 - Common Subsequence(最長公共子序列) 題解
void 方式 mem strong 輸出 inline ron eof init 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=1458 題目大意: 有若幹組數據,每組給出兩個字符
HDU1257 最少攔截系統 —— 貪心 or LIS(最長上升子序列)
ret pre key ear out hide 裏來 程序 http 題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 最少攔截系統 Time Limit: 2000/1000 MS (Java/Oth
LCS(最長公共子序列)
rdquo 工作 dna abc sub 動態規劃 != 給定 似的 這個問題很有意思,在生物應用中,經常需要比較兩個(或多個)不同生物體的DNA片段。例如,某種生物的DNA可能為S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA,S2 = GTCGT
poj 3616 Milking Time(dp,類似於最長上升子序列)
題意:給奶牛擠奶,共m次可以擠,給出每次開始擠奶的時間st,結束擠奶的時間ed,還有擠奶的量ef, 每次擠完奶要休息r時間,問最大擠奶量. 題解:此題靈感來自於最長上升子序列的做法 #include <iostream> #include <cstring>
動態規劃-導彈攔截(求最長不上升子序列和最長上升子序列)
lower_bound(a,a+n,i)函式 返回從陣列a到a+n中第一個>=i的元素地址 upper_bound(a,a+n,i)函式 返回從陣列a到a+n中第一個>i的元素地址 #include<cstdio> #include<algorithm> #i
小樂樂匹配字串 (最長公共子序列)
連結:https://ac.nowcoder.com/acm/contest/301/E 來源:牛客網 小樂樂匹配字串 時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 32768K,其他語言65536K 64bit IO Format: %lld 題目描述