POJ 1089 Intervals【合并n个区间/贪心】

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 <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <list>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
const double PI = acos(-1.0);
const int maxn = 101;
const int INF = 0x3f3f3f3f;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};

int n,m;
int f[maxn],cnt=0,sum=0;
struct node
{
    int l,r;
}a[maxn];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d %d",&a[i].l,&a[i].r);
        sort(a,a+n,cmp);
        int x=a[0].l,y=a[0].r;
        for(int i=0;i<n;i++)
        {
            if(a[i].l>=x && a[i].l<=y)
            {
                if(a[i].r>y)
                    y=a[i].r;
            }
            else
            {
                printf("%d %d\n",x,y);
                x=a[i].l;
                y=a[i].r;
            }
        }
        printf("%d %d\n",x,y);
    }
}
/*

*/

原文地址:https://www.cnblogs.com/Roni-i/p/9565229.html

时间: 2024-08-04 00:48:30

POJ 1089 Intervals【合并n个区间/贪心】的相关文章

POJ 1201 Intervals(差分约束 区间约束模版)

关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至少放w个球, 问最后整个区间最少要放多少个球. 分析: 假设d[i] 是 [1,i] 至少有多少个点被选中, 特殊地, d[0] = 0. 每个区间的描述可以转化为d[R] - d[L-1] >= w.(因为d[L]也要选中, 左闭右闭区间, 所以要减d[L-1])因为d[i]描述了一个求和函数,所

【贪心+区间合并】POJ 1089

Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8270   Accepted: 3268 Description 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 pai

[LeetCode] 56 - Merge Intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 思路: 我们首先要做的就是给区间集排序,由于我们要排序的是个结构体,所以我们要定义自己的comparator,才能用sort来排序,我们以start的值从小到大来排序,排完序我们就可以开始合并了,首先把第一个区间存入结果

lintcode 容易题:Merge Intervals 合并区间

题目: 合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10], [15, 18] [15, 18] ] ]挑战 O(n log n) 的时间和 O(1) 的额外空间. 解题: 先以区间的左边界进行排序,再异步的方式比较右边边界进行合并 程序来源 Java程序 /** * Definition of Interval: * public class Inte

POJ 1201 Intervals(图论-差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end po

POJ 3670 Intervals(费用流)

POJ 3680 Intervals 题目链接 题意:给定一些区间,每个区间有一个权值,要求用这些区间去覆盖,每个点最多覆盖k次,问最多得到权值多少 思路:典型的区间k覆盖问题,区间连边容量1,代价-w,然后其他点相邻两两连边,容量k,代价0,跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm&g

POJ 3680 Intervals 离散 + 费用流

Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6246   Accepted: 2542 Description You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize t

POJ 1149 网络流 合并建图

这个题目我敲了一个简单的EK,这不是难点 难点在于建图,按题目的要求 每个猪圈和顾客都建点的话,那也太多了...我看了Edelweiss里面的缩点方法才建好的图,哎,惭愧啊 实际那些猪圈根本不需要单独建点,猪圈无非就是向顾客输送流量 以及向同时开着的猪圈输送流量,这一步可以直接缩为,当某个猪圈被第一次打开,它里面的流量就全部输送给那个顾客那个点,而且可以叠加,因为每一次猪圈是可以互通的,而且猪圈本身是没有容量限制,如果有限制,那就还得再考虑. 此外,每次对猪圈的接下来的访问者都进行建边.用来输送

HDU 2037 今年暑假不AC (区间贪心)

题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). 知道这个的话,这个问题还不so easy!先对每个节目结束的时间排序,然后一个一个的选,保证没有相交,也就是说当前的开始时间一定要是上一个后面或者上一个刚结束当前就开始就OK了. 代码如下: #include <iostream> #include <cstdio> #include