Wooden Sticks POJ 1065 (simple dp)

The original title

Topic Link

Topic analysis

Meaning of the questions is clear, is to maintain a monotonically increasing sequence, the last to see how many monotonous sequence can have, set a dp array, cnt represents the size of the array is initialized to 0, and then put all the wood from small to large row of a sequence, when while traversing the wood i, if dp array smaller than wood i have, just replace it, otherwise dp [cnt ++] = i wood, and finally cnt is the answer.

Code

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13 
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18 
19 struct N
20 {
21     int l,w;
22 }stick[5000];
23 int dp[5000];
24 int cnt;
25 
26 bool cmp(N a,N b)
27 {
28     if(a.l==b.l) return a.w<b.w;
29     return a.l<b.l;
30 }
31 
32 void add(int i)
33 {
34     for(int j=0;j<cnt;j++)
35     {
36         if(stick[i].w>=dp[j])
37         {
38             dp[j]=stick[i].w;
39             return ;
40         }
41     }
42     dp[cnt++]=stick[i].w;
43 }
44 
45 int main()
46 {
47 //    freopen("black.in","r",stdin);
48 //    freopen("black.out","w",stdout);
49     int t;
50     cin>>t;
51     while(t--)
52     {
53         int n;
54         cin>>n;
55         for(int i=0;i<n;i++) cin>>stick[i].l>>stick[i].w;
56         sort(stick,stick+n,cmp);
57         cnt=0;
58         for(int i=0;i<n;i++) add(i);
59         cout<<cnt<<endl;
60     }
61     return 0;
62 }

 

Guess you like

Origin www.cnblogs.com/VBEL/p/11408359.html