当前位置:网站首页>【15. 区间合并】
【15. 区间合并】
2022-07-01 14:28:00 【小呆鸟_coding】
区间合并
题目
给定 n 个区间 [li,ri]要求合并所有有交集的区间。
注意如果在端点处相交,也算有交集。
输出合并完成后的区间个数。
例如:[1,3]和 [2,6]可以合并为一个区间 [1,6]。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含两个整数 l 和 r。
输出格式
共一行,包含一个整数,表示合并区间完成后的区间个数。
数据范围
1 ≤ n ≤ 100000
−109≤ li ≤ ri≤109输入样例:
5 1 2 2 4 5 6 7 8 7 9输出样例:
3
代码
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef pair<int, int> PII; const int N = 100010; int n; vector<PII> segs; void merge(vector<PII> &segs) { vector<PII> res; //存的是区间合并后的结果 sort(segs.begin(), segs.end()); //排序,pair排序优先以左端点排序,然后再以右端点排序 int st = -2e9, ed = -2e9; // 当还没有遍历任何区间,首先可以设置一个边界值 for (auto seg : segs) if (ed < seg.first) //判断有没有交集 { if (st != -2e9) res.push_back({ st, ed}); //不能是最开始我们初始的区间,假如没有交集,就加到答案中去 st = seg.first, ed = seg.second; //更新区间 } else { ed = max(ed, seg.second); //此时有交集,需要把右端点,更新成较长的那个值 //cout << ed << seg.second <<endl; } if (st != -2e9) res.push_back({ st, ed}); //将最后一个区间加到答案中去。判断主要是防止我们输入的数组中,没有任何区间的,防止是空的 segs = res; } int main() { cin >> n; for (int i = 0; i < n; i ++) { int l, r; cin >> l >> r; segs.push_back({ l, r}); } merge(segs); //进行区间合并 // for (auto ses : segs) // { // cout << ses.first << ses.second << endl; // } cout << segs.size() << endl; return 0; }
边栏推荐
- Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
- 我们该如何保护自己的密码?
- Advanced C language
- sqlilabs less9
- TDengine 连接器上线 Google Data Studio 应用商店
- leetcode622. Design cycle queue (C language)
- Don't want to knock the code? Here comes the chance
- Using CMD to repair and recover virus infected files
- Leetcode(69)——x 的平方根
- 用栈实现队列、用队列实现栈(C语言_leetcode_232+225)
猜你喜欢

Blog recommendation | in depth study of message segmentation in pulsar

Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界

643. Maximum average number of subarrays I

Basis of target detection (NMS)

【R语言数据科学】:机器学习常见评估指标

241. 为运算表达式设计优先级

QT learning management system

博文推荐 | 深入研究 Pulsar 中的消息分块

用栈实现队列、用队列实现栈(C语言_leetcode_232+225)

使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
随机推荐
Don't want to knock the code? Here comes the chance
被裁三个月,面试到处碰壁,心态已经开始崩了
2022-2-15 learning the imitation Niuke project - post in Section 2
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
Research Report on the development trend and competitive strategy of the global navigation simulator industry
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
Basic operation of queue (implemented in C language)
Scheme of printing statistical information in log
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
Basis of target detection (NMS)
日志中打印统计信息的方案
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
How will the surging tide of digitalization overturn the future?
【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
How to pass array parameters in get request
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
leetcode622. Design cycle queue (C language)
