当前位置:网站首页>[15. Interval consolidation]
[15. Interval consolidation]
2022-07-01 14:33:00 【Little silly bird_ coding】
Interval merging
subject
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≤109sample input :
5 1 2 2 4 5 6 7 8 7 9sample output :
3
Code
#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; // Save the result of interval merging sort(segs.begin(), segs.end()); // Sort ,pair Sort first sort by left endpoint , Then sort by the right endpoint int st = -2e9, ed = -2e9; // When you haven't traversed any interval , First, you can set a boundary value for (auto seg : segs) if (ed < seg.first) // Judge whether there is intersection { if (st != -2e9) res.push_back({ st, ed}); // It can't be our initial interval at the beginning , If there is no intersection , Just add it to the answer st = seg.first, ed = seg.second; // Update interval } else { ed = max(ed, seg.second); // At this time, there is intersection , You need to put the right endpoint , Update to the longer value //cout << ed << seg.second <<endl; } if (st != -2e9) res.push_back({ st, ed}); // Add the last interval to the answer . Judgment is mainly to prevent the array we enter , There is no interval , Prevent is empty 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); // Perform interval merging // 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 diamond suspension industry
- 2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
- Force deduction solution summary 241- design priority for operation expression
- Realize queue with stack and stack with queue (C language \leetcode\u 232+225)
- 【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
- This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- 生成随机数(4位、6位)
- Sqlachemy common operations
- 微服务大行其道的今天,Service Mesh是怎样一种存在?
猜你喜欢

Minimum spanning tree and bipartite graph in graph theory (acwing template)

2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle

Tdengine connector goes online Google Data Studio app store

2022 PMP project management examination agile knowledge points (6)

phpcms实现订单直接支付宝支付功能

Vnctf2022 open web gocalc0
![[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial](/img/44/b65aaf11b1e632f2dab55b6fc699f6.jpg)
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial

深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障

【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换

问题随记 —— Oracle 11g 卸载
随机推荐
Minimum spanning tree and bipartite graph in graph theory (acwing template)
Research Report on the development trend and competitive strategy of the global traditional computer industry
sqlilabs less9
光环效应——谁说头上有光的就算英雄
When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
sqlilabs less-11~12
el-form-item 正则验证
2022-2-15 learning the imitation Niuke project - post in Section 2
Blog recommendation | in depth study of message segmentation in pulsar
This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
C 语言基础
Basis of target detection (NMS)
Provincial election + noi Part XI others
111. Minimum depth of binary tree
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
8 best practices to protect your IAC security!
sqlilabs less-8
sqlilabs less13
TDengine 连接器上线 Google Data Studio 应用商店
