The minimum length of the interval coverage

# The meaning of problems

I is represented by the x-axis coordinates [i-1, i] in the length of the interval 1 and said (1≤n≤200) different integers n, n represents the number of such intervals. 

Videos are now required to cover all the line segments m interval, 

With the proviso that: each segment can be any length, but requires drawn line segment length and a minimum,

And the number of segments is not more than m (1≤m≤50).

# Solution problem
length of a segment of at least 1

 

 I.e., the largest gap does not take several intervals, take the length of section 1

 1 #include <bits/stdc++.h>
 2 #define pii pair<int,int>
 3 #define fi first
 4 #define se second
 5 using namespace std;
 6 const int N=210;
 7 int a[N],b[N];
 8 int main(){
 9     int n,m;
10     while(scanf("%d",&n)!=EOF) {
11         scanf("%d",&m);
12         for(int i=0;i<n;i++)
13             scanf("%d",&a[i]);
14         sort(a,a+n);
15         int ans=a[n-1]-a[0]+1;
16         for(int i=0;i<n-1;i++)
17             b[i]=a[i+1]-1-a[i];
18         sort(b,b+n-1);
19         int res=0;
20         m-=1;
21         for(int i=n-2;m;i--,m--)
22             res+=b[i];
23         printf("%d\n",ans-res);
24     }
25 }

 

 

 

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12543779.html
Recommended