当前位置:网站首页>acwing 803. 区间合并
acwing 803. 区间合并
2022-06-12 16:09:00 【_刘小雨】
给定 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
题目示意图:在这里是端点相同也是可以合并的
做题步骤:
- 按照区间左端点排序
- 当一个区间是蓝色线时, 后续区间只会出现绿色的3种情况,红色情况不会出现

- 第一种情况不用更新区间,第二种情况会延长之前区间,第三情况出现后,就会把之前维护的区间保存起来,后面情况就维护当前区间(维护区间更新为第三种情况)。
#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++中对pair 排序优先根据左端点排序
int st = -2e9, ed = -2e9;
for(auto seg : segs)
if(ed < seg.first) // 第三种情况
{
if(st != -2e9) re.push_back({
st, ed});
st =seg.first, ed = seg.second; // 更新维护区间
}
else ed = max(ed, seg.second); // 第一、二两种情况
//if(st != -2e9) // 这里好像不用加这个也能AC
re.push_back({
st,ed}); // 提交最后一个区间
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;
}
边栏推荐
- 一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
- 思考游戏王决斗链接中抽卡概率问题
- IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码
- With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough
- [thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
- Instructions de soumission des tâches télécharger les tâches sur le disque réseau
- (四)GoogleNet複現
- Global and Chinese market of soft capsule manufacturing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
- Global and Chinese markets for air sampling calibration pumps 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Golang collaboration scheduling (I): Collaboration Status

Application of postman-rest client plug-in

IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码

When programming is included in the college entrance examination...

Broadcast and multicast (tcp/ip details volume 1/2)

Axure RP 9 for MAC (interactive product prototyping tool) Chinese version

Writing code can also be classified as "manual" or "vulgar", and we should be good at finding good hands!

C语言 分割bin文件程序

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
![[practical case of light source] UV-LED curing innovation makes the production line more smooth](/img/6f/04f52a37782c54b1050f908f46eadf.png)
[practical case of light source] UV-LED curing innovation makes the production line more smooth
随机推荐
Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
面试:什么是浅拷贝、深拷贝?
Escape rules and examples of go
What is JUC in high concurrency programming
Job submission instructions upload jobs to network disk
< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small
Example of bit operation (to be continued)
Learning record [email protected] understand canvas
Saga architecture pattern: implementation of cross service transactions under microservice architecture
Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
go net库(待续)
Redis string type common commands
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
Five models of software testing
借助SpotBugs将程序错误扼杀在摇篮中
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
Project training of Software College of Shandong University rendering engine system basic renderer (III)
Keep an IT training diary 067- good people are grateful, bad people are right
Match single character