American group reviews 2017 Spring Recruit Programming Questions _ American Group

Source: Internet
Author: User

The programming question has the question number, should be randomly extracted. The main effect of the topic

Given a sequence, the length of its longest ascending subsequence is obtained. Idea-DP

Very simple DP, first to do the programming questions, the first quickly wrote an O (N2) solution then to do the previous question. Set Dp[i] Represents the length of the longest ascending sequence ending with the number I, and the state transition equation is: Dp[i]=max (Dp[j]) +1,j<i&&num[j]<num[i]

All done, found that there are half an hour, began to think O (NLOGN) solution, although I have forgotten how to write it, I remember that in the process of processing, the length of the ascending subsequence is subscript, and the last number of ascending subsequence is the value (same length, smaller value), and a monotonically increasing array can be constructed. Then you can use a second to find the length of the longest ascending subsequence of the last number that is less than the current number.

After proficiency, you don't need a DP array to code

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n, NUM, L, R, Mid, MX;
int Mn[100003];//mn[i] Represents the minimum value

int main () {
    while (1 = scanf ("%d", &n)) in the ascending subsequence of length i {
        mx = 0;
        Memset (MN, 0x3f, sizeof (MN));
        while (n--> 0) {
            scanf ("%d", &num);
            L = 1;
            r = mx;
            while (L <= R) {
                mid = (L + r) >> 1;
                if (Mn[mid] < num) {
                    L = mid + 1;
                }
                else {
                    r = mid-1;
                }
            }
            Mn[r + 1] = min (mn[r + 1], num);
            mx = max (mx, r + 1);
        }
        printf ("%d\n", MX);
    }
    return 0;
}

American group Reviews also Youdao reading program questions, gave an activity to create and destroy functions, create a function in the file read, but did not close the input stream. Only found this one error ...

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.