开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思。 
试题描述 
输入一个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[];
bool maxn(node a,node b)
{
return a.head<b.head;
}
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
cin>>input[i].head>>input[i].tail;
sort(input,input+n,maxn);
int shead=input[].head,stail=input[].tail;
for(int i=;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 ;
}

最新文章

  1. iframe和form表单的target应用简单例子
  2. Phonegap在ios7上系统状态栏的问题解决
  3. CVU介绍
  4. hdu3746
  5. 微信ios版6.2更新 聊天记录迁移更快捷朋友圈可翻译
  6. SharePoint 2010 母版页制作的简单介绍
  7. sharepoint的webpart开发
  8. VC++界面编程之--使用分层窗口实现界面皮肤
  9. iScroll 下 a 标签失效
  10. LCD_FSMC
  11. 聊聊数据库~2.SQL环境篇
  12. spring boot 2.0.3+spring cloud (Finchley)3、声明式调用Feign
  13. Character流与Byte流的区别(转)
  14. centos6.5环境搭建openvp服务器及windows客户端搭建及配置详解
  15. 怎样解决WampServer #1405 - Access denied for user &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39; (using password: NO)
  16. Unity3D学习笔记(二十二):ScrollView和事件接口
  17. 201621123010 《Java程序设计》第2周学习总结
  18. 数据库中MCO
  19. Android开源框架-Annotation框架(以ViewMapping注解为例)
  20. asp.net core mvc视频A:笔记3-2.表单使用

热门文章

  1. Felx布局(三)
  2. Linux--线程安全与可重入函数的异同
  3. ubuntu 下安装Angular2-cli脚手架
  4. 【内网渗透】MSF的exploit和pyload的基础使用
  5. 蓝桥杯-打印大X-java
  6. scp 命令快速使用讲解
  7. 与redmine对接
  8. CI Weekly #19 | 关于软件开发模型的思考,以及最新 CI/CD 实践分享
  9. 【持久化框架】Mybatis简介与原理
  10. kotlin 语言入门指南一