According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some timen snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.

However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a PRogram that models the behavior of Ankh-Morpork residents.
InputThe first line contains single integer n (1?≤?n?≤?100?000) — the total number of snacks.
The second line contains n integers, thei-th of them equals the size of the snack which fell on thei-th day. Sizes are distinct integers from1 to n.
OutputPrint n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on thei-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
ExamplesInput33 1 2Output3 2 1Input54 5 1 2 3Output5 4 3 2 1NoteIn the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size1 fell, and the residents weren't able to place it because they were missing the snack of size2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size1 which had fallen before.
題目大意:
給你N個數(shù)(從1-N),每次遇到剩余數(shù)字中的最大項之后,逆序將之前出現(xiàn)過的數(shù)字按照每次-1的順序輸出(就是說必須連續(xù))。
如果沒有任何數(shù)字輸出,那么輸出一個空行。
思路:
過程維護剩余數(shù)字中的最大值,然后每一次遇到,逆序模擬輸出即可。
Ac代碼:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[100060];int vis[100060];int main(){ int n; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } int maxn=n; int minn=0x3f3f3f3f; for(int i=1;i<=n;i++) { vis[a[i]]=1; if(a[i]!=maxn) { minn=min(minn,a[i]); printf("/n"); } else { int flag=0; minn=min(minn,a[i]); for(int j=maxn;j>=minn;j--) { if(vis[j]==1) printf("%d ",j); else { flag=1; maxn=j; break; } } if(flag==0)maxn=minn-1,minn=0x3f3f3f3f; printf("/n"); } } }}
新聞熱點
疑難解答