UVA-10020-贪心
题意:给你一些数轴上的线段,要求寻找出某些线段能够完全覆盖[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-贪心的更多相关文章
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
随机推荐
- MTP 设备不显示
win7 资源管理器(我的电脑)中不显示,但应用宝,豌豆荚工具能访问文件. 环境:WIN7 64位,手机HTC U11+ . 解决: 在设备管理器,计算机名称上右键-扫描检测硬件改动,等安装完手机驱动 ...
- 关于此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。
注册表进入如下路径中 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy 将 enable设置为0 ...
- ios导航栏和tabbar的坑
多年不写ios,目前重构项目,发现navBar和tabbar需要注意的点,记录备忘 translucent属性会导致view起始点的变化,默认为透明,和设计图有色差,改成不透明以后,坐标位置有变化,修 ...
- 关于“最小的K个数”问题
从一堆无序的数中(共n个数)找到最小的K个数,这也算是一道比较经典的题目了,关于这道题目的解法,一般有几种: 方法1:先对所有的数据进行排序,然后直接找出前K个数来,即最小的K个数.时间复杂度为O(N ...
- Easyloggingpp的使用
对于有开发经验的程序员来说,记录程序执行日志是一件必不可少的事情.通过查看和分析日志信息,不仅可以有效地帮助我们调试程序,而且当程序正式发布运行之后,更是可以帮助我们快速.准确地定位问题.在现在这个开 ...
- 《Java并发编程实战》笔记-synchronized和ReentrantLock
在一些内置锁无法满足需求的情况下,ReentrantLock可以作为一种高级工具.当震要一些高级功能时才应该使用ReentrantLock,这些功能包括:可定时的.可轮询的与可中断的锁获取操作,公平队 ...
- 第一个react
个人觉着react和vue是很相似的,之前还转载过一篇介绍两个异同点的文章,那个时候还完全不懂react,现在才慢慢开始接触,所以只能总结一些个人的心得,首先自然是react的优点了,个人觉着主要有以 ...
- python中画散点图
python中画散点图 示例代码: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d impor ...
- PHP中常用的数组函数总结
整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数,数组的分段和填充,数组与栈,数组与列队,回调函数,排序,计算,其他的数组函数等. 一,数组操作的基本函数 数组的键名和值 array_va ...
- Linux 防火墙和SELinux的开启和关闭
防火墙(firewalld) 临时关闭防火墙 systemctl stop firewalld 永久防火墙开机自关闭 systemctl disable firewalld 临时打开防火墙 syste ...