There are K ordered arrays, please find a minimum range of digits. So that each of the K-ordered arrays has at least one number in the range.
For example:
1:4, 10, 15, 24, 26
2:0, 9, 12, 20.
3:5, 18, 22, 30.
The minimum range is [20,24], where 20 is in 2, 22 is in 3, 24 is in 1.
By merging the sort idea, we make sure that each of the elements from different arrays is compared to get the maximum value and the minimum value. You can get a range that contains all the numbers in the array.
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <
Vector> using namespace std;
(((())) int main () {int k,n,i,j,x;
cin>>k>>n;
Vector<vector<int> > A;
for (i = 0;i < K;i + +) {vector<int> T;
for (j = 0;j < N;j + +) {cin>>x;
T.push_back (x);
} a.push_back (t);
int index[99999];
for (i = 0;i <k;i + +) Index[i] = 0;
BOOL B = true;
int min,max,mint,maxt,p,mini = 99999;
while (b) {mint = a[0][index[0]];
Maxt = a[0][index[0]];
p = 0;
for (i = 1;i < K;i + +) {if (A[i][index[i]] < mint) {mint = a[i][index[i]];
p = i;
} if (A[i][index[i]] > maxt) {maxt = a[i][index[i]];
} if (Maxt-mint < MinI) {max = Maxt; min = mint
MinI = Maxt-mint;
} Index[p] + +;
if (Index[p] >= n) b = false;
} cout<<min<< "" <<max;
return 0;
}