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/# ...
随机推荐
- git不显示提交代码
今天提交代码找不到提交的具体代码,找了好半天,最后发现是右边能下拉能拉下去,尴尬.
- YAPI接口自动鉴权功能部署详解
安装准备 以下操作,默认要求自己部署过yapi,最好是部署过yapi二次开发环境. 无论是选择在线安装或者是本地安装,都需要安装client工具. 1.yapi-cli:npm install yap ...
- c语言printf输出最前端字符不显示
原因:语法错误,和其它语言语法混用. printf("链表长度 : %d \n",length); printf("length is : %d \n",len ...
- 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)
vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...
- 一次forEach 中 await 的使用
forEach 和 await/async 的问题 最近在刷面试提的时候看见这样一道题 const list = [1, 2, 3] const square = num => { return ...
- VMware和Centos的安装及配置
目录 1. 安装VMware 2. 安装CentOS6及配置 2.1 Centos安装 2.1.1 配置网络连接的三种形式 2.1.1.1 桥连接 2.1.1.2 NAT模式 2.1.1.3 主机模式 ...
- Oracle-left join两表关联只取B表匹配到的第一条记录【over partition by(分组后对组内数据排序)】
背景: A表.B表两表关联,关联出来的结果里B表有不止一条,需求是只要B表结果中的某一条(按某字段排序) 经过百度,发现 row_number() over(partition by a order ...
- 08 eclipse配置JDK
eclipse配置JDK1.8 一.打开eclipse:Window>>Preferences: 二.搜索:"jdk",并点击右侧的"Add": 三 ...
- Scala(一)【安装和IDEA中开发】
目录 一.下载 二.windows安装 三.linux环境安装 四.Ida开发Scala 1.在线下载Scala插件 2.离线下载Scala插件 3.验证 五.HelloWorld入门程序 1.新建M ...
- Oracle—全局变量
Oracle全局变量 一.数据库程序包全局变量 在程序实现过程中,经常用遇到一些全局变量或常数.在程序开发过程中,往往会将该变量或常数存储于临时表或前台程序的全局变量中,由此带来运行效率降 ...