题目地址: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. CorelDRAW X6低价再次冲破底线

    平时我们看到的标志设计.杂志排版.产品商标.插图描画......这些都是设计师们使用CorelDRAW设计而来.如今CorelDRAW已经成为每个设计师必装的软件,从12年发布CorelDRAW X6 ...

  2. jQuery基本操作以及与js的一些比较

    jQuery和js主要区别在DOM操作 用jQuery必须先引进jQuery.js文件 js和jQuery写在哪: 1.标签里面 常用就是方法调用 2.写在script标签里面 3.js文件 dom操 ...

  3. HTML提交表单

    一.使用form提交表单 <form action="#" method="post"> {% csrf_token %} 班级<input ...

  4. NuSOAP简介 php中使用webservice

    许多机构已经采用了Apach和PHP作为他们的Web应用环境.在Web services模式中采用PHP可能看上去可能会比较难.但是事实上,搭配NuSoap,你可以轻松的应用PHP构建SOAP的客户端 ...

  5. 题解 P3369 【【模板】普通平衡树】

    在网上某篇神奇的教程和@codesonic 大佬的标程帮助下,我又肝完了Leafy Tree,跑过来写篇题解(好像以前写过一篇?) 什么是Leafy Tree? Leafy Tree由两种节点组成:辅 ...

  6. 2015 Multi-University Training Contest 3 hdu 5316 Magician

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  7. Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException总结

    最新项目中遇到了 Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.Proces ...

  8. gdb与信号

    http://simohayha.iteye.com/blog/493091 gdb可以监测在你的程序中的任何信号. 主要靠的命令是: handle signal [keywords...] 这里的k ...

  9. XUtils3框架的基本用法(一)

    本文为作者原创,转载请指明出处: http://blog.csdn.net/a1002450926/article/details/50341173 今天给大家带来XUtils3的基本介绍.本文章的案 ...

  10. oracle 存储过程定义及调试,并终于被C# 调用 代码

    C# 调用存储过程 參考了非常多文章,写了例如以下文字,算是分享吧 目的:更改积分,并作一定校验 一.一般的调试方法: 方法一:带返回out參数,必须定义变量 myresult DECLARE myr ...