首页 > 其他 > 详细

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

时间:2015-03-22 21:03:21      阅读:398      评论:0      收藏:0      [点我收藏+]
题目链接: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

input output
1
-1 0
-5 -3
2 5
0 0
No solution
1
-1 0
0 1
0 0
1
0 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(最小覆盖 数学啊 )

原文:http://blog.csdn.net/u012860063/article/details/44540963

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!