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 }

 

转载于:https://www.cnblogs.com/VBEL/p/11408359.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值