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. 问如何分配路线才能使加班费最少. 分析: 感 ...
随机推荐
- ALGO-119_蓝桥杯_算法训练_寂寞的数
问题描述 道德经曰:一生二,二生三,三生万物. 对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和.例如,d()=++=, d()=++++=. 因此,给定了任意一个n作为起点,你可 ...
- Importing multi-valued field into Solr from mySQL using Solr Data Import Handler
http://stackoverflow.com/questions/20233837/importing-multi-valued-field-into-solr-from-mysql-using- ...
- PAT 乙级 1041 考试座位号(15) C++版
1041. 考试座位号(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 每个PAT考生在参加考试时都会被分 ...
- 服务链路追踪(Spring Cloud Sleuth)
sleuth:英 [slu:θ] 美 [sluθ] n.足迹,警犬,侦探vi.做侦探 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的 ...
- SkipList理解
记下自己对跳表SkipList的理解. SkipList采用空间换时间的思想,通过增加数据间的链接,达到加快查找速度的目的. 数据库LevelDB和RocksDB中用到了SkipList,Redis中 ...
- 如何获取阿里云OSS上每个文件夹的大小
原文 https://help.aliyun.com/document_detail/88458.html?spm=a2c4g.11186623.2.11.792462b15oU02q OSS文件按照 ...
- TextView-- 测量文字宽度
https://my.oschina.net/lengwei/blog/637380; http://blog.csdn.net/mare_blue/article/details/51388403; ...
- F5负载均衡原理(转载)
https://blog.csdn.net/panxueji/article/details/42647193 一. 负载均衡技术 负载均衡技术在现有网络结构之上提供了一种廉价.有效.透明的方法,来扩 ...
- 从MediaStorehe和sd中删除媒体文件
参考资料:http://www.sandersdenardi.com/querying-and-removing-media-from-android-mediastore/ 从媒体表中删除: pri ...
- 基于 MBTiles 规范扩展的缓存文件格式说明
MBTiles 是由 MapBox 制定的一种将瓦片地图数据存储到SQLite数据库中并可快速使用,管理和分享的规范.该规范由MapBox制定,详见http://mapbox.com/mbtiles- ...