Finding the minimum interval of K-arrays

Source: Internet
Author: User

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;
 }

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.