codeforces #322 div 2 B. Luxurious Houses (思路)
2015-09-29 01:07
253 查看
B. Luxurious Houses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 i from 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.
Sample test(s)
input
output
input
output
View Code
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 i from 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.
Sample test(s)
input
5 1 2 3 1 2
output
3 2 0 2 0
input
4 3 2 1 4
output
2 3 4 0 从后往前扫.. 更新最大值.
/************************************************************************* > File Name: code/cf/#322/B.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年09月29日 星期二 00时27分25秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) #define lr dying111qqz using namespace std; #define For(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=1E5+7; int a ; int ans ; int n; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); #endif cin>>n; for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]); int mx ; ms(ans,0); mx = a ; for ( int i = n-1 ; i >= 1 ; i--) { if (a[i]>mx) { mx = a[i]; continue; } else { ans[i] = mx-a[i]+1; } } for ( int i = 1 ; i < n ; i++) printf("%d ",ans[i]); printf("%d",0); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- codeforces #322 div 2 A. Vasya the Hipster(随便搞)
- CentOS 安装Telnet
- 20135219洪韶武——信息安全系统设计基础第二周学习总结
- [转]GC简介
- 读jQuery官方文档:jQuery对象
- 数据库字段类型
- virtual void addChild(Node * child, int localZOrder)中ZOreder参数设置
- 教你玩BeEF
- Controller向View传输数据
- Developing Skills(优先队列)
- Unity3D之LOD技术详解
- RobotFrameWorK控制流之if语句——Run Keyword If
- byte,short,int,long数据之间的倍数关系
- Core Animation
- C语言中不允许创建临时变量,交换两个数的内容
- Luxurious Houses(模拟)
- LAMP编译安装只PHP以模块化的方式工作
- POJ - 2976 Dropping tests(二分查找,最大化平均值)
- block小解
- 【iOS设计】一款APP从设计稿到切图过程概述