当前位置:网站首页>[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; }
边栏推荐
- 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?
- Basic knowledge of C language
- Develop small programs and official account from zero [phase III]
- Build your own website (21)
- Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
- sqlilabs less-11~12
- Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
- Research Report on the development trend and competitive strategy of the global aviation leasing service industry
- 券商万1免5证券开户是合理安全的吗,怎么讲
- C 语言进阶
猜你喜欢
MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
Fundamentals of C language
Build your own website (21)
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
户外LED显示屏应该考虑哪些问题?
被裁三個月,面試到處碰壁,心態已經開始崩了
"National defense seven sons" funding soared, with Tsinghua reaching 36.2 billion yuan, ranking second with 10.1 billion yuan. The 2022 budget of national colleges and universities was made public
[dynamic programming] interval dp:p1005 matrix retrieval
leetcode622. Design cycle queue (C language)
随机推荐
Details of appium key knowledge
Research Report on the development trend and competitive strategy of the global axis measurement system industry
百度上找的期货公司安全吗?期货公司怎么确定正规
Guess lantern riddles, not programmers still can't understand?
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
Provincial election + noi Part IX game theory
Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
30 Devops interview questions and answers
Provincial election + noi Part VIII fraction theory
TexStudio使用教程
Tdengine connector goes online Google Data Studio app store
Research Report on development trend and competitive strategy of global consumer glassware industry
After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
[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
Pat 1065 a+b and C (64bit) (20 points) (16 points)
【15. 区间合并】
【R语言数据科学】:机器学习常见评估指标
原来程序员搞私活这么赚钱?真的太香了
qt捕获界面为图片或label显示
生成随机数(4位、6位)