week4 job B

The meaning of problems:
ZJM has four columns A, B, C, D, each column has the number of digits n. ZJM a number taken from each respective column number, he would like to know how many kinds of programs such that the number of 4 and 0.
When a number of columns has the same plurality of numbers, to treat them as a different number.
Please help him!
input:
first line: n (number representative of the number of columns) (1≤n≤4000)
the next n-th row, the i-th row of four numbers, each number of columns A, B, C, D of the i-th digit (digit 2 to the power of not more than 28)
output:
output of the number of different combinations.
INPUT Sample:
. 6
-45 22 is 42 is -16
-41 -27 56 is 30
-36 53 is 77 -37
-36 30 -75 -46
26 is 62 is -38 -10
-32 -54 -6 45
Sample Output:
. 5
Sample Explanation:
(-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46), (- 32, a 30, -75, 77), (- 32, -54, 56, 30)
ideas:
If directly through the four nested loops, then the time complexity is too large, so the use of other methods. Two nested loop, where the sum obtained two series, and in a container, the container and the number stored for this sort. Also two nested loop drawn by two series of added value and find a location of the first and last columns and two opposite front and by the number of two functions, the final result obtained. In two lookup functions, the application binary search to find the position, to find to find the required container capacity value from 0 to the container.
Code:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> temp;
int find1(int am)
{
 am=-am;
 int l=0;
 int r=temp.size();
 int count=-1;
 while(l<=r)
 {
  int mid=(l+r)/2;
  if(temp[mid]==am)
  {
   count=mid;
   r=mid-1;
  }
  else if(temp[mid]>am)
  {
   r=mid-1;
  }
  else
  {
   l=mid+1;
  }
  
 }
 return count;
}
int find2(int am)
{
 am=-am;
 int l=0;
 int r=temp.size();
 int count=-1;
 while(l<=r)
 {
  int mid=(l+r)/2;
  if(temp[mid]==am)
  {
   count=mid;
   l=mid+1;
  }
  else if(temp[mid]>am)
  {
   r=mid-1;
  }
  else
  {
   l=mid+1;
  }
  
 }
 return count;
}
int main()
{
 int n;
 cin>>n;
 int count=0;
 //int a1,a2;
 int a[n],b[n],c[n],d[n];
 for(int i=0;i<n;i++)
 {
  cin>>a[i]>>b[i]>>c[i]>>d[i];
 }
 for(int i=0;i<n;i++)
 {
  for(int j=0;j<n;j++)
  {
   temp.push_back(a[i]+b[j]);
   //a1=a[i]+b[j];
  }
 }
 sort(temp.begin(),temp.end());
 for(int i=0;i<n;i++)
 {
  for(int j=0;j<n;j++)
  {
   int am=c[i]+d[j];
   int num1=find1(am);
   int num2=find2(am);
   if(num1!=-1&&num2!=-1)
   count=count+num2-num1+1;
  }
 }
 cout<<count;
 return 0;
} 
Published 19 original articles · won praise 0 · Views 210

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104983198