POJ 2533 Longest Ordered Subsequence (模版LIS)

题意:输出最长递增子序列的长度
思路:直接裸LIS,
</pre><pre code_snippet_id="648389" snippet_file_name="blog_20150420_4_7842089" class="sio" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"><pre name="code" class="cpp">#include<cstdio>

const int N = 1001;
int a[N], f[N], d[N]; // d[i]用于记录a[0...i]的最大长度
int bsearch(const int *f, int size, const int &a) {
    int l=0, r=size-1;
    while( l <= r ){
        int mid = (l+r)/2;
        if( a > f[mid-1] && a <= f[mid] ) return mid;// >&&<= 换为: >= && <
        else if( a < f[mid] ) r = mid-1;
        else l = mid+1;
    }
}

int LIS(const int *a, const int &n){
    int i, j, size = 1;
    f[0] = a[0]; d[0] = 1;
    for( i=1; i < n; ++i ){
        if( a[i] <= f[0] ) j = 0; // <= 换为: <
        else if( a[i] > f[size-1] ) j = size++;// > 换为: >=
        else j = bsearch(f, size, a[i]);
        f[j] = a[i]; d[i] = j+1;
    }
    return size;
}

int main()
{
    int i, n;
    while( scanf("%d",&n) != EOF ){
    for( i=0; i < n; ++i ) scanf("%d", &a[i]);
        printf("%d\n", LIS(a, n)); // 求最大递增/上升子序列(如果为最大非降子序列,只需把上面的注释部分给与替换)
    }
    return 0;
}
				
时间: 2024-10-08 19:05:11

POJ 2533 Longest Ordered Subsequence (模版LIS)的相关文章

POJ 2533 Longest Ordered Subsequence(LIS模版题)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Accepted: 21120 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

题解报告:poj 2533 Longest Ordered Subsequence(LIS)

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 < ... < iK <= N. For example, seq

POJ 2533 Longest Ordered Subsequence (LIS)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35930   Accepted: 15779 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

[2016-04-01][poj][2533][Longest Ordered Subsequence]

时间:2016-04-01 21:35:02 星期五 题目编号:[2016-04-01][poj][2533][Longest Ordered Subsequence] #include <cstdio> #include <algorithm> using namespace std; int dp[1000 + 10],a[1000 + 10]; int main(){ int n; while(~scanf("%d",&n)){ for(int i

POJ 2533 Longest Ordered Subsequence(裸LIS)

传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61731   Accepted: 27632 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the

POJ 2533 Longest Ordered Subsequence

题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35605   Accepted: 15621 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the

POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K 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, ...,

poj 2533 Longest Ordered Subsequence(LIS)

Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,a2, ..., aN) be any sequence (ai1,ai2, ..., aiK), where 1 <=i1 < i2 < ... < iK <=N. For example, sequence (1

POJ 2533 Longest Ordered Subsequence(dp LIS)

Language: Default Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33986   Accepted: 14892 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric seq

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)