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. myeclipse maven web打包

    1.在当前的项目pom.xml的文件上,如下图所示:鼠标右键->run As->Maven Build...

  2. javaSE基础复习

    第一天:复习java入门知识,jvm内存,java程序执行流程,数据类型,变量和自动类型转换,运算符... 学习java目的:起初是因为想找工作,拿高薪.后来逐渐在学习java技术的过程中渐渐循喜欢上 ...

  3. 洛谷 P4002 - [清华集训2017]生成树计数(多项式)

    题面传送门 神题. 考虑将所有连通块缩成一个点,那么所有连好边的生成树在缩点之后一定是一个 \(n\) 个点的生成树.我们记 \(d_i\) 为第 \(i\) 个连通块缩完点之后的度数 \(-1\), ...

  4. 回文字符串 Manacher

    1. Manacher 忘光了,忘光了. 首先将字符串所有字符之间(包括头尾)插入相同分隔符,再在最前方插入另一个分隔符防止越界. 设以 \(s_i\) 为对称中心的回文串中,最长的回文半径为 \(p ...

  5. 如何构建自己的KEGG数据库

    本文转自Y叔公众号 自己KEGG数据库好处: 可重复性好 没网也可以进行分析 步骤 1 在KEGG官网找到自己物种的3字符缩写 2 加载Y叔获取kegg.db 的R包 1 ##安装Y叔的包 2 lib ...

  6. adblock plus-看下图你就懂

  7. python第三天 列表和元组

    枚举 for in enumerate 循环输出字符串的内容并且输出它的索引信息: #判断索引为5的字符串是不是"您" is in Python提供了⼤量的内置数据结构,包含了列表 ...

  8. 详解 Rainbond Ingress 泛解析域名机制

    Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.区别于一般的 Ingress 配置中,用户需要自行定义域名的使用体验,Rainbond 的 ...

  9. 24. 解决Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

    第一种: sudo vim /etc/resolv.conf 添加nameserver 8.8.8.8 第二种: /etc/apt/sources.list 的内容换成 deb http://old- ...

  10. 18. MYSQL 字符编码配置

    MYSQL 5.7版本的my.ini 在C盘隐藏文件夹下 C:\ProgramData\MySQL\MySQL Server 5.7 [client] default-character-set=ut ...