zoj 1025 || poj 1065 Wooden Sticks(DP做法)

最长XX子序列小变形。

 

学习了下,详见 http://blog.csdn.net/wmbol/archive/2010/04/05/5450952.aspx

 

还有党说的删除最大串,然后再DP,再删再。。。这里的DP就是类似找最长XX子序列了。

 

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; // Don't forget the header files!!!Especially the ALGORITHM!!! typedef struct{ int len,wei; }Stick; Stick s[5010]; int cmp( Stick x,Stick y) { if( x.len == y.len ) return x.wei < y.wei; else return x.len < y.len; } int main(void) { int ncases; int n; scanf("%d",&ncases); int dp[5010]; while( ncases--) { while( scanf("%d",&n)!=EOF ) { for(int i=1; i<=n; i++) scanf("%d %d",&s[i].len,&s[i].wei); sort(s+1,s+n+1,cmp); // sort....second-sort. memset(dp,0,sizeof(dp)); dp[1] = 1; for(int i=2; i<=n; i++) { int max = 1; for(int k=1; k<i; k++) { if( s[i].wei < s[k].wei ) if( dp[k] + 1 > max ) max = dp[k] + 1; } dp[i] = max; } int max = 0; for(int i=1; i<=n; i++) if( dp[i] > max ) max = dp[i]; cout << max << endl; } } return 0; }  

你可能感兴趣的:(Algorithm,struct,header,2010)