Problems with codility (35) neon 2014

Source: Internet
Author: User

It's also an interesting question. More and more mathematical ...... Not good at doing this.


A pier has n wooden piles which are used to hold the ship. The pier length is m, which can be understood as a line segment between 0 and M. There are n tuning vessels, each of which is half the length of X, so the captain is 2 X. The center of each ship must be tied to a wooden pile. And each pile can only be tied to one boat. The length of the rope is the distance between the center of the ship and the position of the pile. Of course, the position of the stake cannot be moved, but the ship can be moved freely. The stern of the ship must be on the dock (0 .. (m), the ship can also be considered as a line segment with a length of 2 x, please specify a position for each ship to make the longest rope length of the boat shortest, find the shortest length of the rope. If it cannot accommodate all ships, output-1.

The function header is:int solution(vector<int> &R, int X, int M)

The length of R is N, which indicates the position of the pile. The position of the pile is not decreasing, X is the half captain, and m is the length of the pier.

Data range: N is [1 .. 100000], M and X are [1 .. 1000000000].

Time complexity O (N) and space complexity O (n) are required ).

Analysis: if the total length of all ships exceeds m, there is obviously no solution. Of course, division is used to prevent overflow, but multiplication seems okay. There must be some solutions for the rest. This question can also be done in two points, but it cannot meet the required complexity.

At first, I felt very confused, and the code I wrote was also very messy. Later, I sorted out the following ideas:

(1) first move the ship to the leftmost, so that the center position of the ship I (starting from 0) is C [I] (2 * I + 1) * x

(2) Now let's see how to move the ship to the right, from left to right. If the ship I moves to the right, all the ships in the back will move to the right.

(3) Step (2) the key is to see how much each ship moves, because all the ships on the right move together, as long as they record all the movements of the ship, therefore, the center position of ship I is C [I] '= C [I]-Move, mainly depending on the minimum value of the ship on the right (minimum suffix ), the minimum value is reduced by less, that is, the average value of the current ship location and all the minimum values on the right is used as the moving distance. Specifically, if all C' values are negative at this time, you do not need to do so. That is, when the current ship C is positive, the Cx 'of the later ships "booked" the distance that the ship should pull to the right (c' + cx ')/2, as long as it is not the value, it is not the best, but the current ship can only meet one, and cannot pull too much to the right (because other ships have the opportunity to pull to the right, it is useless to pull the current ship to the right ), so we chose the smallest Cx '. One reason is that, if you pull more, the minimum value will become worse. -- this is because you can only pull to the right. The current ship's C' will eventually be positive, and the Minimum Ship's C' will eventually become negative, rado will be worse, pulling the C of the current ship is bigger than it is now ......

Code:

// you can use includes, for example:// #include <algorithm>// you can write to stdout for debugging purposes, e.g.// cout << "this is a debug message" << endl;int center(int X,int i) {    return ((i << 1) | 1) * X;}int solution(vector<int> &R, int X, int M) {    // write your code in C++11    int n = R.size();    if ((M / (X << 1)) < n) {        return -1;    }    vector<int> mini(n);    for (int i = n - 1; i >= 0; --i) {        mini[i] = R[i] - center(X, i);        if (i + 1 < n) {            mini[i] = min(mini[i], mini[i + 1]);        }    }    int answer = 0, move = 0;    for (int i = 0; i < n; ++i) {        int can = ((R[i] - center(X, i) - move) + (mini[i] - move)) / 2;        if (can > 0) {            move += min(can, M - X - center(X, n - 1) - move);        }        answer = max(answer, abs(R[i] - center(X, i) - move));     }    return answer;}




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.