Today's headline 2018 campus recruitment back-end development engineer (second batch) programming questions-Problem solving

Source: Internet
Author: User
Tags requires

Have done the third batch of topics, today headline 2018 Campus recruitment back-end development engineer (third batch) programming questions-the problem. The topic of this field is biased by techniques and algorithms, while the third instalment of the problem-biased coding. The algorithm involved has two-part search and interval dynamic programming.

link to the original topic: click here. first question: User Preferences Title:

To continually optimize recommendations, today's headlines store and process massive amounts of data every day. Suppose there is a scenario: we have users in accordance with their registration time to label, for a class of articles, each user has a different preferences, we would like to know a certain period of time registered users (label connected to a number of users), how many users of this kind of article preferences value K K K. For some special reasons, a user interval that does not appear for a query completely overwrites the user interval of another query (does not exist l1<=l2<=r2<=r1 L 1 <= L 2 <= r 2 <= R 1 l_1). Input:

The 1th behavior n represents the number of users 2nd behavior n integers, the first I represent user marking I of the user's preference for a certain kind of article 3rd behavior a positive integer q represents the number of groups queried, 4th rows to (3+Q) lines, each row contains 3 integers l,r,k represents a set of queries, that is, labeled l<=i<= The number of users in R who prefer this type of article to a value of K. Data range n<=300000,q<=300000 n <= 300000, Q <= 300000 N, K is integral type. Output:

A total of Q lines, one integer per line representing the number of users who prefer the value K. Sample Input:

5
1 2 3 3 5
3
1 2 1
2 4 5
3 5 3
Sample output:
1
0
2
parsing

The data is large, asked 300,000 times, then each time the corresponding operation must be asked for the complexity of an O (1) O (1) O (1) or O (logn) O (l O g N) o (LOGN);

This gives us the direction to think about the algorithm, O (1) O (1) O (1) is obviously not possible, then the operation of the time Complexity O (LOGN) O (l O g N) O (logn) algorithm can only be binary search, so you from this time complexity to the binary search, then the problem you are poor Not much has been done;

The requirements of the two points are ordered sequentially, so no matter how many, first sort, but according to what sort of things. Because the topic requires in a time range of how many people like K, then you can put the same K-value of people together to form a sub-sequence, and then based on the range of time in this sub-sequence, so the structure of the two-level sorting, the size of the K-value is sorted in ascending order, if the K value is the same, and

BOOL sort_cmp (const pair<int, int> &a, const pair<int, int> &b)
{
    return a.first = = B.first? A.second < b.second:
        A.first < B.first;
}

This sort of level two is a stable sort, so the whole sequence after sorting is the K value ascending, and the time in each subsequence is ascending.

After ordering, is the process of finding, first use Equal_range to find the sequence of K value for the target K value of the sub-sequence, and then use Lower_bound and Upper_bound in the sub-sequence to find the target time range of the eldest son sequence (with Lower_ Bound find the first position greater than or equal to the left endpoint, use Upper_bound to find the last position less than or equal to the right endpoint), such as the target time range is [3, 7], then assume the maximum subsequence is [4, 5], [4,5]⊂[3,7] [4, 5]⊂[3, 7] [ 4, 5]\subset[3, 7], the answer is the length of the oldest sequence.

If this process is still unclear, look at the following picture:

The time complexity of the three functions of Equal_range, Lower_bound, Upper_bound is O (logn) O (l O g N) O (logn), so the problem is resolved.

If you are not familiar with these three functions, then you can hand-write a two-part to find the upper bound, a two-point for the lower bound, in particular, you can refer to the two points you really understand how to write-two points in the wording of the explanation. Code

#include <bits/stdc++.h> using namespace std; BOOL sort_cmp (const pair<int, int> &a, const pair<int, int> &b) {return A.first = = B.first?
A.second < B.second:a.first < B.first; } struct FIND_FIRST_CMP {bool operator () (const pair<int, int> &p, int k) const {return p.firs
    T < K;
    } bool Operator () (int k, const pair<int, int> &p) const {return K < P.first;

}
};  struct FIND_SECOND_CMP {bool operator () (const pair<int, int> &p, int k) const {return P.second
    < K;
    } bool Operator () (int k, const pair<int, int> &p) const {return K < P.second;

}
};
    int main () {int n, q;
        while (EOF! = scanf ("%d", &n)) {vector<pair<int, int> > arr;

        for (int i = 0, x; i < n; Cin >> x, Arr.emplace_back (x, ++i)) {} sort (Arr.begin (), Arr.end (), sort_cmp); for (scanF ("%d", &q); q--;)
            {int L, R, K;
            scanf ("%d%d%d", &l, &r, &k);
                Pair<vector<pair<int, int> >::iterator, vector<pair<int, int> >::iterator> sd =
            Equal_range (Arr.begin (), Arr.end (), K, find_first_cmp{}); printf ("%d\n", Upper_bound (Sd.first, Sd.second, R, find_second_cmp{})-Lower_bound (Sd.first, Sd.second, L
        , find_second_cmp{}));
}} return 0;
 }
second question: Hand strings Title:

As a string artist, a gold master ordered a string containing n variegated beads-each bead is either colorless or painted in several colors. In order to make the color of the hand string look less monotonous, the golden master requires that any color on the hand string (not containing colorless), in any continuous m-beads to appear at most once (note that the hand string is a ring). The color on the hand string is a total of c. Now tell you in a clockwise order what color each bead contains, with a string of n beads. Please determine how many colors on the hand string do not meet the requirements. That is, ask how many colors appear at least two times in any consecutive m beads. Input:

The first line enters n,m,c three numbers, separated by a space. (1<=n<=10000,1<=m<=1000

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.