Ural 1303 Minimal Coverage(贪心)

题目地址:Ural 1303

先按每个线段的左端点排序,然后设置一个起点s,每次都从起点小于等于s的线段中找到一个右端点最大的。并将该右端点作为新的起点s,然后继续找。从左到右扫描一遍即可。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int a[100000];
struct node
{
    int l, r, ll, rr;
} fei[100000];
int cmp(node x, node y)
{
    return x.l<y.l;
}
int main()
{
    int m, i, j, n, l, r, s, max1, cnt=0, tot, pos, flag, flag1, ll, rr;
    scanf("%d",&m);
    while(scanf("%d%d",&ll, &rr)!=EOF&&(ll||rr))
    {
        if(rr<=0||ll>=m) continue ;
        fei[cnt].ll=ll;
        fei[cnt].rr=rr;
        l=ll;r=rr;
        if(l<0)
            l=0;
        if(r>m)
            r=m;
        fei[cnt].l=l;
        fei[cnt++].r=r;
    }
    sort(fei,fei+cnt,cmp);
    s=0;
    tot=0;
    if(cnt==0)
    {
        printf("No solution\n");
    }
    else
    {
        for(i=0; i<cnt;)
        {
            max1=-1;
            flag=0;
            while(fei[i].l<=s&&i<cnt)
            {
                flag=1;
                if(max1<fei[i].r)
                {
                    max1=fei[i].r;
                    pos=i;
                }
                i++;
            }
            if(!flag)
                break;
            if(s<max1)
            {
                a[tot++]=pos;
                s=max1;
            }
        }
        if(i<cnt||s<m)
            puts("No solution");
        else
        {
            printf("%d\n",tot);
            for(i=0; i<tot; i++)
            {
                printf("%d %d\n",fei[a[i]].ll,fei[a[i]].rr);
            }
        }
    }
    return 0;
}
时间: 2024-08-11 05:36:28

Ural 1303 Minimal Coverage(贪心)的相关文章

贪心 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>

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

uva 10020 Minimal coverage(贪心,区间覆盖)

这道题一读就是经典的区间问题,是区间覆盖,敲过之后还有花了很长的调试时间,还是我不熟练,现在做题确实挺慢 的,简单题目也要做好久,没事,慢慢来.最重要的要确保正确率和心态问题,认真对待,调试找到了好多bug,一些 细节问题...都是刚开始没有注意到的.交了之后RE,在数组上多加了两个0.A了,,uva老是不提示数据有多大, 所以只能乱开... 思路: 先对区间按左边的点进行排序,如果当前需要涵盖的区间为[x,y],那么在排序的区间中到左边小于x,右 边最大的那个区间,设为Max,然后更新想找的区

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

【区间覆盖问题】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

uva10020 - Minimal coverage(区间覆盖)

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

ural Minimal Coverage (区间覆盖)

http://acm.timus.ru/problem.aspx?space=1&num=1303 给出一些区间,选择尽量少的区间能覆盖到[0,m]. 小白p154,典型的区间覆盖问题.一直在想怎么dp.. 首先预处理,先按左端点从小到大排序,若左端点相同右端点从大到小排序,若区间x完全包含y,按照贪心的思想,y是没有意义的,有大区间可以选何必选择小区间.处理完事之后各个区间满足a1 <= a2 <= a3....且b1 <= b2 <= b3... 这样找到第一个覆盖0的

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