美文网首页
poj1065 偏序+LIS

poj1065 偏序+LIS

作者: 暖昼氤氲 | 来源:发表于2019-11-04 11:59 被阅读0次
/*
Time:2019.11.3
Author: Goven
type:偏序问题+LIS(最长下降子序列) 
err:
ref:不会:https://blog.csdn.net/AC_hell/article/details/51416840 
知识点:偏序序列 + lower_bound(二分查找) 
*/ 
#include<iostream>
#include<algorithm>
#define MAXN 5005
using namespace std;
struct Node {
    int l, w;
};
Node a[MAXN];
int dp[MAXN]; // 表示栈 
int top;
bool cmp(const Node &x, const Node &y) {
    if (x.l != y.l) return x.l < y.l;
    return x.w < y.w; 
}
int find(int v) {
    int l = 0, h = top;
    int mid;
    while (l <= h) {
        mid = (l + h) / 2;
        if (dp[mid] == v) return mid;//err1:漏了这一句,所以二分中一定要分三种情况 
        else if (dp[mid] < v) h = mid - 1;
        else l = mid + 1; 
    }
    return l;
}
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> a[i].l >> a[i].w;
        }
        sort(a, a + n, cmp);
        top = 0;
        dp[top] = a[0].w;
        for (int i = 1; i < n; i++) {
            if (dp[top] > a[i].w) dp[++top] = a[i].w;
            else {
                int tp = find(a[i].w);
                dp[tp] = a[i].w;
            }
        }
        top++;
        cout << top << endl;
    } 
    return 0;
}
//lower_bound 
#include<iostream>
#include<algorithm>
#include<functional> //greater<type>() 
#include<cstring>
#define MAXN 5005
using namespace std;
struct Node {
    int l, w;
};
Node a[MAXN];
int dp[MAXN]; // 表示栈 
int top;
bool cmp(const Node &x, const Node &y) {
    if (x.l != y.l) return x.l < y.l;
    return x.w < y.w; 
}
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> a[i].l >> a[i].w;
        }
        sort(a, a + n, cmp);
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++) {
            *lower_bound(dp, dp + n, a[i].w, greater<int>()) = a[i].w;
        }
        cout << lower_bound(dp, dp + n, 0, greater<int>()) - dp << endl;//找到第一个小于等于0的位置——即栈的top 
    } 
    return 0;
}

相关文章

  • poj1065 偏序+LIS

  • 偏序(cdq bitset)

    题目链接:三维偏序 cdq分治: 题目链接:五维偏序

  • 偏序关系

    前言:到了二元关系中最后一部分,非常非常抽象,但是理解了就还好,我们一步一步来 0X00 「偏序关系」的基本定义 ...

  • 离散数学

    偏序:在整数集中定义偏序:若a能整除b,我们就记为a≺b显然它满足序公理。但整数集中,不是任何两个数都存在整除关系...

  • 事务si和ssi隔离级别

    rr隔离级别:有幻读但无写偏虚 si快照隔离级别:有写偏序但无幻读,写偏序问题的规避留给应用避免,应用使用sele...

  • Dilworth定理

    狄尔沃斯定理(Dilworth's theorem)亦称偏序集分解定理,其定义是:对于任意有限偏序集,其最大反链中...

  • 一, 格的基本定义和性质 定义 偏序集定义: 偏序集中对任意两个元a, b, 在L中都存在一个元是他们...

  • Chapter11——动态规划——经典问题

    1. 题目列表 POJ3267(字符串匹配dp) POJ1836(LIS最长上升子序列的变形:最长先上升后下降子序...

  • 赛码网 水仙花数例题 python solution

    def judge(line): lis=[] for pin line.split(): lis.append(...

  • LIS

    求最大上升子序列(Longest Increasing Subsequence),动态规划中最基础的题目。 状态:...

网友评论

      本文标题:poj1065 偏序+LIS

      本文链接:https://www.haomeiwen.com/subject/avorbctx.html