当前位置:网站首页>[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 9
sample 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; }
边栏推荐
- 【15. 区间合并】
- 【R语言数据科学】:机器学习常见评估指标
- 2022-2-15 learning the imitation Niuke project - post in Section 2
- How will the surging tide of digitalization overturn the future?
- Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
- 微服务开发步骤(nacos)
- Sqlachemy common operations
- Research Report on the development trend and competitive strategy of the global commercial glassware industry
- Generate random numbers (4-bit, 6-bit)
- 当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
猜你喜欢
2022-2-15 learning the imitation Niuke project - Section 3 post details
sqlilabs less13
Build your own website (21)
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
Sqlachemy common operations
博文推荐 | 深入研究 Pulsar 中的消息分块
[dynamic programming] p1004 grid access (four-dimensional DP template question)
Microservice development steps (Nacos)
深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障
Buuctf reinforcement question ezsql
随机推荐
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
qt捕获界面为图片或label显示
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
Develop small programs and official account from zero [phase III]
【15. 区间合并】
Provincial election + noi Part 10 probability statistics and polynomials
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
Opencv interpolation mode
30 Devops interview questions and answers
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
Research Report on the development trend and competitive strategy of the global CCTV robot industry
Halo effect - who says that those with light on their heads are heroes
Pat 1065 a+b and C (64bit) (20 points) (16 points)
Research Report on the development trend and competitive strategy of the global camera filter bracket industry
Basic operation of queue (implemented in C language)
Websocket (simple experience version)
[stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
[IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
Blog recommendation | in depth study of message segmentation in pulsar