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. 【TcaplusDB知识库】如何部署TcaplusDB Local 版

    [TcaplusDB知识库]部署TcaplusDB Local 版的准备操作 1. 版本介绍 TcaplusDB Local版,是为用户提供的一个满足本地开发调试的版本(基于Docker部署的可下载版 ...

  2. KNN算法实现对iris数据集的预测

    KNN算法的实现 import pandas as pd from math import dist k = int(input("请输入k值:")) dataTest = pd. ...

  3. 解决fatal: unable to access '': Failed to connect to 127.0.0.1 port 1181: Connection refused的问题

    今天把项目提交的git远程的时候遇到一个问题 fatal: unable to access '': Failed to connect to 127.0.0.1 port 1181: Connect ...

  4. Spring Cloud Gateway过滤器精确控制异常返回(实战,完全定制返回body)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 Spring Cloud Gateway应用 ...

  5. 洛谷 P7450 - [THUSCH2017] 巧克力(斯坦纳树+随机化)

    洛谷题面传送门 9.13 补之前 8.23 做的题,不愧是鸽子 tzc( 首先我们先来探讨一下如果 \(c_{i,j}\le k\) 怎么做,先考虑第一问.显然一个连通块符合条件当且仅当它能够包含所有 ...

  6. Codeforces Round #717 (Div.2) 题解

    我 AK 的第二场(?)的 Div.2,还捡了个 rk4(虽然我 div2 only 的最高记录是 rk2)祭之( A 这题我竟然 WA 了两发,丢人( 直接贪心,对于 \(i=1,2,\cdots, ...

  7. doxygen相关命令

    主要配置修改 整个程序配置分几个部分 Project related configuration options 项目相关,包括: 项目名 输出目录 输出语言 是否显示继承属性 是否对C.Java.F ...

  8. python 新闻管理系统——启示

    mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...

  9. rust shadow

    1 fn main() { 2 let mut demo = 12; 3 println!("{}",demo); 4 demo = 21; // 值可变,数据类型不可变 5 pr ...

  10. C语言 字符串指针和字符串数组使用区别

    字符串指针和字符串数组使用区别 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 ...