1089 Intervals

开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思。 
试题描述 
输入一个n,然后输入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 
输入 
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. 
输出 
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. 
输入示例 

5 6 
1 4 
10 10 
6 9 
8 10 
输出示例 
1 4 
5 10 
解题思路 
这是一道贪心,大概就是把每个闭区间的开始的数排序,然后从小开始,如果这个开始数最小的区间的最后一个数比第二小的区间的第一个数大,那么就将这两个区间合并,如果比它小就将已有的区间输出,以此类推。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct node
{
    int head,tail;
}input[1000010];
bool maxn(node a,node b)
{
    return a.head<b.head;
}
int main()
{
     int n;
     cin>>n;
     for(int i=0;i<n;i++)
         cin>>input[i].head>>input[i].tail;
     sort(input,input+n,maxn);
     int shead=input[0].head,stail=input[0].tail;
     for(int i=1;i<n;i++)
     {
         if(input[i].head>stail)
         {
             cout<<shead<<" "<<stail<<endl;
             shead=input[i].head;
             stail=input[i].tail;
         }
         else
             stail=max(stail,input[i].tail);
     }
     cout<<shead<<" "<<stail;
     return 0;
}
时间: 2024-10-16 00:11:51

1089 Intervals的相关文章

1089 Intervals(中文版)

开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思. 试题描述 输入一个n,然后输入n组数据,每个数据有两个数,代表这个闭区间是从几到几.然后看,如果任意两个闭区间有相重的地方,那么就把这两个闭区间合并,最后输出没有合并的闭区间. 试题描述正版 There is given the series of n closed intervals [ai; bi], where i=1,2,…,n. The sum of those intervals may be rep

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 inte

PKU1201 Intervals

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 points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

POJ1201 Intervals查分约束系统(最短路)

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 points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

ural 1346. Intervals of Monotonicity

1346. Intervals of Monotonicity Time limit: 1.0 secondMemory limit: 64 MB It’s well known that a domain of any continuous function may be divided into intervals where the function would increase monotonically or decrease monotonically. A number of in

poj 3225 Help with Intervals(线段树)

题目链接:poj 3225 Help with Intervals 题目大意:模拟集合操作,输出最终的集合. 解题思路:线段树. U l r:[l,r]区间置为1 I l r:[0,l),(r,maxn]置为0 D l r:[l,r]区间置为0 C l r:[0,l),(r,maxn]置为0,[l,r]区间取逆 S l r:[l,r]区间取逆. 然后基本水水的线段树,注意一下区间开和闭. #include <cstdio> #include <cstring> #include &

leetcode || 56、 Merge Intervals

problem: 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]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

POJ1375 Intervals(直线与圆切线、线段合并)

题目链接: http://poj.org/problem?id=1375 题目描述: Intervals Description In the ceiling in the basement of a newly open developers building a light source has been installed. Unfortunately, the material used to cover the floor is very sensitive to light. It

(线段树)poj3225-Help with Intervals

LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating time intervals