poj 1089 Intervals(区间合并问题)
来源:互联网 发布:淘宝网处罚考试50题 编辑:程序博客网 时间:2024/06/05 10:48
There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those intervals may be represented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.
Task
Write a program which:
reads from the std input the description of the series of intervals,
computes pairwise non−intersecting intervals satisfying the conditions given above,
writes the computed intervals in ascending order into std output
Task
Write a program which:
reads from the std input the description of the series of intervals,
computes pairwise non−intersecting intervals satisfying the conditions given above,
writes the computed intervals in ascending order into std output
Input
In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.
Output
The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.
Sample Input
55 61 410 106 98 10
Sample Output
1 45 10
题意就是:找出可以合并输入的闭区间的区间并输出。
#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef pair<int, int> pii;typedef long long ll;const double esp = 1e-5;#define N 50100int n;struct Node {int a, b;int input(void){return scanf("%d%d", &a, &b);}void output(void){printf("%d %d\n", a, b);}}node[N], res[N];bool cmp(Node u, Node v){if (u.a == v.a) {return u.b > v.b;} else {return u.a < v.a;}}int main(int argc, char *argv[]){while (scanf("%d", &n) != EOF) {for (int i = 0; i < n; ++i) {node[i].input();}// 按a升序排列,如果a相同就按b降序排列sort(node, node + n, cmp);int cnt = 0;for (int i = 0; i < n; ++i) {if (i == 0 || node[i].a != node[i - 1].a) {node[cnt++] = node[i];}}int ans = 1;res[0] = node[0];for (int i = 1; i < cnt; ++i) {if (node[i].a <= res[ans - 1].b) {// 防止出现1-5,2-3这种情况if (res[ans - 1].b < node[i].b) {res[ans - 1].b = node[i].b;}} else {res[ans++] = node[i];}}for (int i = 0; i < ans; ++i) {res[i].output();}}return 0;}
接下来是一种简便,便于理解的做法:
#include<iostream>#include <cstdio>#include <algorithm>using namespace std;struct node{public:int x, y;int input(){return scanf("%d%d", &x, &y);}}t[50000];bool cmp(node u, node v) //排序 ,按照左端点由小到大排序,如果左端点相同,就按照右端点从小到大排序 {if (u.x == v.x){return u.y < v.y;}else{return u.x < v.x;}}int main(){int n;int i, j, k;int min, left, right;while(~scanf("%d", &n)) {for (i = 0; i < n; i++){t[i].input();}sort(t, t + n, cmp);left = t[0].x;right = t[0].y;for (i = 1; i < n; i++){if (t[i].x > right) //这两种情况就避免了出现[1 5],[2 3]的情况 {printf("%d %d\n", left, right); left = t[i].x;right = t[i].y;}else if (right < t[i].y) {right = t[i].y;}}printf("%d %d\n", left, right);}return 0;}
0 0
- poj 1089 Intervals(区间合并问题)
- LeetCode | Merge Intervals(合并区间间隔)
- LeetCode 56. Merge Intervals(合并区间)
- Leetcode 56 - Merge Intervals(区间合并)
- 【区间合并】Merge Intervals
- Merge Intervals 合并区间
- 合并区间 Merge Intervals
- POJ 3680 Intervals(区间k覆盖问题)
- POJ 1089 Intervals 区间覆盖+ 贪心
- [POJ 1201] Intervals (区间贪心选点)
- 【Intervals POJ 区间覆盖】
- Merge Intervals 合并区间@LeetCode
- Merge Intervals 多区间合并
- POJ 3225 Help with Intervals(区间更新 + 倍增区间)
- LeetCode OJ 之 Merge Intervals(合并区间)
- 156.Merge Intervals-合并区间(容易题)
- Problem 60: Intervals(划分区间问题)
- POJ 3680 Intervals 区间覆K次 网络流问题
- Java学习笔记------BufferedLine类的readLine()方法 使用须知
- HDU A Mathematical Curiosity
- 堆排序(初学者)
- 【物理白痴进化论,欢迎吐槽】键盘按键原理及应用
- Parcelable接口的使用
- poj 1089 Intervals(区间合并问题)
- 快速幂
- Topcoder SRM 663 Div2 Hard: CheeseRolling(状压DP)
- 解决 Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’的问题
- POJ 1321 棋盘问题 dfs
- 树的遍历方式(不知道对不对)
- CodeForces 545A
- C++中引用(&)的用法和应用实例
- Spark100期公益大讲堂