CodeForces - 581B-Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all ifrom 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0
思路:从后边扫,去看是否大于当前的最大值,然后就更新
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100005;
int a[maxn],b[maxn];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int Max=0;
for(int i=n-1;i>=0;i--)
{
if(a[i]>Max) b[i]=a[i];
else if(a[i]==Max) b[i]=0;
else
b[i]=Max;
Max=max(Max,a[i]);
}
for(int i=0;i<n-1;i++)
{
if(b[i]==a[i]) printf("0 ");
else if(b[i]==0) printf("1 ");
else
printf("%d ",b[i]-a[i]+1);
}
printf("0\n");
return 0;
}
CodeForces - 581B-Luxurious Houses的更多相关文章
- 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 ...
- cf581B Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- 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 ...
- 【Henu ACM Round#19 B】 Luxurious Houses
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从右往左维护最大值. 看到比最大值小(或等于)的话.就递增到比最大值大1就好. [代码] #include <bits/std ...
- CF581B Luxurious Houses 题解
Content 一条大街上有 \(n\) 个房子,第 \(i\) 个房子的楼层数量是 \(h_i\).如果一个房子的楼层数量大于位于其右侧的所有房屋,则房屋是豪华的.对于第 \(i\) 个房子,请求出 ...
- Codeforces Round #322 (Div. 2)
水 A - Vasya the Hipster /************************************************ * Author :Running_Time * C ...
- Codeforces Round #501 (Div. 3) 1015D Walking Between Houses
D. Walking Between Houses time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #501 (Div. 3) D. Walking Between Houses
题目链接 题意:给你三个数n,k,sn,k,sn,k,s,让你构造一个长度为k的数列,使得相邻两项差值的绝对值之和为sss, ∑i=1n∣a[i]−a[i−1]∣,a[0]=1\sum_{i=1}^n ...
随机推荐
- php设置文件编码
<?php @header('Content-type: text/html;charset=UTF-8'); ?>
- Thread,Service和AsyncTask
Thread,Service和AsyncTask这三种东西,似乎都是用来执行后台耗时操作的: 印象里Service是「超过5s的耗时操作就应该放进去」,但是Service实际上仍然是主线程,所以,在S ...
- laravel登录后台500错误!
登录页面正常显示,填写完用户名密码 点登录后 页面一片空白,没有任何输出.debug可以看到一个500错误,preview和response都是空的.追踪了一下 发现在public/index.php ...
- Sed在匹配行前后加入一行
a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)i 插入内容 sed ‘/匹配词/i\要加入的内容’ example.file 将 ...
- ACM学习历程—FZU 2140 Forever 0.5(计算几何 && 构造)
Description Given an integer N, your task is to judge whether there exist N points in the plane su ...
- AtCoder Grand Contest 013 C:Ants on a Circle
题目传送门:https://agc013.contest.atcoder.jp/tasks/agc013_c 题目翻译 给你一个周长为\(L\)的圆,有\(N\)只蚂蚁在圆上爬,速度为一单位距离每秒. ...
- 南阳oj水题集合,语言的灵活运用
a+b 输入 输入两个数,a,b 输出 输出a+b的值 样例输入 2 3 样例输出 5 c/c++ #include<iostream> using namespace std; int ...
- RHEL6安装JDK7
一.安装准备 1.操作系统:redhat-server-6.1-x86_64 下载地址: http://www.verycd.com/files/d39b97540497d24175340915244 ...
- 4. docker镜像的概念、管理(查看、下载、删除)
镜像的概念 镜像是一个包含程序运行必要依赖环境和代码的只读文件,它采用分层的文件系统,将每一次改变以读写层的形式增加到原来的只读文件上.镜像是容器运行的基石. 下图展示的是Docker镜像的系统结构. ...
- HDU - 6400 多校8 Parentheses Matrix(构造)
Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...