Two interesting questions related to sequence

Source: Internet
Author: User

(4) The absolute value of the two numbers found in the array is minimized

Does it look like 2-sum? Not much to explain. Mainly is the absolute value big move can. It's a good way to sweep both ends!

Of course first sort, go out sort is O (n). Count on sort words degenerate to O (NLOGN)

This is also the problem of codility, before the time to tidy up.

Previous code:

You can also with includes, for example://#include <algorithm> #include <algorithm>int ab (int x) {    retur N (x >= 0)?

X: (-X);} int f (int x,int y) { return ab (x + y);} int solution (vector<int> &a) { //write your code in c++98 vector<int> a = A; Sort (A.begin (), A.end ()); int answer = f (a[0],a[0]); for (int i = 0, j = a.size ()-1; I <= J;) { answer = min (answer, F (a[i], a[j])); if (AB (a[i) > AB (A[j])) { ++i; } else { --j; } } return answer;}


(5) A non-negative real array is given. has been ordered in accordance with non-descending order.

Set the array to C. The logarithm of the subscript length is N,0≤p < Q < N and C[p] * C[q]≥c[p] + c[q].

The question on codility is this definition of c[i] = A[i] + b[i]/10^6,a is the integer part [0..1000], and B is the numerator of the fractional part [0..999999] (the denominator of fractional portions is 1000000). Time Complexity of O (N)

The problem is actually analysis to be careful, again emphasizing that several numbers do not necessarily enumerate. We're just going to calculate a * b >= a + b because it's all positive. Again by symmetry we only consider the situation of 0<=a<=b.

Consider a larger number B possible scenario

(1) b = = 0 only A = = 0 to meet the conditions

(2) 0 < b < 2 no solution

(3) B >= 2 b/(b-1) <= a <= b


Note that assuming we are from small to large enumeration B, the condition (3) that b/(b-1) = 1/(1-1/b) is monotonically reduced, so for larger B. When we consider a, the previous legal A is also legal. This is the key to O (N), and we just need to record the last valid position of a.

On the 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;const int M = 1000000000;const int W = 1000000;long Long cmp (long   Long X1,long long y1, long long x2, long long y2) {//X1/y1-x2/y2 return x1 * y2-x2 * y1; }int solution (vector<int> &a, vector<int> &b) {//write your code in C++11/* Let A < = b A * b >= A + b b = = 0 A = = 0 0 < b < 2 no solution B >= 2 b/(b-1)    <= a <= b */int n = a.size (), NUM0 = 0, last = N, answer = 0;        for (int i = 0; i < n; ++i) {if ((a[i] = = 0) && (b[i] = = 0)) {//B = = 0 Answer + = num0++;            } else if (A[i] >= 2) {//b >= 2 if (last >= n) {last = i-1;            } int x = a[i] * W + b[i], y = x-w; for (; (last >= 0) && (CMP (a[last] * W + b[last],w, x, y) >= 0);            --last);        Answer + = i-1-last;        } if (answer >= m) {return m;            }//printf ("%d%d\n", I, answer);        } return answer; }




Two interesting questions related to sequence

Related Article

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.