POJ-2785 4 Values whose Sum is 0 (binary enumeration sort + dichotomy)

Source: Internet
Author: User

Title Link:http://poj.org/problem?id=2785

Test instructions is giving you 4 numbers. To take a number from each column, make the sum of four number 0, and find out the number of cases of such a combination.

When one of the series has multiple identical numbers, consider them as different numbers.

The practice is to put the first two columns and values in an array (a), the next two columns and the existence of another array (B), the array is n^2. Then the B array is sort, the a array is traversed, and the second finds the number of elements in the B array and the array of a and 0. One of the points to note is that in case the A array is all 0, and the B array is 0 (there are other cases), the two points can only find a match. So it is possible to find exactly the number that matches the number, subtract their position, and add up, that's the answer.

Here can use two functions Lower_bound () and Upper_bound (), all using two points to find, the previous function is to return an array of greater than or equal to a number of positions, followed by the return is greater than this number of positions (can not understand Google or Baidu How to use these two functions).

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 Const intMAXN =4010;7typedefLong LongLL;8 9 LL A[MAXN], B[MAXN], C[MAXN], D[MAXN];TenLL NUM1[MAXN * MAXN], NUM2[MAXN *MAXN]; One  A intMain () - { -     intN; the      while(~SCANF ("%d", &N)) { -          for(inti =0; I < n; i++) { -scanf"%lld%lld%lld%lld", A + I, B + i, C + i, D +i); -         } +         intf =0; -          for(inti =0; I < n; i++) { +              for(intj =0; J < N; J + +) { ANUM1[F] = A[i] +B[j]; atnum2[f++] = C[i] +D[j]; -             } -         } -LL res =0; -         inttemp =0; -Sort (num2, num2 +f); in          for(inti =0; i < F; i++) { -temp = Lower_bound (num2, num2 + F,-num1[i])-num2; to             if(Temp < F && Num2[temp] + num1[i] = =0) { +Res + = Upper_bound (num2, num2 + F,-num1[i])-num2-temp; -             } the         } *cout << Res <<Endl; $     }Panax Notoginseng}

POJ-2785 4 Values whose Sum is 0 (binary enumeration sort + dichotomy)

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.