当前位置:网站首页>【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; }
边栏推荐
- Play with grpc - communication between different programming languages
- phpcms实现订单直接支付宝支付功能
- Use lambda function URL + cloudfront to realize S3 image back to source
- Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
- Is the futures company found on Baidu safe? How do futures companies determine the regularity
- [dynamic programming] p1004 grid access (four-dimensional DP template question)
- 我们该如何保护自己的密码?
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
- 数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
- WebSocket(简单体验版)
猜你喜欢

After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse

微服务大行其道的今天,Service Mesh是怎样一种存在?

111. Minimum depth of binary tree

那个很努力的学生,高考失败了……别慌!你还有一次逆袭机会!
![[flask] flask starts and implements a minimal application based on flask](/img/45/77df241c85c4916914a37bb78275a5.png)
[flask] flask starts and implements a minimal application based on flask

【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
![[dynamic programming] interval dp:p1005 matrix retrieval](/img/c9/2091f51b905d2c0ebc978dab3d34d3.jpg)
[dynamic programming] interval dp:p1005 matrix retrieval

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

QT learning management system

Websocket (simple experience version)
随机推荐
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?
Go integrates logrus to realize log printing
Leetcode(69)——x 的平方根
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
Research Report on the development trend and competitive strategy of the global camera filter bracket industry
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
About the use of HTTP cache validation last modified and Etag
Phpcms realizes the direct Alipay payment function of orders
sqlilabs less13
Play with mongodb - build a mongodb cluster
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
日志中打印统计信息的方案
Use of Oracle database objects
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
8 best practices to protect your IAC security!
Scheme of printing statistical information in log
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
算网融合赋能行业转型,移动云点亮数智未来新路标
2022-2-15 learning the imitation Niuke project - post in Section 2
phpcms实现订单直接支付宝支付功能
