链接:

http://acm.timus.ru/problem.aspx?space=1&num=1303

按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间改为这个右端点到M这个区间。依次类推下去,这样的话就只需要扫一遍就可以找去来。

要做的预备工作就是将线段按照左端点的升序排序就可以了。

它的时间复杂度就是O(n)

代码一直WA,望大神指教

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define MAXN 200005
#define INF 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define judge(i) (ma[i].l<=L && ma[i].r>=L && ma[i].l != ma[i].r)//判断是否覆盖了点L struct node{int l,r;}ma[MAXN];
int ans[MAXN];
int M,N,T;
int L; int cmp(node a,node b)
{
if(a.l != b.l)return a.l<b.l;
else return a.r<b.r;
} int find_index(int s,int &i)//找到覆盖了点L而且右端点最大的点,并返回;
//同时将后面要覆盖的点设置为它的右端点
{
i = s;
int e = s,endd = ma[i].r;
while(i < N && judge(i) )
{
if(ma[i].r > endd)
{
endd = ma[i].r;
e = i;
}
i++;
}
L = endd;//endd就是满足覆盖条件下的最大的右端点
//并将其设置为下次比较的左端点
return e;//返回此次的线段的下标
} int main()
{
while(~scanf("%d",&M))
{
N=;
int i;
mem(ans);
while(scanf("%d%d",&ma[N].l, &ma[N].r) && (ma[N].l || ma[N].r))
{
if(ma[N].r <= || ma[N].l>=M)ma[N].l = ma[N].r = INF;//吧在要求区间两端之外的线段去掉
//吧它的左右端点值赋值为INF,这样的话排序时自然就会到最后方,也就相当于不用考虑
N++;
}
sort(ma,ma+N,cmp); L = ;//最初要被覆盖的点是0
int num = ;
for(i=;i<N;i++)
{
int t = i+;//将t设置为i+1,如果下面的judge成功,就会进入函数的while中,那么i会++,如果不成功,t=i+1
if(judge(i)) ans[num++] = find_index(i,t);
i = t-;//由于t相当于多+了1,所以-1
if(L >= M)break;//一旦找到,就退出循环
}
if(L < M)printf("No solution\n");
else
{
printf("%d\n",num);
for(i=;i<num;i++)
{
printf("%d %d\n",ma[ans[i]].l,ma[ans[i]].r);
}
}
}
return ;
}

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

  1. 贪心 URAL 1303 Minimal Coverage

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

  2. ural 1303 Minimal Coverage【贪心】

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

  3. Ural 1303 Minimal Coverage(贪心)

    题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...

  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. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  2. PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different 2012-04-11 13:01

    启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) ...

  3. Codeforces 383A - Milking cows

    原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...

  4. bzoj2794

    这题我得到一个经验,bool型的dp一定要想办法把bool去掉来表示更多的东西(1933也是这个道理) 暴力大家都会,这里有两个限制条件 一个限制条件我们可以排序不断加入,另一个呢 我们可以用f[i] ...

  5. Unit testing Cmockery 简单使用

    /********************************************************************** * Unit testing Cmockery 简单使用 ...

  6. c& c++ enum

    1.为什么要用enum       写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等.   ...

  7. ORACLE 如何定位消耗资源的SQL

    在分析SQL性能的时候,经常需要确定资源消耗多的SQL,总结如下: 1 查看值得怀疑的SQLselect substr(to_char(s.pct, '99.00'), 2) || '%' load, ...

  8. Entity Framework中编辑时错误ObjectStateManager 中已存在具有同一键的对象

    ObjectStateManager 中已存在具有同一键的对象.ObjectStateManager 无法跟踪具有相同键的多个对象. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈 ...

  9. Storm-6 Storm的并行度、Grouping策略以及消息可靠处理机制简介

    概念: 配置并行度 动态的改变并行度 流分组策略----Stream Grouping 消息的可靠处理机制 概念: Workers (JVMs): 在一个节点上可以运行一个或多个独立的JVM 进程.一 ...

  10. 【LeetCode 201】Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...