13 URAL1303 Minimal Coverage

给cnt条线段,要求覆盖[0,m],问能否覆盖,至少需要多少条线段。

用贪心的想法,假设当前位置在now,那么此时添加一条线段使得左端点<=now,右端点最远,这样是最划算的,

下次继续添加这样一条线段,使得右端点最远,左端点<=上一条选的线段的右端点。

扫完所有线段,now的位置可以到达m,则有解,否则无解。

#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
    int l,r;
}e[100010];
bool cmp(node a,node b)
{
    if(a.l!=b.l) return a.l<b.l;
    return a.r<b.r;
}
int T,m,cnt,p[100010];
int main()
{
    int a,b,i,now,ans,next,f,o,cas=0;
    while(~scanf("%d",&m))
    {
        cnt=0;
        while(scanf("%d%d",&a,&b)&&(a||b))
        {
            e[cnt].l=a;
            e[cnt++].r=b;
        }
        sort(e,e+cnt,cmp);e[cnt].r=e[cnt].l=100000;
        now=ans=next=0;f=o=0;
        for(i=0;i<cnt;i++)
        {
            if(e[i].l<=now&&e[next].r<=e[i].r) next=i,o=1;
            if(e[i+1].l>now&&o)
            {
                o=0;
                now=e[next].r;
                p[ans++]=next;
            }
            if(now>=m)
            {
                f=1;
                printf("%d\n",ans);
                for(i=0;i<ans;i++) printf("%d %d\n",e[p[i]].l,e[p[i]].r);
                break;
            }
        }
        if(!f) puts("No solution");
    }
    return 0;
}
时间: 2024-08-05 08:17:31

13 URAL1303 Minimal Coverage的相关文章

Timus 1303 Minimal Coverage DP或贪心

1303. Minimal Coverage 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 th

贪心 URAL 1303 Minimal Coverage

题目传送门 1 /* 2 题意:最少需要多少条线段能覆盖[0, m]的长度 3 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 4 接着就是从p=0开始,以p点为标志,选取 (node[i].l <= p && p < node[i+1].l) 5 详细解释:http://www.cnblogs.com/freezhan/p/3219046.html 6 */ 7 #include <cstdio> 8 #include <iostream>

【区间覆盖问题】uva 10020 - Minimal coverage

可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选择起点在s的最长区间; 2.选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分:  Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinat

uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】

Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test ca

UVA Minimal coverage (贪心)

Description  Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the num

uva10020 - Minimal coverage(区间覆盖)

题目:uva10020 - Minimal coverage(区间覆盖) 题目大意:给出一些线段,然后问怎样取能使得最少的线段覆盖区间[0, M]. 解题思路:先预处理掉那些和区间[0,M]不沾边的线段. 将线段按照起点小的排序. 接着遍历这些线段.首先先判断起点最小的点是否<=0,如果不满足这个说明它不能覆盖区间. 然后每次都小于等于当前覆盖的起点的最长线段,之后要将起点更新成这个最长线段的终点.然后接着判断下一条线段.如果更新了起点发现依然找不到满足条件的线段,说明不能覆盖. 最后还要看一下

URAL1303——贪心——Minimal Coverage

Description Given set of line segments [L i, R i] 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 c

UVA 10020 - Minimal coverage 解题心得

原题: The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test cases, followed

UVa 10020 - Minimal coverage(区间覆盖并贪心)

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0, M].InputThe first line is the number of test cases, followed by a blank line.Eac