4 Values whose Sum is 0 (binary sorting +)

Face questions

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2  28 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
 
 
Meaning of the questions:
To a matrix of n * 4, the number of inputs n * 4, to find a number in each column, and such that the number four is 0;
 
analysis:
The first two are obtained and any two of a number of b, c and d, and stored in the respective array, cd and after sorting, and then to find dichotomy; binary search when the note, if the intermediate data eligible to find something to go down on both sides, because the situation can not be ruled out multiple digital equivalent of
 
note:
When seeking second set of data, according to a result there is no need to initialize submitted to total;
answer:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=4005;
int a[N],b[N],c[N],d[N];
int ab[N*N],cd[N*N];
int main()
{
   int n,total=0,i,j;
   while (cin>>n)
   {
         for (i=0;i<n;i++)
           cin>>a[i]>>b[i]>>c[i]>>d[i];
           int num1=0,num2=0;
         for (i=0;i<n;i++)
            for (j=0;j<n;j++)
           {
                ab[num1++]=a[i]+b[j];
                cd[num2++]=-(c[i]+d[j]);
           }
          sort (cd,cd+num2);
         for (i=0;i<num1;i++)
           {
              int mid,up=num2-1,low=0;
             while (low<=up)
             {
                mid=low+(up-low)/2;
               if (ab[i]==cd[mid])
                {
                  total++;
                    for (j=mid+1;j<=up;j++)
                  {

                       if (ab[i]==cd[j])
                          total++;
                       else
                           break;
                  }
                  for (j=mid-1;j>=low;j--)
                  {
                       if (ab[i]==cd[j])
                         total++;
                       else
                         break;
                  }


                  break;
              }
              else
              {
                  if (ab[i]>cd[mid])
                    low=mid+1;
                  else
                    up=mid-1;
              }
        }
    }
      cout << total << endl;
   }

        return 0;
}

Reprinted from https://www.cnblogs.com/lisijie/p/7289457.html

Guess you like

Origin www.cnblogs.com/xxxsans/p/12641545.html