UVA 10534 Wavio Sequence xx sequence (first word not recognized)

Source: Internet
Author: User
Tags min

Original question:
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 adjacent integers is same in a wavio sequence.
For example 1, 2, 3, 4, 5, 4, 3, 2, 0 are an wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 are
Not a valid Wavio sequence. In this problem, you'll be given a sequence of integers. You has to find
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 would be ' 9 '.
Input
The input file contains less than the cases. The description of each test case is given below. Input
is terminated by end of file.
Each set is starts with a postive integer, N (1≤n≤10000). In next few lines there'll be N
Integers.
Output
For each set of input print the length of longest wavio sequence in a line.
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
Sample Output
9
9
1
Main topic:
Give you n number, let you find such a 2*k+1 number, the first k+1 number is strictly increment sequence, after K number is strict monotonically descending sequence. Let you find the longest 2*k+1 is how much.
Ideas and spit slots See below code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int N = 10001;
struct nums
{
    int mark;
    int nu;
};
struct flag
{
    int pos, mark;
};
nums num [N], seq [N], rnum [N];
int dp [N], rdp [N];
int n, ans;
int max (int x, int y)
{
    if (x> y)
    return x;
    return y;
}
int min (int x, int y)
{
    if (x <y)
    return x;
    return y;
}
flag binS (nums key, int len)
{
    flag tem;
    int low = 1, hi = len;
    while (low <= hi)
    {
        int mid = (low + hi) >> 1;
        if (key.nu> seq [mid] .nu && key.nu <= seq [mid + 1] .nu)
        {
            tem.mark = seq [mid + 1] .mark;
            tem.pos = mid + 1;
            return tem;
        }
        else
        {
            if (key.nu> seq [mid] .nu)
            low = mid + 1;
            else
            hi = mid-1;
        }
    }
    tem.mark = 1;
    tem.pos = 1;
    return tem;
}
void lis ()
{
    flag index;
    int i, len = 1;
    seq [len] .nu = num [1] .nu;
    seq [len] .mark = 1;
    dp [1] = 1;
    for (i = 2; i <= n; i ++)
    {
        if (seq [len] .nu <num [i] .nu)
        {
            len ++;
            seq [len] .nu = num [i] .nu;
            seq [len] .mark = num [i] .mark;
            dp [num [i] .mark] = len;
        }
        else
        {
            index = binS (num [i], len); // The index returned is the position of the number found and the number
            dp [num [i] .mark] = dp [index.mark];
            seq [index.pos] .nu = num [i] .nu;
            seq [index.pos] .mark = num [i] .mark;
        }
    }
}
int main ()
{
    int des, aes, tem;
    ios :: sync_with_stdio (false);
    while (cin >> n)
    {
        memset (dp, 0, sizeof (dp));
        memset (seq, 0, sizeof (seq));
        memset (rdp, 0, sizeof (rdp));
        ans = des = aes = 0;
        for (int i = 1; i <= n; i ++)
        {
            cin >> num [i] .nu;
            num [i] .mark = i;
            rnum [n-i + 1] .nu = num [i] .nu;
            rnum [n-i + 1] .mark = num [i] .mark;
            if (i> 1)
            {
                if (num [i] .nu-num [i-1] .nu> = 0)
                aes ++;
                else
                des ++;
            }
        }
        if (aes == n-1 || des == n-1 || n == 1) // If it is monotonically increasing or monotonically decreasing or has only one value, output 1
        {
            cout << 1 << endl;
            continue;
        }
        lis ();
        for (int i = 1; i <= n; i ++)
        {
            rdp [i] = dp [i];
            num [i] .nu = rnum [i] .nu;
            num [i] .mark = rnum [n-i + 1] .mark;
        }
        memset (dp, 0, sizeof (dp));
        memset (seq, 0, sizeof (seq));
        lis ();
        for (int i = 1; i <= n; i ++)
        {
            tem = min (rdp [i], dp [n-i + 1]);
            tem = tem * 2-1;
            ans = max (ans, tem);
        }
        cout << ans << endl;
/ * for (int i = 1; i <= n; i ++)
        cout << rdp [i] << "";
        cout << endl;
        for (int i = 1; i <= n; i ++)
        cout << dp [i] << "";
        cout << endl; * /
    }
    return 0;
} 


idea:
I wrote this code really smelly and long, but somehow. Do this problem when UVAOJ, submitted code are only displayed in the decision queue, after two days to finally wait until the results, Nema time ~ ~ ~
First thought, the first look at this question will certainly go to the most classic of the dynamic planning problem, the longest increment sub-sequence to think. OK, think of this, the problem has been made 20%, but there is no egg use. Since it is for you to find the sequence of symmetric lengths that increment and decrement, then the sequence is evaluated in a positive direction for the longest increment subsequence, and then in reverse. Results saved to dp[] and rdp[], Dp[i] represents the longest increment subsequence that can be found at the end of the number of I, rdp[]. Then enumerate each number in the original sequence I,dp[i] represents the length of the longest increment subsequence from left to I as a,rdp[i] representing the length of the longest increment subsequence from right to left to I is recorded as B. Each time you find A and B two minimum as the results are saved to the TEM, equivalent to find half of the result sequence (do it yourself), and then multiply by 2 minus 1 (in the middle of the number of enumerated count once) saved to ans, the next time to find Min (A, a, b) to determine whether the new tem is more than ans , if large, is updated.
as shown in figure:

In this way, the first time the code has been made, but timed out. I can't think of any other way, but I know a nlogn algorithm that asks for the longest increment subsequence, so I'm going to take it. Here the disgusting place also comes out, the NLOGN algorithm inside has two array, one is the original sequence number num[], the other one is seqp[], this seq inside holds an ordered sequence, Each newly found element will find its location in this sequence (Baidu Nlogn the longest common subsequence), and does not save anything like dp[i], so add a dp[i] to record it. For example, to find the current element I, requires dp[i] value, first determine if the first value is not greater than i-1 value, if it is dp[i]= a length of +1 (that is, the increased Len), if not, then in the SEQ binary find the position of the element I, specifically find is equal to Num[i] Or the number of the first greater than num[i], this number corresponds to the DP value of num[i] corresponding to the DP value. So down, two times Nlogn algorithm solved ~ ~
The first two commits did not turn off the standard input and output stream, so the speed is a bit slow. Faster after closing.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.