NYOJ 214 monotonic incrementing subsequence

Source: Internet
Author: User

Description
Given an integer series {a1, a2..., an} (0 <n <= 100000), locate the monotonic incrementing oldest sequence and obtain its length.

For example, the longest monotonic increasing subsequence of 1 9 10 5 11 2 13 is 1 9 10 11 13 with a length of 5.

Input
Multiple groups of test data (<= 7)
The first row of each group of test data is an integer n, indicating a total of n integers in the sequence, followed by n integers in the next row, indicating all elements in the series. separate each integer with spaces (0 <n <= 100000 ).
Data ends with EOF.
The input data must be valid (all are int integers )!
Output
For each group of test data, the length of the output integer sequence is the longest incrementing sub-sequence. Each output occupies one row.
Sample Input
7
1 9 10 5 11 2 13
2
2-1 sample output
5
1 At the beginning of thought and "monotonic incrementing sub sequence a" http://blog.csdn.net/xiangguangde/article/details/8824114
Almost, it's just that the data is big. I feel that if I use that method again, it will time out, but I am lucky. Try it and the result is decisive;
Later, after the guidance of my teammates, I stored the sequence in an ordered array, and the binary search finally reached the AC. During this period, I made a small mistake and RE again, because
I have not noticed that there will be a negative number in the question. It is not enough to initialize the f array to 0.
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <cstdlib>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <cmath>
# Include <map>
# Define FLAG 0
# Define M 100000 + 10
Using namespace std;
Int a [M];
Int f [M];
 
Void midfs (int beg, int end, int n)
{
Int mid = (beg + end)/2;
If (f [mid] = n) return;
If (beg = end) {f [end] = n; return ;}
If (n> f [mid])
{
Midfs (mid + 1, end, n );
}
Else midfs (beg, mid, n );
}
 
Int main ()
{
# If (FLAG)
Freopen ("in.txt", "r", stdin );
// Freopen ("out.txt", "w", stdout );
# Endif
Int n, I, j, flag;
While (cin> n)
{
Flag = 1;
Memset (f, 0, sizeof (f ));
F [0] = 1 <31; // It is the smallest negative number. At first, because f [0] is not initialized, it is the smallest and RE.
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & a [I]); // if the first number is negative after f is initialized to 0
If (a [I]> f [flag-1]) f [flag ++] = a [I]; // This sentence will not be executed. The second sentence is entered, then the endless loop!
Else
{
Midfs (1, flag-1, a [I]);
}
}
Printf ("% d \ n", flag-1 );
}
 
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.