题目大意:

Input

* Line 1: Two space separated integers: N and W

* Lines 2..N+1: Two space separated integers, the x and y coordinate of a point where the skyline changes. The xcoordinates are presented in strictly increasing order, and the first x coordinate will always be 1.

Output

* Line 1: The minimum number of buildings to create the described skyline.

Sample Input

10 26
1 1
2 2
5 1
6 3
8 1
11 0
15 2
17 3
20 2
22 1

Sample Output

6

Hint

INPUT DETAILS:

The case mentioned above

需要找到规律

题解:
模拟一个单调栈,使栈内的元素递增。
当新进来一个元素x时,while栈顶的元素大于当前x,ans++,将栈顶元素弹出
如果栈顶元素等于x则只保留一个,ans不变
最后加进去一个0元素把栈按照上述方式清空即可

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,w;
while(~scanf("%d%d",&n,&w))
{
stack <int> s;
while(!s.empty()) s.pop();
int a,b,ans=;
for(int i=;i<=n;i++)
{
if(i==n) b=;
else scanf("%d%d",&a,&b);
if(!s.empty())
{
while(!s.empty()&&b<s.top())
{
ans++;
s.pop();
}
if(!s.empty()&&b==s.top()) s.pop();
s.push(b);
}
else s.push(b);
}
printf("%d\n",ans);
} return ;
}

USACO2005 City Skyline /// oj23401的更多相关文章

  1. BZOJ1628: [Usaco2007 Demo]City skyline

    1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Su ...

  2. Leetcode 807 Max Increase to Keep City Skyline 不变天际线

    Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...

  3. LC 807. Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  4. LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线

    https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...

  5. [USACO2005][POJ3044]City Skyline(贪心+单调栈)

    题目:http://poj.org/problem?id=3044 题意:以坐标的形式给出一张图,表示一些楼房的正视图,求出楼房的最少个数. 分析:和小学常做的立方体问题很像,很容易想到一个贪心方法, ...

  6. bzoj1683[Usaco2005 Nov]City skyline 城市地平线

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...

  7. [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...

  8. [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  9. [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

随机推荐

  1. flask json

    导入 from flask import Flask,jsonify 1.列表 def index(): arr=['mkdir','md','touch'] return jsonify(arr) ...

  2. vue2.0使用基础

    开发情况下需要引入vue.js和vue-resource.js,el:dom生效范围,data,dom静态数据,mounted:初始化调用方法,注意,官方文档需要添加this.$nextTict(fu ...

  3. shell cat 合并文件,合并数据库sql文件

    > 覆盖写入 >> append模式写入 ###################################################################合并数 ...

  4. Windows下安装PIL进行图像处理

    过程一波三折 参考 http://blog.csdn.net/zxia1/article/details/8254113 http://stackoverflow.com/questions/3544 ...

  5. hbase之setCaching 和 setBatch 和setMaxResultSize

    scan的setBatch()用法 val conf = HBaseConfiguration.create() val table: Table = ConnectionFactory.create ...

  6. JUC源码分析-线程池篇(二)FutureTask

    JUC源码分析-线程池篇(二)FutureTask JDK5 之后提供了 Callable 和 Future 接口,通过它们就可以在任务执行完毕之后得到任务的执行结果.本文从源代码角度分析下具体的实现 ...

  7. 从模块到python文件的两种用法

    01模块的四种形式 模块 就是从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名是test.py的话,它的对应模块名就是test) 包 用 ...

  8. eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数

    题目大意: 给定n m 接下来给定m个在n范围内的段的左右端 l r 求选取m-2段 最多能覆盖多少格 #include <bits/stdc++.h> using namespace s ...

  9. JS 将对象转换成字符 字符串转换成json对象

    //js对象 var user = { "name": "张学友", "address": "中国香港" }; //将对 ...

  10. 关于memset赋值无穷大无穷小

    memset(a,,sizeof(a)); 即得到无穷大. memset(a,,sizeof(a)); 即得到无穷小,与上述的值互为相反数. memset(a,,sizeof(a)); 即近似为第一个 ...