URAL 1303. Minimal Coverage(最小覆盖 数学啊 )

题目链接:?space=1&num=1303

Given set of line segments [Li, Ri] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).

Input

First line of the input contains an integer M (1≤M≤5000). Subsequent lines of input contain pairs of integers Liand Ri(50000 ≤ Li< Ri≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.

Output

Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.

If there is no covering subset then print “No solution” to output.

Samples

inputoutput

1-1 0-5 -32 50 0No solution

1-1 00 10 010 1

题意:

最小区间覆盖,求使用最少的木板,能覆盖区间0—-M!

PS:

按照贪心的思想;

每次找到左端点在未被覆盖区间的左端点的左边,且右端点最远的木板,,

然后把要求的覆盖区间改为这个右端点到M这个区间。

把此时木板的右端点作为未被覆盖区间的左端点!

依次类推下去。

代码如下:#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;struct node{int l, r;} a[100017];int vis[100017];bool cmp(node a, node b){return a.l < b.l;}int main(){int m;while(~scanf("%d",&m)){memset(vis, 0,sizeof(vis));int x, y;int k = 0;while(1){scanf("%d%d",&x,&y);if(x==0 && y==0){break;}a[k].l = x, a[k].r = y;k++;}sort(a,a+k,cmp);int cont = 0;int num = 0, p = 0;int flag = 0;int ans = 0;for(int i = 0; ; i++){if(cont >= m){break;}int t_end = 0, tt = 0;while(num < k && a[num].l <= cont){if(a[num].r > t_end){t_end = a[num].r;tt = num;}num++;}if(t_end == 0){flag = 1;break;}cont = t_end;vis[tt] = 1;ans++;}if(flag){printf("No solution\n");continue;}printf("%d\n",ans);for(int i = 0; i < k; i++){if(vis[i]){printf("%d %d\n",a[i].l,a[i].r);}}printf("\n");}return 0;}

人生并不在于获取,更在于放得下。放下一粒种子,收获一棵大树;

URAL 1303. Minimal Coverage(最小覆盖 数学啊 )

相关文章:

你感兴趣的文章:

标签云: