zoukankan      html  css  js  c++  java
  • poj1631 dp,最长上升子序列

    题目描述:

      给你两列点,共2n个

      对于1~n着n个数,每个数给定一个对应的数xi,表示左边的点i与右边的点xi相连,求问这些连线中,最大的不交叉的连线数量

      

    可以观察样例

    我们可以发现,满足不交叉这一条件的必要条件是对于i<j,必有xi<xj,(否则,连线一定相交)

    所以,我们就可以发现,这是一道最长上升子序列的问题

    因为数据范围是n<40000,所以要用nlogn的二分搜索LIS

    可解

    附上代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 int up[50000];
     7 int down[50000];
     8 int port[50000];
     9 int n;
    10 const int inf=9999999;
    11 int solve1()
    12 {
    13     fill(up,up+n,inf);
    14     for(int i=0;i<n;i++)
    15     {
    16         *lower_bound(up,up+n,port[i])=port[i];
    17     }
    18     return lower_bound(up,up+n,inf)-(up);
    19 }
    20 
    21 int main()
    22 {
    23     //freopen("in.txt","r",stdin);
    24     int T;cin>>T;
    25     while(T--)
    26     {
    27         scanf("%d",&n);
    28         for(int i=0;i<n;i++) {scanf("%d",&port[i]);}
    29         //cout<<solve1();
    30 
    31         cout<<solve1()<<endl;
    32     }
    33 }
    View Code
  • 相关阅读:
    2-SAT·hihoCoder音乐节
    Music in Car
    Game with a Strip
    Oleg and Little Ponies
    组合数性质求K个数选取i*j个数分成j组的方案数
    Python学习笔记03
    Python学习笔记02
    Python 学习笔记01
    欺骗侦测
    Oracle 使用小计(4)
  • 原文地址:https://www.cnblogs.com/codeoosacm/p/10029317.html
Copyright © 2011-2022 走看看