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. 问如何分配路线才能使加班费最少. 分析: 感 ...
随机推荐
- Vivado约束文件(XDC)的探究(1)
Vivado约束文件(XDC)的探究(1) 工程建好之后会出现xdc文件: 注意:active 和 target 生成的约束文件如下:
- 使用Tesseract-OCR 进行文字识别
关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试用一下,分享到博客让有同样兴趣的人少走弯路. 文中所用到的身份证图片资源是百度找的,如有侵权可联系我删除. 一. ...
- spring 整合Junit学习
测试一般是测试的局部功能,使用时需要自己写个测试相关的spring配置文件,比较费劲,所以常用的是纯配置的方式来实现测试. @RunWith(SpringJUnit4ClassRunner.class ...
- 关于nginx大流量负载调优
优化nginx包括两方面: 1.是自己重写nginx代码(比如tengine).本身nginx的代码已经足够优秀,如果不是每秒几千的请求,就忽略这个部分吧. 2.另一个就是和优化nginx的配置,这是 ...
- windows迁移linux问题集锦
1)‘_wcsicmp’在此作用域中尚未声明 #ifdef WIN32#define _tcsicmp _wcsicmp#else#define _tcsicmp wcsc ...
- NodeJs之配置文件管理
查询了一些资料,我使用nodejs的object作为配置文件,首先定义一个module config.js: var config = { uploadPath: "E:\\" } ...
- mysql 5.6 binlog组提交实现原理(转载)
http://blog.itpub.net/15480802/viewspace-1411356/ Redo组提交 Redo提交流程大致如下 lock log->mutex write redo ...
- Jmeter(二十九)Jmeter-Question之“Ant集成报告模板优化”
也是在和朋友探讨的时候,发现一个问题,Jmeter在与Ant集成的时候,通常选用的模板是jmeter自带的两个样式表 该自带的样式,节省了大家搭建框架的时间,不需要自己重新写样式,当然也相对简洁: 做 ...
- css定位(后盾网)
1.绝对定位:脱离了文档流,如果设置了left和top,位置相对浏览器来定位,如果不设值top和left,还是按照原来的文档流的位置站位,位置移走后,原来的空间位被其他元素占据 ***应用:一般来说我 ...
- jqgrid使用(1)生成表格
1.引入js,css 2,基本配置 function init() { $("#list1").jqGrid({ url: "../Listing.ashx", ...