当前位置:网站首页>acwing 803. Interval merging
acwing 803. Interval merging
2022-06-12 16:17:00 【_ Liuxiaoyu】
Given n Intervals [li,ri], It is required to merge all intersecting intervals .
Note that if you intersect at the end , There is also an intersection .
Output the number of intervals after merging .
for example :[1,3] and [2,6] Can be merged into one interval [1,6].
Input format
The first line contains integers n.
Next n That's ok , Each line contains two integers l and r.
Output format
All in one line , Contains an integer , Indicates the number of intervals after the consolidation interval is completed .
Data range
1≤n≤100000,
−109≤li≤ri≤109
sample input :
5
1 2
2 4
5 6
7 8
7 9
sample output :
3
Topic diagram : Here, the same endpoint can be merged 
Question making steps :
- Sort according to the left end point of the interval
- When an interval is a blue line , Only green will appear in the subsequent interval 3 In this case , The red condition will not occur

- In the first case, there is no need to update the interval , The second case will extend the previous interval , After the third situation occurs , The previously maintained interval will be saved , In the latter case, the current interval is maintained ( The maintenance interval is updated to the third case ).
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n;
vector<PII> segs;
void merge(vector<PII>& segs)
{
vector<PII> re;
sort(segs.begin(), segs.end()); // c++ Chinese vs pair Sort first sort by left endpoint
int st = -2e9, ed = -2e9;
for(auto seg : segs)
if(ed < seg.first) // The third case
{
if(st != -2e9) re.push_back({
st, ed});
st =seg.first, ed = seg.second; // Update maintenance interval
}
else ed = max(ed, seg.second); // First of all 、 II. Two cases
//if(st != -2e9) // It seems that you can do it without this AC
re.push_back({
st,ed}); // Submit the last interval
segs = re;
}
int main()
{
cin >> n;
for(int i =0; i < n; i ++)
{
int l, r;
cin >> l >> r;
segs.push_back({
l, r});
}
merge(segs);
cout << segs.size() << endl;
return 0;
}
边栏推荐
- ER diagram made by StarUML based on the last student achievement management system
- UE4 common type conversion
- Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
- acwing 803. 区间合并
- 第一章 线性表
- tinyint和int区别
- < 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
- 位运算例题(待续)
- Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
- (4) Googlenet replay
猜你喜欢

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)

acwing 800. 数组元素的目标和
![Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]](/img/48/714f1712f4c2d727dd49cbc6631abf.jpg)
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]

Project training of Software College of Shandong University rendering engine system basic renderer (IV)
![In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]](/img/dd/1bf44d284c709b6bebd4b308ba2cee.jpg)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]

从斐波那契数列求和想到的俗手、本手和妙手

acwing 803. 区间合并

ER diagram made by StarUML based on the last student achievement management system

Project training of Software College of Shandong University rendering engine system basic renderer (V)

Project training of Software College of Shandong University rendering engine system basic renderer (III)
随机推荐
Project training of Software College of Shandong University rendering engine system basic renderer (V)
acwing 797 差分
Project training of Software College of Shandong University rendering engine system point cloud processing (10)
Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
Interview: hashcode() and equals()
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version
面试:了解装箱和拆箱操作吗?
d的sha6转大整
Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL blob and text types
IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码
Apache kylin Adventure
d结构用作多维数组的索引
Why doesn't Alibaba recommend MySQL use the text type?
C packing and unpacking
面试:什么是浅拷贝、深拷贝?
acwing 802. 区间和 (离散化)
acwing 803. 区间合并