题目地址: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;
}

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

  1. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  2. 贪心 URAL 1303 Minimal Coverage

    题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...

  3. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

  4. URAL 1303 Minimal Coverage

    URAL 1303 思路: dp+贪心,然后记录路径 mx[i]表示从i开始最大可以到的位置 sufmx[i]表从1-i的某个位置开始最大可以到达的位置 比普通的贪心效率要高很多 代码: #inclu ...

  5. URAL 1303. Minimal Coverage(DP)

    题目链接 又是输出路径...这题完全受上题影响,感觉两个题差不多..用了基本上一样的算法写了,这题比较纠结,就是卡内存啊...5000*5000的数组开不了..然后没办法,水了好几次MLE,看了一下虎 ...

  6. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  7. uva.10020 Minimal coverage(贪心)

    10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...

  8. Minimal coverage (贪心,最小覆盖)

    题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...

  9. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

    Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...

随机推荐

  1. First-class citizen

    In programming language design, a first-class citizen (also type, object, entity, or value) in a giv ...

  2. MVC 单元测试xUnit初探

    对于.NET项目 Web Api的业务逻辑后台开发[特别是做Web Api接口]而言,编写单元测试用例,会极大的减轻代码帮助与运行的方式.然而使用测试框架,相对于自带的,我更加推荐是用xUnit.ne ...

  3. * ? 【a-z】【0-9】通配符 学习

    通配符顾名思义就是通用的匹配信息的符号,比如星号(*)就是代表匹配零个或多个字符,问号(?)是代表匹配单个字符,中括号内加上数字[0-9]代表匹配单个阿拉伯数字的字符,而中括号内加上字母[abc]则是 ...

  4. Python学习简单练习-99乘法表

    __author__ = 'ZFH'#-*- coding:utf-8 -*-for i in range(10): #外层循环,range(10),1-9 for j in range(1,i+1) ...

  5. @SpringBootApplication cannot be resolved to a type In STS

    @SpringBootApplication cannot be resolved to a type In STS 学习了:https://stackoverflow.com/questions/4 ...

  6. cocos2d_android 第一个游戏

    依据上一篇文章.创建好cocos2d--android的开发环境 先上效果图 实现该效果的代码: package com.cn.firstgame; import org.cocos2d.layers ...

  7. mysql-联结

    一.联结 联结是利用SQL的select能执行的最重要的操作. 1.关系表:假如有一个包含产品目录的数据库表,其中每个类别的物品占一行.对于每种物品要求存储的信息包括产品描述和价格,以及生产该产品的供 ...

  8. UVa 10069 Distinct Subsequences(大数 DP)

     题意 求母串中子串出现的次数(长度不超过1后面100个0  显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数   当a[i]==b[j]&am ...

  9. 可靠的Windows版Redis

    副标题: 评论更精彩,教你怎么解决64位Windows版Redis狂占C盘的问题. MS Open Tech 技术团队近期花了非常多时间来測试最新构建的Windows版Redis(可在 MS Open ...

  10. 学习bootstrap

    菜鸟教程 bootstrap开发框架 伍华聪 Bootstrap——一款超好用的前端框架