Coverage of segment intervals

Source: Internet
Author: User

Use i to denote an interval of [i-1,i] on the x-axis (interval length of 1), and give M different integers to represent M such an interval. It is now required to draw a few segments covering all the intervals, provided that each segment is arbitrarily long, but requires the smallest sum of the length of the drawn segment, and that the number of segments does not exceed N.

Example: Give m=6,1,2,4,5,7,11, respectively, the 6 length of the interval of 1 , requires not more than n=3 Line segment to overwrite it.

A feasible coverage scheme is presented, which covers 6 Small segments of length 1 with three segment segments , which makes the sum of 3 short segment lengths minimum.

l Algorithm ideas

Using the greedy algorithm,M line segments will produce M-1 breaks, there are small gaps, in order to from the large to the small sequence between the line spacing. Suppose that there is a whole line in the initial state covering the entire area, each step from the most spaced position to disconnect the segment until the N-1 times, the whole line is divided into N-section and the length of the N-segment and the smallest.

Establishment and design flow of program model

If n>=m, it is obvious that a segment with M-length 1 can cover all the intervals, and the total length of the line is also M. If N, try to find another way.

① N=1, the total length of the required line is position[m-1]-position[0]+1;

② n=2, then the equivalent of the ① in the long-term segment into two sections. Find the maximum distance distance[i]=position[i]-position[i-1]-1 (1

③ n=3, in the same vein, will distance the second largest position, thus forming 3 segments, and the length and minimum.

④ n=k (k>1), as long as the n=k-1 of the minimum total length of the coverage scheme, found by the same segment covering the largest two intervals, "greedy" from the interval and properly adjust the end of two segments, you can get the total length of the smallest scheme.

• selection of data structures

You can use an integer array of position[m] to represent all the intervals, and assume that position[m] has been sorted in the order of the small arrivals.

l source program code list

#include

using namespace Std;

const int size=200;

void Sort (int value[],int nnumber)// nnumber values from large to small

{

for (int i=0;i

for (int j=0;j

if (Value[j]

{

int TEMP=VALUE[J];

VALUE[J]=VALUE[J+1];

Value[j+1]=temp;

}

}

void Main ()

{

int m=0;

int position[size];

cout<< " Please enter the total number of zones to be covered m=" <<endl;

cin>>m;

int i=0;

cout<< " Please enter the section specifically " <<m

<< " a range to cover:" <<endl;

for (i=0;i

{

cin>>position[i];

}

Sort (POSITION,M);

int distance[size-1];

for (i=0;i

Distance[i]=position[i]-position[i+1]-1;

Sort (distance,m-1);

int n=1; total number of segments available for overlay

cout<< " Please enter the total number of segments available for overlay n=" <<endl;

cin>>n;

//********** greedy algorithm *************************

if (n>=m)

{

cout<< " minimum line length is "

<<M<<endl;

Return

}

else if (N

{

int nline=1; record the total number of current segments

int ntotallength=position[0]-position[m-1]+1;// records The total length of the segment used in the current situation

int ndivide=0; record the current largest non-fractured interval position

while ((NLine0]))

{

nline++;

Ntotallength-=distance[ndivide];

ndivide++;

}

cout<< " minimum line length is "

<<nTotalLength<<endl;

}

}

l Program input, output

l Time and space complexity analysis

The algorithm has a time complexity of O (NLOGN) +o (N).

L Instructions for use of the program

① to the position of the order, whether from the big to the small or to the big no difference, does not affect the interval distance calculation.

② Note Two conditions for a while loop

L Summary and Perfection

This program can get the minimum number of segments required under the total length of the condition, but it is not possible to give the endpoint information for these segments. You can further modify the program to add this functionality.

Coverage of segment intervals

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.