最大不下降子序列
poj 1609
http://poj.org/problem?id=1609
求可以垒的最高瓷砖,凸巢,凹巢(左右两边)
利用最大上升子序列
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1e4+10; 6 int dp[maxn]; 7 int main(){ 8 int n; 9 while(cin>>n){ 10 if(n == 0) { 11 cout<<"*"<<endl; 12 break; 13 } 14 else{ 15 pair<int, int> p[maxn]; 16 memset(dp, 0, sizeof(dp)); 17 for(int i = 0; i < n; i++){ 18 cin>>p[i].first>>p[i].second; 19 dp[i] = 1; 20 } 21 sort(p, p+n); 22 int ma = 1; 23 for(int i = 1; i < n; i++){ 24 for(int j = 0; j < i; j++){ 25 if(dp[i] < dp[j]+1 && p[i].first >= p[j].first && p[i].second >= p[j].second){ 26 dp[i] = dp[j] + 1; 27 } 28 if(dp[i] > ma) ma = dp[i]; 29 } 30 } 31 cout<<ma<<endl; 32 } 33 } 34 }
二维dp
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1e4+10; 6 int dp[110][110], num[110][110]; 7 int main(){ 8 int n, a, b; 9 while(cin>>n){ 10 if(n == 0) { 11 cout<<"*"<<endl; 12 break; 13 } 14 else{ 15 memset(dp, 0, sizeof(dp)); 16 memset(num, 0, sizeof(num)); 17 for(int i = 0; i < n; i++){ 18 cin>>a>>b; 19 num[a][b]++; 20 } 21 for(int i = 1; i < 105; i++){ 22 for(int j = 1; j < 105; j++){ 23 dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + num[i][j]; 24 } 25 //cout<<dp[i][102]<<"______________"<<endl; 26 } 27 cout<<dp[102][102]<<endl; 28 } 29 } 30 }
只有不断学习才能进步!