The second chapter on the Computer Experiment report

Source: Internet
Author: User

1. Practical Topics:

Median number of two ordered sequences

2. Title Description:

Knowing that there are two equal-length non-descending sequence S1, S2, the design function S1 and S2 The median of the set. Ordered sequenceA?0??,A?1??,?,A?N?1?? The median digit of theA?(N?1)/2?? Value, that is, the first? (n+1) /2 number ( a ? 0 ?? is the 1th number).

Input format:

The input is divided into three lines. The first line gives the public length of the sequence N (0<n≤100000), followed by each line entering a sequence of information, i.e. n non-descending integers. The number is separated by a space.

Output format:

Outputs the median of the sequence of two input sequences in a row.

Input Sample 1:
51 3 5 7 92 3 4 5 6
Output Example 1:
4
Input Sample 2:
6-100 -10 1 1 1 1-50 0 2 3 4 5
Output Example 2:

1

3. Algorithm Description:

Using the idea of dichotomy, first to find the median of each sequence a[m1],b[m2], compare the size of a[m1],b[m2], if A[M1]==B[M2], you can determine the median of the merge sequence is a[m1], because A[M1] is greater than a[m1] in front of the element, less than a[ M1] The element that follows, B[M2] is greater than b[m2], and the element after b[m2], and a[m1] is equal to b[m2], you can determine that A[M1] is in the middle of the merge sequence. If A[M1]<B[M2], in two cases: if each sequence is an odd number, then Start1=m1,end2=mid2 (before the big take, after the small take), if it is an even number, then start1=m1+1,end2=m2. If A[M1]>B[M2], also divided into two cases, the same is large before, after the small take, odd: end1=m1,start2=m2; even: end1=m1,end2=m2+1. As long as the start1<end1&&start3<end2, it has been approximated by the dichotomy, until only two numbers are left, the smaller number is the median.

Code implementation:

#include <iostream>
using namespace std;
int m_search (int a[], int b[], int n)
{
int start1 = 0, End1 = n-1, m1, Start2 = 0, End2 = n-1, m2;

while (start1! = End1 | | Start2! = end2)
{
M1 = (Start1 + end1)/2;
M2 = (Start2 + end2)/2;
if (a[m1] = = b[m2])
return A[M1];

if (a[m1]<b[m2])
{
if ((Start1 + end1)% 2 = = 0)
{
Start1 = m1;
End2 = m2;
}
Else
{
Start1 = m1 + 1;
End2 = m2;
}
}
Else
{
if ((Start2 + end2)% 2 = = 0)
{
END1 = m1;
Start2 = m2;
}
Else
{
END1 = m1;
Start2 = m2 + 1;
}
}
}
return A[start1]<b[start2]? A[START1]: B[start2];
}
int main () {
int a[100001],b[100001];
int n;
Cin>>n;
for (int i=0;i<n;i++)
{
cin >>a[i];
}
for (int i=0;i<n;i++)
{
Cin>>b[i];
}
Cout<<m_search (A, B, N);
return 0;
}

4, time complexity and spatial complexity analysis:

Time complexity: The time complexity of the dichotomy is O (logn)

Space complexity: O (1)

5, Experience:

When I first saw the problem, I thought I was going to start by putting two sequences in order and then looking for the median of the sorted array, that is, the fast-track, and then I found that the time complexity of this approach was O (NLOGN). Then think of just learn the dichotomy, but in the case of odd and even card for a long time, and finally slowly try to come to the conclusion.

The second chapter on the Computer Experiment report

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.