Wooden Sticks POJ 1065(简单dp)

原题

题目链接

题目分析

题意很明确,就是要维护单调递增的序列,最后看有多少种单调序列即可,设定一个dp数组,cnt表示数组大小,初始化为0,然后把所有木头从小到大排个序,当遍历到木头i时,如果dp数组里有比木头i还小的,就代替它,否则就dp[cnt++]=木头i,最后cnt就是答案.

代码

 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 }

 

posted @ 2019-08-25 16:36  VBL  阅读(107)  评论(0编辑  收藏  举报