luogu P1233 木棍加工

31 篇文章 0 订阅
13 篇文章 0 订阅

题解

这道题很像之前做过的导弹拦截的题目,关键点是偏序集。
显然答案就是这个二维数集合的最少链划分数,也就是最大的反链长度 (根据Dilworth定理)。
但是直接求反链还是难。
咱们先对其中一维由小到大排序,再对另外一维被固定的数,考察其非降序情况即可。
我看了一些贪心的做法也是可行的。

ps: 再次出现 bus error。 在sort()中必须要用严格升序,参见std::sort function gives “Bus error: 10”


Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int n,m;
struct point{
    int l,w;
}cot[5002];
int f[5002];
typedef struct point P;

bool cmp(P &a, P &b){
    return (a.l == b.l ? a.w<b.w : a.l<b.l);
}
int main(){

    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>cot[i].l>>cot[i].w;
    }

    sort(cot+1,cot+1+n,cmp);

    int cur,ans=0;
    for(int i=n;i>=1;i--){ // dp
        cur = 0;
        for(int j=i+1;j<=n;j++){
            if( cot[i].w > cot[j].w )
                cur = max(cur,f[j]);
        }
        f[i] = cur+1;
        ans = max(ans,f[i]);
    }

    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值