poj1631 dp,最长上升子序列

题目描述:

  给你两列点,共2n个

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

  

可以观察样例

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

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

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

可解

附上代码:

扫描二维码关注公众号,回复: 4263876 查看本文章
 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

猜你喜欢

转载自www.cnblogs.com/codeoosacm/p/10029317.html