Algorithm title: UVA 10534 Wavio Sequence (DP + LIS)

Source: Internet
Author: User

Wavio is a sequence of integers. It has some interesting properties.

· Wavio is of odd length i.e. L = 2*n + 1.

· The "n+1" integers of Wavio sequence makes a strictly increasing.

· The last (n+1) integers of Wavio sequence makes a strictly decreasing.

· No two adjacent integers are 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 are have to find the length of the longest Wavio sequence which is a subsequence of the given. Consider, the given sequence as:

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

Here's 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 test cases. The description of each test case is given Below:input was terminated by end of.

Each set starts with a postive integer, N (1<=n<=10000). In next few lines there would 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

Problemsetter:md. Kamruzzaman, member of the Elite problemsetters ' Panel

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.