UVA-12002 Happy Birthday

Source: Internet
Author: User

Description


Happy Birthday

Today it ' s February 13th. It ' s a very special day:miguel ' s birthday! Like every year, he's organised a big celebration for all his friends. He prepared a succulent dinner at the He House. Everyone had a lot of fun and the celebration is a complete success.

Now it's time for the Not-so-funny cleaning up. He wants to start with moving all the dishes from the table to the kitchen. Since he ' s been going to the gym lately, he feels strong enough to pile and carry at once as many dishes as he wants. Time doesn ' t go unnoticed though:he ' s not as agile as and he used to is, so he can only carry the stack of dishes if it ' s COM Pletely stable. A stack of dishes is stable if each dish on the stack are bigger or equal in size than all the dishes above it. If the stack wasn ' t stable he would drop the dishes and would has even more things to clean!

At the beginning of the scene, Miguel are empty-handed in one side of the table, walks along the table finding and maybe Co Llecting dishes of different sizes until he reaches the other side, and then brings the collected dishes to the kitchen. When he finds a dish, he can:

    • Ignore the dish.
    • If he has an empty hands, pick up the dish.
    • If he has a stack of dishes in his hands, pile the dish on top of the stack.
    • If he has a stack of dishes in his hands, put the stack on top of the dish and then pick up the new stack (including the dish ).

Miguel is tired, so he wants to clean up the table as soon as possible. He ' d like-to-take as many dishes as he can in each run, but he's exhausted from the party and can ' t think clearly. He ' s asked you-to-find out-what-the-maximum number of dishes he can collect in a single run is.

Input

Input contains several datasets. Each dataset consists on the lines. The first line of each dataset contains a integern ( 1N), the number of dishes on the table . The second line of each dataset contains N integers, k1 ... KN (1ki1000), specifying the size of each dish he finds, in the same order he finds them. Input ends with a dataset withN = 0. This case shouldn ' t is processed.

Output

For each dataset, print the maximum number of dishes Miguel can collect in a single run.

Sample Input
63 1 5 6 2 463 4 2 5 2 60
Sample Output
46 Test Instructions: There are n plates, select the past, you can choose or not, you can also put on the top of the heap or below, but must meet the above plate is less than equal to the plate below, the longest is how many ideas: first we do the LIS and LDS from behind, and then we can think if we start from the first I take, Then we can be bigger than its plate J after encountering it, then we need to look for the larger as the starting point of the LIS, plus the small LDS, when encountered in small cases, the opposite. The simple thing is to make the big bigger, the smaller, the first to do the wrong thing.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 1005;int LIS[MAXN], Lds[maxn];int N, num[maxn];int main () {while (scanf ("%d", &n)! = EOF &&am P N) {memset (LIS, 0, sizeof (LIS)), memset (LDS, 0, sizeof (LDS)), for (int i = 1; I <= n; i++) scanf ("%d", &num[i]); int i = n; I >= 1; i--) {Lis[i] = lds[i] = 1;for (int j = n; j > i; j--) {if (Num[i] >= num[j]) lds[i] = max (Lds[i], lds[j] + 1); if (num [i] <= num[j]) lis[i] = max (Lis[i], lis[j] + 1);}} int ans = -1;for (int i = 1; I <= n; i++) {ans = max (ans, Max (lis[i], lds[i])); for (int j = i+1; J <= N; j + +) {if (n UM[J] < Num[i]) ans = max (ans, lis[i] + lds[j]), else if (Num[j] > Num[i]) ans = max (ans, lds[i] + lis[j]);}} printf ("%d\n", ans);} return 0;}


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.