Bridging signals POJ 1631(最长递增子序列dp)

原题

题目链接

题目分析

由题目知,如果能求出连接点的最长递增子序列,则可以把连接不在该序列中的点的线全部剪掉.而维护最长递增子序列可以用dp来做,考虑到相同长度的递增子序列末尾数字越小越好,可以这样定义dp,dp[i]长度为i的递增子序列的最小末尾值,初始化为INF,由于这个dp具有有序性,因此可以用二分来加快更新,每次遍历到值num[i],只需二分找出大于等于num[i]的更新之即可.最后从扫一遍dp数组即可得到最长长度.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18
19 int dp[50000];
20
21 int main()
22 {
23 //    freopen("black.in","r",stdin);
24 //    freopen("black.out","w",stdout);
25     int t;
26     cin>>t;
27     while(t--)
28     {
29         int n;
30         cin>>n;
31         for(int i=0;i<=n;i++) dp[i]=INF_INT;
32         for(int i=0;i<n;i++)
33         {
34             int x;
35             scanf("%d",&x);
36             *lower_bound(dp,dp+n,x)=x;
37         }
38  //       for(int i=0;i<n;i++) printf("dp[%d]=%d\n",i,dp[i]);
39         int ans=0;
40         while(dp[ans]!=INF_INT) ans++;
41         cout<<ans<<endl;
42     }
43     return 0;
44 }

原文地址:https://www.cnblogs.com/VBEL/p/11408916.html

时间: 2024-08-04 08:35:33

Bridging signals POJ 1631(最长递增子序列dp)的相关文章

POJ 1631(最长上升子序列 nlogn).

~~~~ 由题意可知,因为左边是按1~n的顺序递增排列,要想得到不相交组合,左边后面的一定与相应右边后面的相连,如此一来, 就可以发现其实是一道最长上升子序列的题目,要注意的是N<40000,用n^2的算法一定会超时. 题目链接:http://poj.org/problem?id=1631 ~~~~ nlogn的算法在这里补充一下. 最长不下降子序列的O(nlogn)算法分析如下: 设 A[t]表示序列中的第t个数,F[t]表示从1到t这一段中以t结尾的最长上升子序列的长度,初始时设F [t]

poj之最长递增子序列

题目:POJ 2533   Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 &l

Bridging signals(NlogN最长上升子序列)

Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2354    Accepted Submission(s): 1536 Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferlan

UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)

Wavio Sequence  Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence. ·  The last (n+1) integers of Wavio s

Longest Ordered Subsequence POJ - 2533 最长上升子序列dp

题意:最长上升子序列nlogn写法 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int dp[1005]; 7 int a[1005]; 8 int main(){ 9 int n; 10 while(cin>>n){ 11 for(int i=0;i<n;i++){ 12

POJ 1631 最长上升子序列的O(nlogn)算法

用普通的O(n^2)方法会超时,于是在网上习得O(nlogn)的算法. 1 #include<algorithm> 2 #include<cstdio> 3 #include<vector> 4 using namespace std; 5 6 int a[40000]; 7 vector<int> dp; 8 int main() 9 { 10 int t; 11 scanf("%d",&t); 12 while(t--) 13

最长递增子序列 dp

/* Name: Copyright: Author: 流照君 Date: data Description: */ #include <iostream> #include<string> #include <algorithm> #include <vector> #define inf 0x3f3f using namespace std; typedef long long ll; int main() { //freopen("in.tx

POJ 2533 Longest Ordered Subsequence【最长递增子序列】【DP思想】

Longest Ordered Subsequence Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 1 Problem Description A numeric sequence of ai is ordered ifa1 < a2 < ... < aN. Let t

最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij=yj.例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列. 考虑最长公共子序列问题如何分解成