↓ 跳过正文
  1. Posts/

codeforces #322 div 2 B. Luxurious Houses (思路)

·578 字·2 分钟

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 h__i (1 ≤ h__i ≤ 109), where h__i equals the number of floors in the i-th house.

Output

Print n integers _a_1, _a_2, …, a__n, where number a__i 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 a__i 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

从后往前扫.. 更新最大值.

代码实现
 1/*************************************************************************
 2	> File Name: code/cf/#322/B.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年09月29日 星期二 00时27分25秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#include<cctype>
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define ms(a,x) memset(a,x,sizeof(a))
25#define lr dying111qqz
26using namespace std;
27#define For(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31const int N=1E5+7;
32int a[N];
33int ans[N];
34int n;
35int main()
36{
37  #ifndef  ONLINE_JUDGE
38//   freopen("in.txt","r",stdin);
39  #endif
40   cin>>n;
41   for ( int i = 1 ; i <= n ; i++)
42       scanf("%d",&a[i]);
43   int mx ;
44   ms(ans,0);
45   mx = a[n];
46   for ( int i = n-1 ; i >= 1 ; i--)
47    {
48	if (a[i]>mx)
49	{
50	    mx = a[i];
51	    continue;
52	}
53	else
54	{
55	    ans[i] = mx-a[i]+1;
56	}
57    }
58   for ( int i = 1 ; i < n ; i++)
59       printf("%d ",ans[i]);
60   printf("%d",0);
61
62 #ifndef ONLINE_JUDGE
63  fclose(stdin);
64  #endif
65	return 0;
66}

相关文章

uva 1587 Box(思路)

·527 字·2 分钟
给6个矩形的长和宽(或者宽和长),问这六个矩形能否组成一个长方体. 思路比较简单,不过需要注意的地方有点多. 首先由于长和宽的顺序为止,所以要处理一下(一开始只处理了后来读入的五组,没有处理单独读入的第一组,差评)

codeforces #322 div 2 A. Vasya the Hipster(纱布题)

·471 字·1 分钟
A. Vasya the Hipster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks. According to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot. Every day Vasya puts on new socks in the morning and throws them away before going to bed as he doesn’t want to wash them.

[转自codeforces] How to come up with the solutions: techniques

·901 字·2 分钟
As I work with students I often face the situation when if a problem doesn’t seem clear to a student at the first sight, it makes them unable to solve it. Indeed, you always hear about specific methods and techniques. But you don’t hear about how to think in order to apply them. In this note I’ll try to sum up my experience of solving programming contest problems. However, some pieces of advice will also be applicable for olympiads in mathematics and your first steps in academic research.

uva 489

·711 字·2 分钟
In ``Hangman Judge,’’ you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: 1. The contestant tries to solve to puzzle by guessing one letter at a time. 2. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.'' For example, if your guess is ``o'' and the word is ``book'', then both ``o''s in the solution will be counted as ``solved.'' 3. Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once. ______ | | | O | /| | | | / __|_ | |______ |_________| 4. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses. 5. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game. 6. If the contestant does not guess enough letters to either win or lose, the contestant chickens out. Your task as the ``Hangman Judge’’ is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.