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 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...
随机推荐
- nodejs将JSON字符串转化为JSON对象
如何将JSON字符串转化为JSON对象? JSON.parse(str) JSON是javascript的一个内置对象,提供了转换JSON对象与字符串互相转换的方法: 问题来了,道理我都懂 ...
- Webdriver API之操作(一)
一. 控制浏览器 1. 控制浏览器大小 driver.set_window_size(480,800) #浏览器宽480,高800显示 dirver.maximize_window() #浏览器最大化 ...
- 网络语音视频技术浅议(附多个demo源码下载)
我们在开发实践中常常会涉及到网络语音视频技术.诸如即时通讯.视频会议.远程医疗.远程教育.网络监控等等,这些网络多媒体应用系统都离不开网络语音视频技术.本人才疏学浅,对于网络语音视频技术也仅仅是略知皮 ...
- ES6 深入let的作用域
说到ES6的let变量声明,我估计很多人会想起下面几个主要的特点: 没有变量声明提升 拥有块级作用域 暂时死区 不能重复声明 很多教程和总结基本都说到了这几点(说实话大部分文章都大同小异,摘录的居多) ...
- React服务器渲染最佳实践
源码地址:https://github.com/skyFi/dva-starter React服务器渲染最佳实践 dva-starter 完美使用 dva react react-router,最好用 ...
- const常量类型
1.定义:const常量类型表示一个”常值变量“,其值是不能被修改的变量.即一旦变量被声明为const类型,编译器将禁止任何试图修改该变量的操作. 2.声明:const <声明数据类型> ...
- STM8驱动HX711
普及:HX711AD一款专为高精度电子秤而设计的 24 位 A/D 转换器芯片: 获取数据方法:两个普通IO DOUT输入:GPIO_Mode_In_FL_N ...
- Webgis开源解决方案之环境搭建(三)
前两篇文章介绍了开源GIS服务器Geoserver的安装以及开源数据库Postgres的安装,这篇文章介绍开源前端udig软件的安装,使用在后续篇幅中介绍 开源前端目前主流的是QGIS或者udig,网 ...
- 2017Unity开发者大会备受关注的原因有哪些?
Unite大会是由Unity举办的全球开发者大会,至今已有10年的历史.从最开始Unity开发者大会仅500人,到现在Unity大会已经增长到5000人,10倍的参与人数,Unity开发者大会仅仅用了 ...
- jenkins 设置自动发送邮件
1.测试邮件发送 进入jenkins系统管理-->系统设置做如下配置 1.设置系统管理员邮件地址,这是一个全局变量,意味着所有的jenkins执行的任务,最后都会通过这个邮件地址发送邮件 ...