题意:给你一些数轴上的线段,要求寻找出某些线段能够完全覆盖[0,M],并且取的线段数目最小.

解题思路:

贪心思路,

1.每个线段都有一个L和R,代表它的起点和终点,对于所有R <= 0 ,  L>=R的线段全不要,不符合题意.

2.对于每个线段,根据L进行排序,如果L相同,长度长的排前面.

那么选取的时候只要从原点0开始,每次选取最长的线段即可.

附上一组用例.


-
- - -
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip> namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack; class Point
{ public: int l, r, len;
Point() {};
Point(int x, int y) :l(x), r(y) {
if (l < 0)
this->len = r;
else
this->len = r - l; }; int operator < (Point& p)
{
int l1 = p.l;
int l2 = this->l;
if (l1 < 0)
l1 = 0;
if (l2 < 0)
l2 = 0;
if (l1 < l2)
return 0;
if (l1 == l2)
return p.len < this->len;
return 1;
} }; constexpr int N = 100000; int M;
int ok;
int ans = 0;
Point result[N + 1];
Point input[N + 1];
int n; void init()
{
ok = M = ans =n = 0;
} void dump()
{
for (int i = 0;i < n;i++)
{
cout << input[i].l << " " << input[i].r << endl;
}
} void search(int curR, int curIndex)
{
int curMax = 0;
int okIndex = -1;
int endIndex = -1;
for (int i = curIndex;i < n;i++)
{
if ((input[i].l <= curR && input[i].r > curR))
{
if (input[i].r > curMax)
{
curMax = input[i].r;
okIndex = i;
continue;
}
}
if (input[i].l > curR)
{
endIndex = i;
break;
}
}
if (okIndex != -1)
{
result[ans++] = input[okIndex];
if (input[okIndex].r >= M)
{
ok = 1;
return;
}
if (endIndex == -1)
return;
search(curMax,endIndex);
}
} void solve()
{
int cases;
cin >> cases;
int t = 0;
while (cases--)
{
if (t != 0)
cout << endl;
t++;
int l, r;
init();
cin >> M;
while (cin >> l && cin >> r && (l || r))
{
if (r <= 0||l >= r)
continue;
Point p(l, r);
input[n++] = p;
}
sort(input, input + n);
//dump();
search(0,0);
if (ok)
{
cout << ans << endl;
for (int i = 0;i < ans;i++)
cout << result[i].l << " " << result[i].r << endl; }
else
{
cout << 0 << endl;
} } } }; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return 0;
}

  

UVA-10020-贪心的更多相关文章

  1. uva.10020 Minimal coverage(贪心)

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

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

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

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

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

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

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

  5. 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...

  6. UVA 11389(贪心问题)

    UVA 11389 Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description II  ...

  7. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 10020 (最小区间覆盖) Minimal coverage

    题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...

  10. UVa 11389 (贪心) The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...

随机推荐

  1. PHP扩展开发:第一个扩展

    在上一篇文章<PHP扩展开发:安装PHP>我们已经将开发PHP扩展的PHP环境安装成功,那么接下来采用最简单直接的方式创建第一个扩展. 我们先假设业务场景,是需要有这么一个扩展,提供一个叫 ...

  2. 利用VMware虚拟机安装CentOS7

    CentOS7镜像下载:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso VMware虚拟 ...

  3. InfluxDB(官方使用说明)

    安装InfluxDB OSS 此页面提供有关安装,启动和配置InfluxDB的说明. InfluxDB OSS安装要求 root为了成功完成,需要安装InfluxDB软件包或具有管理员权限. Infl ...

  4. Ubuntu 14.04 下安装 MongoDB 服务器 和 PHP MongoDB Driver 数据驱动

    https://laravel-china.org/topics/309/install-mongodb-server-and-php-mongodb-driver-data-driver-under ...

  5. 纯css3实现文字间歇滚动效果

    场景: 假设有4条数据或者标题,视口中只显示两条,采用每次向上滚动一条数据来展示所有的数据.效果如图: 用JavaScript也很容易实现,但是需要操作DOM,可以参考这篇博客.考虑到项目中经常使用V ...

  6. JavaScript压缩代码解压缩还原具体办法

    我们知道许多网站都对Javascript代码进行了压缩,但这对开发者和学习者来说,读起来很费劲,本文分享给大家在Chrome浏览器中,如何使用Pretty Print功能反压缩JS代码,让代码更加直观 ...

  7. Java第09次实验(IO流)

    参考资料 本次作业参考文件 正则表达式参考资料 第1次实验 0. 验证 使用FileOutputStream写字节.(二进制文件与文本文件.try...catch...finally注意事项) 使用D ...

  8. 测试教程网.unittest教程.5. 实例: 找出所有是弱密码的用户

    From: http://www.testclass.net/pyunit/test_example_3/ 背景 当我们的测试数据是下面这些的时候,我们的用例是有问题的. [ {"name& ...

  9. LeetCode——2. Add Two Numbers

    一.题目链接:https://leetcode.com/problems/add-two-numbers 二.题目大意: 给定两个链表,每个链表表示一个非负数,不过数字的顺序是反过来存储的,要求给出这 ...

  10. Mongodb条件查询Query的用法

    Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...