CF581B Luxurious Houses 题解
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 题解的更多相关文章
- cf581B Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- CF581B Luxurious Houses 模拟
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- 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 ...
- Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- 【Henu ACM Round#19 B】 Luxurious Houses
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从右往左维护最大值. 看到比最大值小(或等于)的话.就递增到比最大值大1就好. [代码] #include <bits/std ...
- LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...
- Codeforces Round #322 (Div. 2)
水 A - Vasya the Hipster /************************************************ * Author :Running_Time * C ...
- Codeforces Round #177 (Div. 1) 题解【ABCD】
Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...
- cdoj 04 Complete Building the Houses 暴力
Complete Building the Houses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/# ...
随机推荐
- vue 事件监听和es6模板语法
es6模板语法的反引号是通过左上角的飘字符弄出来了,学废了吗?
- Flink 是如何统一批流引擎的
关注公众号:大数据技术派,回复"资料",领取1000G资料. 本文首发于我的个人博客:Flink 是如何统一批流引擎的 2015 年,Flink 的作者就写了 Apache Fli ...
- jQuery源码中的赌博网站
前言 jQuery源码中有赌博网站? 起因是公司发的一份自查文件,某银行在日常安全运营过程中发现在部分jQuery源码中存在赌博和黄色网站链接. 链接分为好几个: www.cactussoft.cn ...
- Mac Maven 安装及配置
一.下载 打开 Maven 官方下载页面:https://maven.apache.org/download.cgi#,点击下载链接即可开始下载: 以 Maven 3.8.4 为例,解压后可以 ...
- [Cnoi2020]线性生物
期望入门题.但是我不会做. 考虑设\(E_{x\to{x+1}}\)为\(x\)到\(x+1\)点的期望步数. 则\(ans = \sum_{i = 0}^{n} E_{x\to{x+1}}\) 知\ ...
- Codeforces 464E The Classic Problem(主席树+最短路+哈希,神仙题)
题目链接 题意:给出一张 \(n\) 个点 \(m\) 条边的无向图,第 \(i\) 条边连接 \(u_i,v_i\),边权为 \(2^{w_i}\),求 \(s\) 到 \(t\) 的最短路. \( ...
- 洛谷 P3704 [SDOI2017]数字表格(莫比乌斯函数)
题面传送门 题意: 求 \[\prod\limits_{i=1}^n\prod\limits_{j=1}^mfib_{\gcd(i,j)} \] \(T\) 组测试数据,\(1 \leq T \leq ...
- R绘图布局包 customLayout
今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...
- Linux基础命令---mysqladmin数据库管理工具
mysqladmin mysqladmin是mysql数据库的管理工具,可以控制.查看.修改数据库服务器的配置和状态. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedor ...
- 用oracle中的Row_Number实现分页
Row_Number实现分页 1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号 ...