Content

一条大街上有 \(n\) 个房子,第 \(i\) 个房子的楼层数量是 \(h_i\)。如果一个房子的楼层数量大于位于其右侧的所有房屋,则房屋是豪华的。对于第 \(i\) 个房子,请求出至少要添加多少层才能使这个房子变得豪华。注意,对于所有的房子,其答案互不影响。

数据范围:\(1\leqslant n\leqslant 10^5,1\leqslant h_i\leqslant 10^9\)。

Solution

我们可以由后往前求出从 \(i\) 到 \(n\) 的 \(h_i\) 最大值 \(s_i=\max\{s_{i+1},h_i\}\),然后再记录最大值有变化时的位置,最后很明显,这些房子都不需要盖楼了,否则对于第 \(i\) 个房子,需要盖的楼层数就是 \(s_i+1-a_i\)(\(s_i\) 的含义上面有讲)。

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std; int n, a[100007], maxi[100007], effo[100007]; int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
maxi[n] = a[n], effo[n] = n;
for(int i = n - 1; i >= 1; --i) {
if(a[i] > maxi[i + 1]) {
maxi[i] = a[i];
effo[i] = i;
} else {
maxi[i] = maxi[i + 1];
effo[i] = effo[i + 1];
}
}
for(int i = 1; i <= n; ++i) {
if(effo[i] == i) printf("0 ");
else printf("%d ", maxi[i] + 1 - a[i]);
}
return 0;
}

CF581B Luxurious Houses 题解的更多相关文章

  1. cf581B Luxurious Houses

    The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...

  2. CF581B Luxurious Houses 模拟

    The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...

  3. Codeforces Round #322 (Div. 2) B. Luxurious Houses 水题

    B. Luxurious Houses Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/pr ...

  4. Luxurious Houses

    The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...

  5. 【Henu ACM Round#19 B】 Luxurious Houses

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从右往左维护最大值. 看到比最大值小(或等于)的话.就递增到比最大值大1就好. [代码] #include <bits/std ...

  6. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  7. Codeforces Round #322 (Div. 2)

    水 A - Vasya the Hipster /************************************************ * Author :Running_Time * C ...

  8. Codeforces Round #177 (Div. 1) 题解【ABCD】

    Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...

  9. cdoj 04 Complete Building the Houses 暴力

    Complete Building the Houses Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/# ...

随机推荐

  1. Python学习手册(第四版)——使用入门(自学用)

    Python的优点 -可读性 -可移植性 -不是一个独立的工具,可以调用各种库,同时也可以被调用等等 -使编程变得很有趣 -面向对象 -可混合 Python的理念 随便找的一张图 脚本语言? Pyth ...

  2. 洛谷 P7156 - [USACO20DEC] Cowmistry P(分类讨论+trie 树上 dp)

    题面传送门 题意: 给出集合 \(S=[l_1,r_1]\cup[l_2,r_2]\cup[l_3,r_3]\cup\dots\cup[l_n,r_n]\) 和整数 \(k\),求有多少个三元组 \( ...

  3. GWAS数据分析常见的202个问题?

    生信其实很简单,就是用别人的工具调参就行了.生信也很折腾,哪一步都可能遇到问题,随时让你疯掉(老辩证法了~).但是,你遇到的问题大部分人也都经历过.这时,检索技能就显得很重要了.平时Biostar和S ...

  4. 转-nRF5 SDK for Mesh(六) BLE MESH 的 基础概念

    nRF5 SDK for Mesh(六) BLE MESH 的 基础概念 Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile s ...

  5. [转载]ORA-02287: 此处不允许序号

    原文地址:ORA-02287: 此处不允许序号作者:nowhill 转载自 http://blog.sina.com.cn/s/blog_6d496bad01011dyv.html 开发人员反映序列不 ...

  6. C++面试基础篇(一)

    1. static关键字的作用 (1)全局静态变量 在全局变量前面加上关键字static, 全局变量就定义为一个全局静态变量 在静态存储区,在整个程序运行期间一致存在. 初始化:未初始化的全局静态变量 ...

  7. Jumpserver堡垒机容器化部署

    JumpServer 是符合 4A 的专业运维安全审计系统. 前提条件 已部署docker Jumpserver 对外需要开放 80 443 和 2222 端口 服务器.数据库.redis 等依赖组件 ...

  8. 判断是否有重复,判断字符串是否有重复汉字【c#】

    string corn = "公司"; int n = 0; if (tbCorporateName.Text.IndexOf(corn) > -1) { string co ...

  9. 学习java的第二十八天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  10. 数仓:解读 NameNode 的 edits 和 fsimage 文件内容

    一.edits 文件 一)文件组成 一个edits文件记录了一次写文件的过程,该过程被分解成多个部分进行记录:(每条记录在hdfs中有一个编号) 每一个部分为: '<RECORD>...& ...