24h購物| | PChome| 登入
2012-04-12 08:18:53| 人氣2,339| 回應0 | 上一篇 | 下一篇

[UVA][LIS&LDS] 10534 - Wavio Sequence

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D
Wavio Sequence
Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

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 sequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in a Wavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.


Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.

 

Input

The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.

 

Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.

 

Output

For each set of input print the length of longest wavio sequence in a line.

Sample Input                                   Output for Sample Input

10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
 
9
9
1

 


做法 : O(nlogn) 的 LIS LDS

做出來後接起來比較即可

#include <stdio.h>
#include <stdlib.h>

void findLIS(int A[], int LIS[], int n) {
    int pos[10000] = {};
    int i, j, m, l, r, newSet;
    int L = -1;
    for(i = 0; i < n; i++) {
        l = 0, r = L, newSet = -1;
        while(l <= r) {
            m = (l+r)/2;
            if(pos[m] < A[i]) {
                if(m == L || pos[m+1] >= A[i]) {
                    newSet = m+1;
                    break;
                } else
                    l = m+1;
            } else
                r = m-1;
        }
        if(newSet == -1)
            newSet = 0;
        pos[newSet] = A[i];
        LIS[i] = newSet+1;
        if(L < newSet)
            L = newSet;
    }
}
int main() {
    int n, i, j, tmp;
    int A[10000], LIS[10000], LDS[10000];
    while(scanf("%d", &n) == 1) {
        for(i = 0; i < n; i++)
            scanf("%d", &A[i]);
        findLIS(A, LIS, n);
        for(i = 0, j = n-1; i < j; i++, j--)
            tmp = A[i], A[i] = A[j], A[j] = tmp;
        findLIS(A, LDS, n);
        for(i = 0, j = n-1; i < j; i++, j--)
            tmp = LDS[i], LDS[i] = LDS[j], LDS[j] = tmp;
        int min, ans = 0;
        for(i = 0; i < n; i++) {
            min = LIS[i] < LDS[i] ? LIS[i] : LDS[i];
            min = min*2-1;
            if(min > ans)
                ans = min;
        }
        printf("%d\n", ans);
    }
    return 0;
}

台長: Morris
人氣(2,339) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 270 - Lining Up
此分類上一篇:[UVA][Math] 10642 - Can You Solve It?

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文