当前位置:网站首页>【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; }
边栏推荐
- In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
- After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
- Provincial election + noi Part 10 probability statistics and polynomials
- SWT / anr problem - how to capture performance trace
- sqlilabs less-11~12
- 【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
- Basic knowledge of C language
- [Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
- Research Report on the development trend and competitive strategy of the global navigation simulator industry
- Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
猜你喜欢

Details of appium key knowledge

This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI

数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体

That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!

How will the surging tide of digitalization overturn the future?
![[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize

643. Maximum average number of subarrays I

如何看待国企纷纷卸载微软Office改用金山WPS?
![[dynamic programming] interval dp:p1005 matrix retrieval](/img/c9/2091f51b905d2c0ebc978dab3d34d3.jpg)
[dynamic programming] interval dp:p1005 matrix retrieval

日志中打印统计信息的方案
随机推荐
[dynamic programming] p1004 grid access (four-dimensional DP template question)
MySQL日志
SQLAchemy 常用操作
Vnctf2022 open web gocalc0
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
微服务大行其道的今天,Service Mesh是怎样一种存在?
The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
111. Minimum depth of binary tree
Research Report on the development trend and competitive strategy of the global chemical glassware industry
被裁三個月,面試到處碰壁,心態已經開始崩了
TexStudio使用教程
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
Provincial election + noi Part VIII fraction theory
Guess lantern riddles, not programmers still can't understand?
QT community management system
[R language data science]: common evaluation indicators of machine learning
2022-2-15 learning the imitation Niuke project - post in Section 2
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
Provincial election + noi Part 10 probability statistics and polynomials
Using CMD to repair and recover virus infected files
