UVa 10534 - Wavio Sequence (简单DP 最长上升下降子序列)

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

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

 


Problemsetter: Md. Kamruzzaman, Member of Elite Problemsetters' Panel



题意:

给定长度为n的整数序列,求一个最长子序列(不一定连续),使得该子序列的长度为奇数(2*k-1),前k个数严格递增,后k个数严格递减。


先求出以每个点为终点的最长上升和下降子序列,然后以该点为中心的波浪序列长度就是min(inc[i], dec[i]) * 2 - 1



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 10000 + 20;
int A[maxn];
int deca[maxn];
int inca[maxn];
int g[maxn];

int main() {
    int n;

    while(scanf("%d", &n) != EOF) {
        for(int i=0; i<n; i++) scanf("%d", &A[i]);
        memset(g, 0x3f, sizeof(g));
        for(int i=0; i<n; i++) {
            int t = lower_bound(g+1, g+n+1, A[i]) - g;
            inca[i] = t;
            g[t] = A[i];
        }
        memset(g, 0x3f, sizeof(g));
        for(int i=n-1; i>=0; i--) {
            int t = lower_bound(g+1, g+n+1, A[i]) - g;
            deca[i] = t;
            g[t] = A[i];
        }
        int ans = 0;
        for(int i=0; i<n; i++) {
            ans = max(ans, min(deca[i], inca[i]) * 2 - 1);
        }
        printf("%d\n", ans);
    }

    return 0;
}



以下是用c语言实现二分查找求最长下降子序列长度和数列的代码: ``` #include <stdio.h> #include <stdlib.h> int binary_search(int *nums, int len, int target) { int l = 0, r = len - 1; while (l <= r) { int mid = l + (r - l) / 2; if (nums[mid] < target) { l = mid + 1; } else { r = mid - 1; } } return l; } int longestIncreasingSubsequence(int *nums, int len, int *resArr) { int *tails = (int*)malloc(len * sizeof(int)); memset(tails, 0, len * sizeof(int)); int lenTails = 1; tails[0] = nums[0]; resArr[0] = 1; for (int i = 1; i < len; i++) { if (nums[i] > tails[lenTails - 1]) { tails[lenTails++] = nums[i]; resArr[i] = lenTails; } else { int index = binary_search(tails, lenTails, nums[i]); tails[index] = nums[i]; resArr[i] = index + 1; } } free(tails); return lenTails; } int main() { int nums[] = {1, 3, 5, 7, 9, 2, 4, 6, 8}; int len = sizeof(nums) / sizeof(int); int *resArr = (int*)malloc(len * sizeof(int)); int LIS_len = longestIncreasingSubsequence(nums, len, resArr); printf("LIS length: %d\n", LIS_len); printf("LIS sequence: "); for (int i = 0; i < len; i++) { if (resArr[i] == LIS_len) { printf("%d ", nums[i]); LIS_len--; } } printf("\n"); free(resArr); return 0; } ``` 以上程序会输出最长下降子序列的长度及其子序列。其中,`longestIncreasingSubsequence`函数返回最长下降子序列的长度,`resArr`存储每个位置的元素在子序列中的位置(方便输出子序列),`binary_search`函数是二分查找实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值