区间覆盖问题
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
用i来表示x坐标轴上坐标为[i-1,i]的长度为1的区间,并给出n(1≤n≤200)个不同的整数,表示n个这样的区间。
现在要求画m条线段覆盖住所有的区间,
条件是:每条线段可以任意长,但是要求所画线段的长度之和最小,
并且线段的数目不超过m(1≤m≤50)。
Input
输入包括多组数据,每组数据的第一行表示区间个数n和所需线段数m,第二行表示n个点的坐标。
Output
每组输出占一行,输出m条线段的最小长度和。
Example Input
5 3
1 3 8 5 11
Example Output
7
Hint
Author
///区间覆盖问题
#include<stdio.h>
void asort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]<a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int n,m;
int a[210],b[210];
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
asort(a,n);
for(int i=0;i<n-1;i++)
{
b[i]=a[i]-a[i+1]-1;
}
asort(b,n-1);
int len=a[0]-a[n-1]+1;
int j=0;
for(int i=1;i<n&&i<m;i++)
{
len-=b[j++];
}
printf("%d\n",len);
}
return 0;
}
区间覆盖问题
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
用i来表示x坐标轴上坐标为[i-1,i]的长度为1的区间,并给出n(1≤n≤200)个不同的整数,表示n个这样的区间。
现在要求画m条线段覆盖住所有的区间,
条件是:每条线段可以任意长,但是要求所画线段的长度之和最小,
并且线段的数目不超过m(1≤m≤50)。
Input
输入包括多组数据,每组数据的第一行表示区间个数n和所需线段数m,第二行表示n个点的坐标。
Output
每组输出占一行,输出m条线段的最小长度和。
Example Input
5 3 1 3 8 5 11
Example Output
7
Hint
Author
#include<stdio.h> void asort(int a[],int n) { for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(a[j]<a[j+1]) { int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } int main() { int n,m; int a[210],b[210]; while(~scanf("%d %d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%d",&a[i]); } asort(a,n); for(int i=0;i<n-1;i++) { b[i]=a[i]-a[i+1]-1; } asort(b,n-1); int len=a[0]-a[n-1]+1; int j=0; for(int i=1;i<n&&i<m;i++) { len-=b[j++]; } printf("%d\n",len); } return 0; }