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
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 ;
}
1089 Intervals(中文)的更多相关文章
- poj 1089 Intervals
http://poj.org/problem?id=1089 Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 1089 Intervals(中文版)
开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思. 试题描述 输入一个n,然后输入n组数据,每个数据有两个数,代表这个闭区间是从几到几.然后看,如果任意两个闭区间有相重 ...
- POJ 1089 Intervals【合并n个区间/贪心】
There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those interv ...
- 51nod1089(最长回文子串之manacher算法)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- Openfire Strophe开发中文乱码问题
网站上有很多Openfire Web方案,之前想用Smack 但是jar包支持客户端版本的,还有JDK版本问题 一直没调试成功 估计成功的方法只能拜读源码进行修改了. SparkWeb 官网代码很 ...
- Documentation/sched-bwc.txt 的中文翻译
Chinese translated version of Documentation/sched-bwc.txt If you have any comment or update to the c ...
- 【D3 API 中文手冊】
[D3 API 中文手冊] 声明:本文仅供学习所用,未经作者同意严禁转载和演绎 <D3 API 中文手冊>是D3官方API文档的中文翻译. 始于2014-3-23日,基于VisualCre ...
- win10 环境 gitbash 显示中文乱码问题处理
gitbash 是 windows 环境下非常好用的命令行终端,可以模拟一下linux下的命令如ls / mkdir 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...
随机推荐
- android中全局异常捕捉
android中全局异常捕捉 只要写代码就会有bug,但是我们要想办法收集到客户的bug.有第三方bugly或者友盟等可以收集.但是,android原生就提供了有关收集异常的api,所以我们来学习一下 ...
- Linux嵌入式开发中常用的两个工具
TFTP 全称:Trivial File Transfer Protocol(简单文件传输协议) 进行小文件传输 在ubuntu下设置TFTP服务器 $ sudo apt-get install tf ...
- (二)一起学 Java Collections Framework 源码之 AbstractCollection
. . . . . 目录 (一)一起学 Java Collections Framework 源码之 概述(未完成) (二)一起学 Java Collections Framework 源码之 Abs ...
- [.NET] 《C# 高效编程》(一) - C# 语言习惯
C# 语言习惯 目录 一.使用属性而不是可访问的数据成员 二.使用运行时常量(readonly)而不是编译时常量(const) 三.推荐使用 is 或 as 操作符而不是强制类型转换 四.使用 Con ...
- C语言学习的第一章
首先,学习编写程序要先知道什么是程序,我们为什么要写程序? 程序就是为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合.程序里有很多算法,算法是解决问题的具体方法和步骤,就像我们想要得 ...
- Linux 基础(2)
Linux 基础(二) 用户 组 及权限的相关操作 一.useradd命令选项:–u:指定用户的UID useradd –u 100 mu #指定mu的UID为100–g:指定用户所属的群组 user ...
- 最新的css3动画按钮效果
效果演示 插件下载
- 【代码学习】PHP中GD库的使用
PHP--GD库 ================================================ 一.支持: 需要php支持GD库 二.作用: 验证码.水印.缩放等 三.绘画步骤: ...
- AOJ/搜索递归分治法习题集
ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...
- mui开发app之自定义事件以更新其他页内容
我之前做过jquery mobile的开发,那还是前年的事情 在jquery mobile中,由于页面是存储在div[data-role=page]的dom中(jqmobile通过对data-role ...