最长上升子序列 CSU - 1047

名词解释:

一串数字比如1、5、3、6、9、8、10,它的子序列是从左到右不连续的若干个数,比如1、5、6,3、9、8、10都是它的子序列。

最长上升子序列即从左到右严格增长的最长的一个子序列,1、5、6、9、10就是这个序列的一个最长上升子序列。

给出若干序列,求出每个序列的最长上升子序列长度。

Input

多组数据,每组第一行正整数n,1 <= n <= 1000,第二行n个空格隔开的不大于1,000,000的正整数。

Output

每组数据输出一行,最长上升子序列的长度。

Sample Input

7
1 5 3 6 9 8 10

Sample Output

5 还没有理解LIS的精髓 所以这道题是排序+LCS做的

 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<vector>
16 int a[1010],b[1010];
17 int c[1010][1010];
18 int main()
19 {
20     int t;
21     int lena=0;
22     while(cin>>t)
23     {
24         a[0]=0;
25         b[0]=0;
26         memset(c,0,sizeof(c));
27         for(int i=1;i<=t;i++)
28         {
29             lena=i;
30             cin>>a[i];
31             b[i]=a[i];
32         }
33         sort(b,b+lena+1);
34         int max1=0;
35         for(int i=1;i<=lena;i++)
36         {
37             for(int j=1;j<=lena;j++)
38             {
39                 if(a[i]==b[j])
40                     {
41                         c[i][j]=c[i-1][j-1]+1;
42                     max1=max(max1,c[i][j]);
43                     }
44                     else
45                 c[i][j]=max(c[i-1][j],c[i][j-1]);
46             }
47         }
48         cout<<max1<<endl;
49     }
50     return 0;
51 }

时间: 2024-08-09 10:43:39

最长上升子序列 CSU - 1047的相关文章

最长上升子序列 CSU - 1047 ( LIS LCS )

名词解释: 一串数字比如1.5.3.6.9.8.10,它的子序列是从左到右不连续的若干个数,比如1.5.6,3.9.8.10都是它的子序列. 最长上升子序列即从左到右严格增长的最长的一个子序列,1.5.6.9.10就是这个序列的一个最长上升子序列. 给出若干序列,求出每个序列的最长上升子序列长度. Input 多组数据,每组第一行正整数n,1 <= n <= 1000,第二行n个空格隔开的不大于1,000,000的正整数. Output 每组数据输出一行,最长上升子序列的长度. Sample

【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数字替换,最终形成一个1~N的排列,求这个排列的最长上升子序列长度为N-1的方案数. 题目思路: [模拟] 这道题需要分类讨论. 首先可以肯定,一个长度为n的序列最长上升子序列长度为n-1(最长下降子序列长度为2),那么这个序列的样子是1~n从小到大排列后其中一个数字挪到其余数字中间(错位) 一个长度为L的

[csu/coj 1078]多个序列的最长公共子序列

题意:给n个序列,同一个序列里面元素互不相同,求它们的最长公共子序列. 思路:任取一个序列,对于这个序列里面的两个数ai,aj(i<j),如果对于其它每一个序列,都出现过ai,aj,且ai在aj之前出现,那么i到j连一条长度为1的有向边,完美转化为DAG最长路.需要注意:对于某个数,如果某个序列没出现那么这个点的答案应该为-INF,表示这个点表示的状态不合法. 代码: 1 #pragma comment(linker, "/STACK:10240000,10240000") 2

CSU 1225 最长上升子序列并记录其个数

1 for(int j=0;j<i;j++){ 2 if(h[i] > h[j]){ 3 if(len[i] == len[j] + 1) cnt[i]+=cnt[j]; 4 if(len[i] < len[j] + 1) len[i] = len[j] + 1 , cnt[i] = cnt[j]; 5 } 6 //身高相同的情况统计 7 /*else if(h[i] == h[j]){ 8 if(len[i] == len[j]) cnt[i] += cnt[j]; 9 if(len[

14-高效求最长公共子序列(二维数组存不下)

/*                                   See LCS again时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述 There are A, B two sequences, the number of elements in the sequence is n.m; Each element in the sequence are different and less than 100000. Calculate the length

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 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)

最长公共子序列的代码实现

关于最长公共子序列(LCS)的相关知识,http://blog.csdn.net/liufeng_king/article/details/8500084 这篇文章讲的比较好,在此暂时不再详说. 以下是我代码实现两种方式:递归+递推: 1 #include <bits/stdc++.h> 2 using namespace std; 3 int A[100]; 4 int B[100]; 5 6 //int B[]={2,3,5,6,9,8,4}; 7 int d[100][100]={0};

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

算法面试题 之 最长递增子序列 LIS

找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E6%95%B0%E7%BB%84%E9%83%BD%E6%B2%A1%E7%BB%99%E5%87%BA%E6%9D%A5 我就是理解了一下他的分析 用更通俗易懂的话来说说题目是这样 d[1..9] = 2 1 5 3 6 4 8 9 7 要求找到最长的递增子序列首先用一个数组b[] 依次的将d里面