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 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...
随机推荐
- png、jpg、gif三种图片格式的区别
png.jpg.gif三种图片格式的区别 2014-06-17 为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布.雕塑家要了解他的石材一样,作为网页设计师也应该对图片格式的特性有一定 ...
- 监听器的小示例:利用HttpSessionListener和HttpServletContextListener实现定时销毁HttpSession
1.创建MyServletContextListener实现HttpServletContextListener接口 @Override public void contextDestroyed(Se ...
- java中json和字符串互转及日期转换 练习
一:以下是用到的jar名称: commons-beanutils-1.6.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons- ...
- Java 8 Lambda 表达式
Lambda 是啥玩意 简单来说,Lambda 就是一个匿名的方法,就这样,没啥特别的.它采用一种非常简洁的方式来定义方法.当你想传递可复用的方法片段时,匿名方法非常有用.例如,将一个方法传递给另外一 ...
- POJ 3294 出现在至少K个字符串中的子串
在掌握POJ 2774(两个串求最长公共子串)以及对Height数组分组后,本题还是容易想出思路的. 首先用字符集外的不同字符连接所有串,这是为了防止两个后缀在比较时超过某个字符串的分界.二分子串的长 ...
- Elasticsearch - 快速入门
Elasticsearch是基于Apache 2.0开源的实时.分布式.分析搜索引擎,相比Lucene,Elasticsearch的上手比较容易,这篇文章主要纪录Elasticsearch的基本概念和 ...
- 热爱H5
我不去想是否能够功成名就既然选择了前端便只顾埋头于无声 我不去想能否赢得掌声既然钟情于IT就勇敢得踏步前行 我不去想前行的路上会不会日暮穷途既然目标是未来留给现在的只能是汗水 我不去想所谓的成功是不是 ...
- 转Fiddler 构造http请求
今天使用Fiddler构造一个POST请求,server端的PHP脚本的 $_POST数组中怎么也获取不到值,后来偶然发现是因为缺少了一个http头:Content-Type: application ...
- VR全景:互联网与实体店的完美结合
VR元年已过,VR项目.VR创业潮转为理性,VR行业分为两个方向:硬件和内容.硬件又分为VR头显和辅助设备,内容又分为VR全景和VR虚拟内容,如游戏.娱乐.根据行业划分为VR+购物,VR+教育,VR ...
- Servlet简单总结(一)
一.Servlet简单总结 1.1. 什么是Servlet Servlet是JavaEE三大组建之一,是使用Java语言编写服务器端的程序,主要用来处理Web应用程序中的请求-响应.Servlet并没 ...