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. 问如何分配路线才能使加班费最少. 分析: 感 ...
随机推荐
- Elasticsearch的脚本化数据导入导出
我用的ES的版本是2.4.1,由于没有相应的命令实现数据的导入和导出,就是像mysql的那种mysqldump类似的指令. 更苦逼的是,我们的生产和测试环境,还不能联网,连ES的第三方的插件都没有办法 ...
- webstorm 破解码
https://blog.csdn.net/voke_/article/details/76418116 摘自此博客
- Daubechies小波介绍
Daubechies小波是正交.连续且紧支撑的. 正交条件下,$H(\omega)$必须满足下式: $|H(\omega)|^2+|H(\omega + \pi)|^2 =1$ 连续紧支撑条件下,$H ...
- ubuntu MySQL拒绝远程连接(10061)
MySQL是使用apt-get安装的 1.停止mysql服务 sudo service mysql stop 2.修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf 将b ...
- 字符串分割split()
知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...
- Docker启动一个Centos镜像
docker镜像的获取与使用 docker中使用centos7镜像 接着上文,我们下载完成一个Centos镜像之后,开始启动 #运行命令 docker run -d -i -t <imageID ...
- git遇到的问题之“Please make sure you have the correct access rights and the repository exists.”
对于git的提交一直很小心翼翼,感觉一不小心就会踩到莫名的坑. 这不, 某天commit 就遇到了On branch master nothing to commit (working directo ...
- [UE4]限制杀人信息的显示数量
- [UE4]AIPerception,AI感知
- vue 动态路由 Get传值
main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...