Divide&conquer
Divide&conquer
divide&conquer(input I)
begin
if (size of input is small enough) then
solve directly;
return;
endif
#include <stdio.h>
int i, n;
int m[1001];
int main(void)
{
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &m[i]);
E-OLYMP 928. The sum of the largest and the smallest Array of n integers is
given. Find the sum of minimum and maximum in array using divide-and-conquer
technique.
► Implement the functions FindMin(left, right) and FindMax(left, right).
To solve the problem, print the value of FindMin(1, n) + FindMax(1, n).
Merge operation
E-OLYMP 9593. Merge the sequences Two sequences are given. Merge them.
► We can read two sequences into one array and sort it. It takes O(nlog2n) time,
where n is the sum of sizes of two sequences.
If a and b are input arrays, res is resulting array, we can use STL function merge, it
takes O(n) time.
merge(a.begin(), a.end(), b.begin(), b.end(), res.begin());
But let’s consider the process of merging two sequences a and b more thoroughly.
Declare three variables – pointers p, q, i and set up them:
p = 0, points to the start of the first array a;
q = 0, points to the start of the second array b;
i = 0, points to the start of the resulting array res;
At each step (iteration) compare the values a[p] and b[q]. The least of these values
assign to res[i], and increase by 1 the pointer i and pointer to the minimum element (p
or q). For example, after some steps we can get next state of arrays:
When the pointer in one of arrays comes to the end, the rest of the second array
should be copied to the resulting array.
While none of pointers has reached the end of array, assign res[i] = min(a[p], b[q])
and move the corresponding pointers forward by one.
for (i = 0; p < n && q < m; i++)
{
if (a[p] <= b[q]) res[i] = a[p], p++;
else res[i] = b[q], q++;
}
One of the pointers reached the end of array. Copy the remainder of the second
array into the resulting one. If at the moment p = n, then only the second while will run.
If q = m at the moment, then only the first while will run.
while (p < n) res[i++] = a[p++];
while (q < m) res[i++] = b[q++];
E-OLYMP 9605. Merge three sequences Three sequences are given. Merge
them.
► Merge three sequences in O(n + m + k) time, where n, m, k are the sizes of input
sequences.
Merge sort
Merge sort divides input array in two halves, calls itself for the two halves and
then merges the two sorted halves.
void merge(int *a, int bleft, int bright, int cleft, int cright)
{
// a[bleft..bright] and a[cleft..cright]
// are merged into a[bleft..cright]
int i, left = bleft, len = cright - bleft + 1;
int *res = new int[len];
for (i = 0; i < len; i++)
{
if ((bleft > bright) || (cleft > cright)) break;
if (a[bleft] <= a[cleft]) res[i] = a[bleft], bleft++;
else res[i] = a[cleft], cleft++;
}
int m[1001], i, n;
int main(void)
{
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &m[i]);
mergeSort(m, 1, n);
E-OLYMP 972. Sorting time Sort the time according to specified criteria.
► Use MergeSort to sort the time structures.
Declare structure MyTime.
struct MyTime
{
int hour, min, sec;
MyTime() {};
MyTime(MyTime &a) : hour(a.hour), min(a.min), sec(a.sec) {};
};
E-OLYMP 8735. Train swapping Find the number of inversions in array. Use
2
O(n ) solution.
► Let the array m contains the input permutation – the current order of the
carriages. The required minimum number of permutations equals to the number of
inversions in the permutation.
An inversion is a pair of numbers (mi, mj) such that i < j but mi > mj. That is, a pair
of numbers forms an inverse if they are not in the correct order.
For example, the array m = {3, 1, 2} has two inversions: (3, 1) and (3, 2). The pair
(1, 2) does not form an inversion, since the numbers 1 and 2 stand in the correct order
relative to each other.
The number of inversions in the array of length n can be calculated using a double
loop: we iterate over all possible pairs (i, j) for which 1 ≤ i < j ≤ n, and if mi > mj, then
we have an inversion.
The minimum number of permutations for putting the train in order is calculated in
the variable res.
res = 0;
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
if (m[i] > m[j]) res++;
E-OLYMP 1457. “Sort” station At the “Sorting” railway station on the way there
are n railroad cars, out of which it is necessary to form the railway train. All cars have
the same length, but different cargoes are arranged at them, so the cars may have
different weights. The workers of the “Sort” train station should order the cars in
ascending weight, then the train is allowed to go.
Usually for such purposes the so-called shunting locomotives and electric
locomotives are used, but at this station the experimental device for sorting cars is used.
It is assumed that it will significantly reduce the time required for forming trains.
This device is moved on an air cushion above the cars, its length is slightly greater
than the length of the two cars. It can hang on two adjacent cars, pick them both in the
air and interchange. However, the device lifting capacity is limited: the operation can be
carried out if the total mass of the two cars does not exceed M.
Your task is to write a program that determines whether it is possible to sort the
cars on the rails with the help of experimental device in the demanding order.
► To sort the cars, we will use exchange sorting. However, the task requires not to
sort the cars, but to determine whether it is possible. For this, for each adjacent pair of
cars for which mi > mi+1, swap the cars with numbers i and i + 1. If there is an i such that
mi > mi+1 and mi + mi+1 > M, then sorting cannot be performed.
Calculate in the variable mx the maximum among the numbers m1, m2, …, mi. If the
next value mi+1 is less than mx, then the car of mass mx must be swapped with the car of
mass mi+1 during the exchange sorting process. If for some i the inequality mx + mi+1 >
M holds and, in addition, mx > mi+1, then sorting is impossible. Otherwise, the cars can
be sorted in increasing order of their masses.
Let’s simulate the merge sort from the first example. Inversions are counted during
the merge operation. Near each sequence obtained after merge operation, we give the
number of inversions formed by the elements of the merged subsequences.