您的位置:首页 > 其它

poj 1089 Intervals(区间合并问题)

2015-08-08 17:11 399 查看
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


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

5
5 6
1 4
10 10
6 9
8 10


Sample Output

1 4
5 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 50100

int 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
, res
;

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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: