哈理工oj 1756Merge Intervals【结构体 + 排序】
来源:互联网 发布:团队协作工具linux 编辑:程序博客网 时间:2024/05/17 05:51
Merge IntervalsTime Limit: 1000 MSMemory Limit: 65535 KTotal Submit: 65(30 users)Total Accepted: 32(28 users)Rating: Special Judge: NoDescription
Given a collection of intervals, merge all overlapping intervals.
For example,
Given: [1,3],[2,6],[8,10],[15,18];
after meger: [1,6],[8,10],[15,18].
InputThere are multiple test cases.
For each test case:
Line 1: This line contains an integer n indicating the number of intervals.
Line 2..n+1: Each line contains a pair of integers a and b, indicating the interval [a, b].
1<=n<=100,000
1<=a <= b<=1,000,000,000
OutputOutput one line, contains the intervals separated by space after the merge. Output the intervals in lexicographically smaller way.
In other words, if we output [ai,bi] before [aj,bj], there must be ai < aj or ai=aj and bi <= bj.
For more details, referring to the sample.
Sample Input41 32 68 1015 18537 6529 4330 4231 8732 86Sample Output
[1,6] [8,10] [15,18][29,87]
#include<bits/stdc++.h>using namespace std;struct node{ int x, y;} uv[100005];bool cmp(node a, node b){ if(a.x != b.x) return a.x < b.x; return a.y < b.y;}int main(){ int n; while(cin >> n) { for(int i = 0; i < n; i++) cin >> uv[i].x >> uv[i].y; sort(uv, uv + n, cmp); int op = uv[0].x; int ed = uv[0].y; for(int i = 1; i < n; i++) { if(uv[i].x <= ed) { if(uv[i].y > ed) ed = uv[i].y; } else { printf("[%d,%d] ", op, ed); op = uv[i].x; ed = uv[i].y ; } if(i == n - 1) printf("[%d,%d]\n", op, ed); } } return 0;}
阅读全文
0 0
- 哈理工oj 1756Merge Intervals【结构体 + 排序】
- hrbust 1756/哈理工oj Merge Intervals【水题】
- 哈理工OJ 2284 Sign Deliveries(结构体排序)
- LeetCode OJ:Merge Intervals
- LeetCode OJ - Merge Intervals
- LeetCode OJ Merge Intervals
- [Leetcode OJ] Merge Intervals
- LeetCode OJ 56. Merge Intervals
- leetcode oj java 56. Merge Intervals
- LeetCode OJ 56 Merge Intervals [Medium]
- 哈理工oj 1631 技能修炼【拓扑排序】
- 哈理工OJ 1631 技能修炼(拓扑排序)
- 哈理工OJ 1150 相识(排序后枚举)
- 哈理工OJ 2261 近代化改修(排序+贪心)
- 南洋理工283 对称排序(结构体排序)
- Merge Intervals
- Merge Intervals
- Merge Intervals
- shell脚本语言编程
- 一个简单的对象模型
- intellij idea 集成数据库
- finished with exit code -1073740791 (0xC0000409)
- 文件上传 ectype解释
- 哈理工oj 1756Merge Intervals【结构体 + 排序】
- PHP中 static 静态方法有什么优缺点 静态方法与非静态方法的使用及区别
- DLL调用(2):C++动态调用DLL
- Mybatis in查询传入字符串参数问题
- 阿里的短信验证码
- EOJ 3367/FOJ 2253咸鱼翻身【最大区间和】
- 配合签名特征来定位新版本APP的函数
- mamp pro apache 中文目录浏览乱码
- 关于面向过程和面向对象和面向抽象和……