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

来源:互联网 发布:淘宝网秋冬男式上衣 编辑:程序博客网 时间:2024/06/05 11:43
题目链接:http://acm.timus.ru/problem.aspx?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 Li and 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 0
No solution
1-1 00 10 0
10 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;}


1 0
原创粉丝点击