poj 3903 poj 2533 (LIS模板题)

pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik.

Sample Input

6
5 2 1 4 5 3
3
1 1 1
4
4 3 2 1
Sample Output

3
1
1

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 int a[100010] ;
10 int f[100010] ;
11 //int dp[100010] ;
12 int n ;
13
14 int bsearch(int size, const int &a) {
15     int l=0, r=size-1;
16     while( l <= r ){
17         int mid = (l+r)/2;
18         if( a > f[mid-1] && a <= f[mid] ) return mid;// >&&<= 换为: >= && <
19         else if( a < f[mid] ) r = mid-1;
20         else l = mid+1;
21     }
22 }
23
24 int LIS()
25 {
26     int i, j, size = 1;
27     f[0] = a[0];
28     //dp[0] = 1;
29     for( i=1; i < n; ++i )
30     {
31         if( a[i] <= f[0] ) j = 0; // <= 换为: <
32         else if( a[i] > f[size-1] ) j = size++;// > 换为: >=
33         else j = bsearch(size, a[i]);
34         f[j] = a[i];
35         //dp[i] = j+1;
36     }
37     return size;
38 }
39
40
41 int main ()
42 {
43   //  freopen("in.txt","r",stdin) ;
44     while(scanf("%d" , &n) !=EOF)
45     {
46         int i ;
47         for (i = 0; i < n ; i++)
48             scanf("%d" , &a[i]) ;
49         printf("%d\n" , LIS()) ; // 求最大递增/上升子序列(如果为最大非降子序列,只需把上面的注释部分给与替换)
50     }
51
52
53     return 0 ;
54 }

时间: 2024-12-10 15:50:59

poj 3903 poj 2533 (LIS模板题)的相关文章

poj 3903 &amp; poj 2533 最长上升子序列(LIS)

最长上升子序列. 做这道题之前先做了2533,再看这道题,感觉两道题就一模一样,于是用2533的代码直接交, TLE了: 回头一看,数据范围.2533 N:0~1000:3903 N :1~100000. 原因终归于算法时间复杂度. 也借这道题学习了nlgn的最长上升子序列.(学习链接:http://blog.csdn.net/dangwenliang/article/details/5728363) 下面简单介绍n^2 和 nlgn 的两种算法. n^2: 主要思想:DP: 假设A1,A2..

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

LIS模板题(Longest Ordered Subsequence)

LIS模板题(Longest Ordered Subsequence) poj-2533 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序列 Input 第一行包含一个整数n,表示给出序列的元素个数. 第二行包含n个整数,代表这个序列. 1 <= N <= 1000 Output 输出给出序列的最长子序列的长度. Sample Input 7 1 7 3 5 9 4 8 Sample Output 4 #include <std

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

POJ 3903 Stock Exchange LIS

题目链接:http://poj.org/problem?id=3903 题目大意:LIS的nlog(n)写法. 解题思路:dp[i]:=长度为i的最长递增子序列的末尾元素最小值.那么由于dp[i]形成了一个有序的序列,所以可以采用二分的办法. dp[j] = a[i]  dp[j] <= a[j] < dp[j + 1] 代码: 1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e5 + 5; 3 int a[maxn], n; 4 int d

POJ 3903 Stock Exchange (LIS:最长上升子序列)

POJ 3903Stock Exchange (LIS:最长上升子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=100000) 的数字序列, 要你求该序列中的最长(严格)上升子序列的长度. 分析: 由于n的规模达到10W, 所以只能用O(nlogn)的算法求. 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序列末尾值为x.(如果到目前为止, 根本不存在长i的上升序列, 那么x==INF无穷大) 假设当前遍历到了第j个

poj 3580 SuperMemo splay树模板题

题意: 给一个序列,对其进行各种操作.在对序列仅对操作的处理上,splay是比线段树强大的,虽然均摊复杂度均为logN,但它能支持1:在某个位置插入一些连续的数,2:在某个位置删除一些连续的数.只是splay随便一些200+行. 分析: 网上各种模板介绍漫天飞,这个还算简洁明了. 代码: //poj 3580 #include <stdio.h> #define maxN 200000 int N,T,node; int a[maxN],size[maxN],left[maxN],right[

POJ 1502 MPI Maelstrom (Dijkstra 模板题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5877   Accepted: 3654 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1269 - Intersecting Lines - [平面几何模板题]

题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection b