Codeforces35E(扫描线)
E. Parade
No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on n sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the i-th building is a rectangle of height hi. Its westernmost point has the x-coordinate of li and the easternmost — of ri. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an envelopingpolyline using the data on the sky-scrapers. The polyline’s properties are as follows:
- If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface.
- The polyline starts and ends on the land level, i.e. at the height equal to 0.
- The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal.
- The polyline’s vertices should have integer coordinates.
- If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area.
- The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land.
- The consecutive segments of the polyline must be perpendicular.
Picture to the second sample test (the enveloping polyline is marked on the right).
Input
The first input line contains integer n (1 ≤ n ≤ 100000). Then follow n lines, each containing three integers hi, li, ri(1 ≤ hi ≤ 109, - 109 ≤ li < ri ≤ 109).
Output
In the first line output integer m — amount of vertices of the enveloping polyline. The next m lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0.
Examples
input
- 2
3 0 2
4 1 3
output
- 6
0 0
0 3
1 3
1 4
3 4
3 0
input
- 5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8
output
- 14
-3 0
-3 3
0 3
0 2
1 2
1 0
2 0
2 4
4 4
4 2
6 2
6 3
8 3
8 0
- //2017-08-11
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <set>
- #include <vector>
- using namespace std;
- int n;
- multiset<int> ms;
- vector< pair<int, int> > line, ans;
- void init()
- {
- ms.clear();
- line.clear();
- ans.clear();
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- while(scanf("%d", &n)!=EOF)
- {
- init();
- int l, r, h;
- for(int i = ; i < n; i++){
- scanf("%d%d%d", &h, &l, &r);
- line.push_back(make_pair(l, h));
- line.push_back(make_pair(r, -h));
- }
- sort(line.begin(), line.end());
- vector< pair<int, int> >::const_iterator it, iter;
- it = line.begin();
- int height = ;
- ms.insert(height);
- while(it != line.end()){
- iter = it;
- do{
- if(iter->second > )
- ms.insert(iter->second);
- else
- ms.erase(ms.find(-iter->second));
- iter++;
- }while(iter != line.end() && iter->first == it->first);
- if(*ms.rbegin() != height){
- ans.push_back(make_pair(it->first, height));
- ans.push_back(make_pair(it->first, height = *ms.rbegin()));
- }
- it = iter;
- }
- printf("%d\n", (int)ans.size());
- for(it = ans.begin(); it != ans.end(); it++){
- printf("%d %d\n", it->first, it->second);
- }
- }
- }
Codeforces35E(扫描线)的更多相关文章
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- HUD 4007 [扫描线][序]
/* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...
- Atitit 图像扫描器---基于扫描线
Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...
- 扫描线+堆 codevs 2995 楼房
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...
- 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 440 Solved: 16 ...
- 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)
4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 61 Solved: 26[Submi ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 线段树基础模板&&扫描线
线段树的单点更新+区间求和 hdu1166敌兵布阵 Input 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=),表示敌人有N个工兵营地 ,接下来有N个正整数,第i个正整 ...
随机推荐
- Django(ORM查询2)
day70 ORM训练专题 :http://www.cnblogs.com/liwenzhou/articles/8337352.html 内容回顾 1. ORM 1. ORM ...
- 多个SpringMVC项目配置统一管理(来自于springCloud的统一配置思路)
因公司项目分多个系统进行开发,而系统架构几乎完全一样,所以同样的配置文件会存在不同的系统中 当其中的某些配置需要修改时,就需要依次把所有系统中相关的配置都修改掉 纯耗时且没技术含量的体力活 所以借鉴S ...
- [Umbraco] document type里的父节点与子节点的设置
虽然我们不能像做数据库设计那样建立主外键关系.但我们建立xml里父子关系,父子关系其实是指是否允许在一个页面(如频道,分类,栏目等)下创建子页面,这就相当于建立站点的树状结构,对于筛选数据会有很大的作 ...
- node-rsa非对称加密
写在最前:此文的目的是介绍编码,减少刚接触时的弯路,所以内容且不做详细累述 一.使用 node-rsa 进行非对称加解密 因为 比特币 中使用的非对称加密,所以在npm中对比找到一个比较方便也直观的库 ...
- DWZ中刷新dialog的方案解决
在DWZ中进行ajax表单提交后,通过回调函数来返回状态结果,以及返回是否需要刷新父页的navTabId. DWZ给我们提供了两个回调函数,一个是子窗口为navTab的navTabAjaxDone,一 ...
- RVM的安装和使用过程中碰到的问题
Ruby Version Manager简称RVM,是一款非常好用的ruby版本管理以及安装工具. 关于rvm的安装,可以参考以下文章: use rvm install and manage ruby ...
- netty客户端源码
随笔记录. //创建一个ChannelFactory(客户端代码) ChannelFactory factory = new NioClientSocketChannelFactory( Execut ...
- java多线程并发控制countDownLatch和cyclicBarrier的使用
java主线程等待所有子线程执行完毕在执行,这个需求其实我们在工作中经常会用到,比如用户下单一个产品,后台会做一系列的处理,为了提高效率,每个处理都可以用一个线程来执行,所有处理完成了之后才会返回给用 ...
- elk-nginx输出json格式的日志
把Nginx日志的格式输出成JSON格式展示在Kibana面板,生产环境中基本都是这么使用. 1, 配置nginx 主要修改nginx的访问日志格式,这里定义成json格式,以便后面logstash更 ...
- 面试:用快排实现数组中的第K大的数
#include <iostream> #include <cassert> using namespace std; int selectKth(int a[],int st ...