G gas stations are authorized to operate over a road of length L. Each gas station is able to sell fuel over a specific area of influence, defined as the closed interval [x − r, x + r], where x is the station’s location on the road (0 ≤ x ≤ L) and r is its radius of coverage (0 < r ≤ L). The points covered by a gas station are those within its radius of coverage.
    It is clear that the areas of influence may interfere, causing disputes among the corresponding gas stations. It seems to be better to close some stations, trying to minimize such interferences without reducing the service availability along the road.
    The owners have agreed to close some gas stations in order to avoid as many disputes as possible. You have been hired to write a program to determine the maximum number of gas stations that may be closed, so that every point on the road is in the area of influence of some remaining station. By the way, if some point on the road is not covered by any gas station, you must acknowledge the situation and inform about it.
Input
The input consists of several test cases. The first line of each test case contains two integer numbers L and G (separated by a blank), representing the length of the road and the number of gas stations, respectively (1 ≤ L ≤ 108, 1 ≤ G ≤ 104). Each one of the next G lines contains two integer numbers xi and ri (separated by a blank) where xi is the location and ri is the radius of coverage of the i-th gas station (0 ≤ xi ≤ L, 0 < ri ≤ L). The last test case is followed by a line containing two zeros.
Output
For each test case, print a line with the maximum number of gas stations that can be eliminated, so that every point on the road belongs to the area of influence of some not closed station. If some point on the road is not covered by any of the initial G gas stations, print ‘-1’ as the answer for such a case.
Sample Input
40 3
5 5
20 10
40 10
40 5
5 5
11 8
20 10
30 3
40 10
40 5
0 10
10 10
20 10
30 10
40 10
40 3
10 10
18 10
25 10
40 3
10 10
18 10
25 15
0 0
Sample Output
0
2
3
-1
1

问题链接:UVA12321 Gas Stations
问题简述:(略)
问题分析:贪心问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA12321 Gas Stations */#include <bits/stdc++.h>using namespace std;const int N = 10000;
pair<int, int> p[N];int main()
{int l, g;while (~scanf("%d%d", &l, &g) && (l || g)) {for (int i = 0; i < g; i++) {int x, r;scanf("%d%d", &x, &r);p[i] = make_pair(x - r, x + r);}sort(p, p + g);int ans = g, last = 0, k = 0;for (;;) {int maxs = last, flag = 0;while (k < g) {if (p[k].first <= last) {if (p[k].second > last) {maxs = max(maxs, p[k].second);flag = 1;}k++;} elsebreak;}ans -= flag;if ((last = maxs) >= l) break;if (flag == 0) {ans = -1;break;}}printf("%d\n", ans);}return 0;
}

UVA12321 Gas Stations【贪心】相关推荐

  1. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  2. 134. Gas Station加油站

    [抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  3. codeUp 2031 To fill or not to fill 复杂贪心

    2031: To Fill or Not to Fill 时间限制: 1 Sec  内存限制: 32 MB 提交: 599  解决: 132 With highways available, driv ...

  4. Leetcode 774. Minimize Max Distance to Gas Station

    LWC 69: 774. Minimize Max Distance to Gas Station 传送门:774. Minimize Max Distance to Gas Station Prob ...

  5. LWC 69: 774. Minimize Max Distance to Gas Station

    LWC 69: 774. Minimize Max Distance to Gas Station 传送门:774. Minimize Max Distance to Gas Station Prob ...

  6. 加油python_力扣——gas station (加油站) python实现

    题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...

  7. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  8. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  9. LeetCode 134.Gas Station 解题分析

    题目来源: https://leetcode.com/problems/gas-station/description/ 题目描述: There are N gas stations along a ...

最新文章

  1. APIO2010 特别行动队 斜率优化DP算法笔记
  2. ubuntu的codelite中代码运行后出现/usr/bin/codelite_exec: 22: /usr/bin/codelite_exec
  3. 运营级IOS一键签名程序源码 支持app超级签名一键分发平台
  4. GNU make manual 翻译( 一百一十一)
  5. 记一次在西部数码上买域名的悲催经历
  6. 为什么不能线程调用类的成员函数_C++多线程编程之创建线程的几种方法
  7. 拨码开关控制数码管的数字显示_VHDL编程
  8. 推荐一个插件: The Great Suspender,大大减少chrome浏览器内存占用量
  9. mysql客户sqlyog_MySQL客户端工具 SQLyog
  10. 剑三重制版怎么同步插件_剑网三重制版怎么导入插件 | 手游网游页游攻略大全...
  11. 赤兔oracle恢复软件 收费,赤兔Oracle数据库恢复软件下载 v11.6官方版-下载啦
  12. 深度学习面试专用:深度学习500问;文字版吴恩达深度学习,机器学习
  13. **Unity环境光遮蔽(Ambient Occlusion)Shader实现逻辑**
  14. VBA 禁止在某个sheet中使用键盘Delete键
  15. 0x01 前情提要随着疫情反复,今天我在家办公。我需要登一台服务器上配置,但是那个地址只能通过深信服vpn连接,在家办公一天就带了台kali系统的笔记本回来,没带windows的。 kali上
  16. 使用网上成熟的【MySqlBackup】组件,通过WEB网页操作,备份远程计算机中的数据库到C:\inetpub\wwwroot文件夹下,系统汇报错误(访问被拒绝),该如何解决呢?
  17. Cesium火灾动画(模型动画,粒子特效)
  18. 使用命令将项目打包并使用WinSCP将包部署到服务器
  19. ubantu查看设备序列号
  20. 手把手教你怎么批量压缩视频文件

热门文章

  1. 我竟然用git send-mail通过QQ邮箱提交了push request
  2. 【转载】什么时候该选C语言实现业务逻辑,什么时候该选Python?
  3. VBA中数组(Array)与随机数(Rnd)的使用
  4. 关于”要执行请求的操作,WordPress需要访问您网页服务器的权限”
  5. 55个javascript经典用法
  6. python爬虫项目描述怎么写_爬虫项目咋写,爬取什么样的数据可以作为项目写在简历上?...
  7. java学习之路目录(已完结)
  8. java 显示锁_Java 实现一个自己的显式锁Lock(有超时功能)
  9. 第5章 C++内存模型和原子类型操作
  10. Win7 Tortoise SVN安装异常--please install the universal crt first.You can .. windows-update(Kb2999226)