poj 1065 Wooden Sticks dp LIS

题目

题目链接:http://poj.org/problem?id=1065

题目来源:《挑战》练习题

简要题意: n 根木棍长度li,重量 wi ,需要以一个序列处理它们。
     前一根木棍长度重量都不超过当前的则不需要处理时间,否则需要 1 时间,求最短处理时间。

数据范围:1n5000;li,wi10000

题解

这题很像导弹拦截。

基本上就是排序一下,然后反向做个LIS就行了。

可以这么做的原因是偏序集的最少划分链等于反向最长链。

排序按照一维升序,一维相同,另一位升序就行了,正好暗合pair。

实现

写题目的姿势很重要,我觉得我这个pair+lower_bound的姿势相当优雅。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 5005;

PII a[N];
int lis[N];

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &a[i].fi, &a[i].se);
        }
        sort(a, a+n);

        int ans = 0;
        for (int i = n-1; ~i; i--) {
            int pos = lower_bound(lis, lis+ans, a[i].se)-lis;
            lis[pos] = a[i].se;
            if (pos == ans) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值