http://poj.org/problem?id=1065
Test instructions is relatively simple, there are N and sticks, in advance to know the length and width of each stick, these sticks need to be sent to processing, the first stick need a minute of production time, if the length of the current stick and width
is larger than the previous stick, then this stick does not require production time, ask you the minimum production time is how much?
First can be greedy, first by the length of l sort, if L is the same, by the width of W sort.
Starting with i=0, each time the next i+1-n-1 is unmarked and the length and width of the stick is greater than or equal to I, the length and width are marked.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intMAXN =5010;7 8 struct Point9 {Ten intl,w; One BOOL operator< (ConstPoint &a)Const A { - returnL==a.l? w<a.w:l<A.L; - } the }P[MAXN]; - - intMARK[MAXN]; - + intMain () - { + intT,n; Ascanf"%d",&t); at while(t--) - { -scanf"%d",&n); - for(intI=0; i<n;i++) - { -scanf"%d%d",&p[i].l,&P[I].W); in } -Sort (p,p+n); to //for (int i=0;i<n;i++) printf ("%d%d\n", P[I].L,P[I].W); + ints=0; -memset (Mark,0,sizeof(Mark)); the for(intI=0; i<n;i++) * { $ inttemp=P[I].W;Panax Notoginseng if(!Mark[i]) - { the for(intj=i+1; j<n;j++) + { A if(p[j].w>=temp&&!Mark[j]) the { +temp=P[J].W; -mark[j]=1; $ } $ } -s++; - } the } -printf"%d\n", s);Wuyi } the return 0; -}
DP: Sort the same as above, then find the longest descending subsequence of W.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intMAXN =5010;7 8 struct Point9 {Ten intl,w; One BOOL operator< (ConstPoint &a)Const A { - returnL==a.l? w<a.w:l<A.L; - } the }P[MAXN]; - - intDP[MAXN]; - + intMain () - { + intT,n; Ascanf"%d",&t); at while(t--) - { -scanf"%d",&n); - for(intI=0; i<n;i++) - { -scanf"%d%d",&p[i].l,&P[I].W); in } -Sort (p,p+n); to //for (int i=0;i<n;i++) printf ("%d%d\n", P[I].L,P[I].W); + intans=0; - for(intI=0; i<n;i++) dp[i]=1; the for(intI=0; i<n;i++) * { $ for(intj=0; j<i;j++)Panax Notoginseng if(P[J].W>P[I].W) Dp[i]=max (dp[i],dp[j]+1); -ans=Max (ans,dp[i]); the } +printf"%d\n", ans); A } the return 0; +}
poj-1065 wooden sticks (greedy or DP)