C++ Count Inversions Problem Description Inversion Count for an array indicates
ID: 3698499 • Letter: C
Question
C++ Count Inversions
Problem Description
Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j .
Example: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
Requirement
- Design should be based on divide and conquer.
- Running time should NOT be worse than theta(n logn)
- Must use recursion to solve the subproblems.
- Do not use fopen, use C++ file IO instead.
Input Specification
Your program should read from an input file, which starts with a line indicating the number of test cases. Each of the following lines indicates a test case, and each test case includes a sequence of numbers (separated by spaces) for which you need to count inversions.
Sample Input (input.txt)
3
2 4 1 3 5
1 2 4 8 9 3 5 6
1 2 0 6 4 5
Output Specification
For each test case, print one line with a format of The sequence has ? inversions in console.
Sample Output
Below is the correct output for the previous sample input:
The sequence has 3 inversions.
The sequence has 7 inversions.
The sequence has 5 inversions.
Explanation / Answer
C++ count Inversion :-
Divide and Conquer:-
we are given an array of n points in the plane, and the problem is to find the closest pair of point in the array
Algorithm:-
1) find the middle point in the sorted array, we can take p[n/2] as middle point.
2) Divide the given array into two halves. The first subarray contains points from p[0] to p[n/2]. The second subarray contains points from p[n/2+1] to p[n-1].
3) Recursively find the smallest distances in both subarrays
4) From above 3 steps, we have an upper bound d of minimum distance. Now need to consider the pairs such that one point in the pair is from left half and other is from right half.
5) sort the array strip[] according to y coordinates. This step is O(nLogn). it can be optimized to O(n) by recursively sorting and merging.
6) Find the smallest distance in the strip[], This is tricky, From first look, it seems to the O(n^2) step, but it is actually O(n).
Code:
#include<iostream.h>
#include<float.h>
#include<stdlib.h>
#include<math.h>
struct point
{
int x,y;
}
int compareX(const void* x, const void* y)
{
point *p1 =(point *)a, *p2=(point *)b;
return (p1->x - p2->x);
}
int compareY(const void* x, const void* y)
{
point *p1 =(point *)a, *p2=(point *)b;
return (p1->y - p2->y);
}
float dist(point p1, point p2)
{
return((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
float bruteForce(point p[],int n)
{
float min=FLT_MAX;
for(int i=0; i<n; ++i)
for(int i=i+1; j<n; ++j)
if(dist(p[i],p[j]) < min)
min= dist(p[i],p[j]);
return min;
}
float min(float x, float y)
{
return (x<y)?x:y;
}
float stripClosest(point strip[], int size, float d)
{
float min =d;
qsort(strip, size, sizeof(point), compareY)
for(int i=0; i<size; ++i)
for(int j= i=1; j<size && (strip[j]).y-strip[i].y <min; ++j)
if(dist(strip[i],strip[j] < min))
min = dist(strip[i],strip[j])
return min;
}
// Recursive function
float closestUtil(poin p[], int n)
{
if(n <= 3)
return bruteForce(p,n);
int mid =n/2;
point midpoint =p[mid];
float dl=closestUtil(p, mid);
float dr=closestUtil(p+mid, n-mid);
float d =mid(dl,dr);
point strip[n];
int j=0;
for(int i=0; i<n; i++)
if(absp[i].x -midPoint.x <d)
strip[j]=p[i],j++;
return min(d,stripClosest(strip, j,d));
}
float closest(point p[], int n)
{
qsort(p,n,sixeof(point),compareX);
return closestUtil(p,n);
}
int main()
{
point p[]= {{3},{2,4,1,3,5},{1,2,4,8,9,3,5,6},{1,2,0,6,4,5}};
int n= sizeof(p)/sizeof(p[0]);
cout<<"the smallest number is %f"<< closest(p,n);
return 0;
}
Note:
The time complexity can be improved to O(nLogn)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.