CodeForces-767A Snacktower

time limit: per test2 seconds
memory limit: per test256 megabytes
input: standard input
output: standard output

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 time n 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.

Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.

The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.

Output
Print 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 the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.

Examples

input
3
3 1 2
output
3

2 1
input
5
4 5 1 2 3
output

5 4

3 2 1

題意是給1-n 這n個數字並將他們輸出,但是必須從最大數n開始一直連續輸出直到到1。在沒有給出n之前不能輸出數字,只能換行,並且輸出只能一個接一個連續輸出已給出的數字。
其實一看就是個很水的題,但是我老是想錯,或者有些地方給漏掉……
第一遍寫了個函數,在註釋掉的部分,總想着要記錄天數,記錄哪幾個輸出過了,記錄當前是第幾天……
其實想多了……
只需從頭到尾遍歷,記錄一下數字是否已給出就行。
也是從n開始輸出,如果這個數有了就直接輸出,然後接着判斷相鄰的數字有沒有給出。如果沒有就輸出換行。
主要是有些換行的地方要注意一下。能連續輸出的時候數字後面是空格,否則是要直接換行的,然後這個就要考慮到下一個是否能直接輸出。

#include<iostream>
using namespace std;

int i, j, k, t;
int a[100005], b[100005], n;
bool flag;

int main()
{
    while (cin >> n)
    {
        for (i = 1; i <= n; i++)
            b[i] = 0;
        for (i = 1, flag = false, j = n; j >= 1;)
        {
            if (i <= n)
            {
                cin >> a[i];
                b[a[i]] = 1;
                i++;
            }
            if (b[j])
            {
                for (; b[j] == 1;)
                {
                    /*if (j > 1)*/
                    {
                        cout << j << " ";
                        flag = true;
                    }
                /*  else
                        cout << 1 << endl;*/
                    j--;
                }
                if (b[j] == 0 && flag == true)
                    cout << endl;
            }
            else
            {
            /*  if (flag == true)
                {
                    cout << endl;
                    flag = false;
                }*/
                cout << endl;
            }
        }
        //f(0, n, 0);
    }
    return 0;
}
//void f(int i, int j, int date)
//{
//  if (j > 0)
//  {
//      if (a[i] == j)
//      {
//          if (j > 1)
//          {
//              cout << a[i] << " ";
//              flag = true;
//              f(0, j - 1, date + 1);
//          }
//          else if (j == 1)
//          {
//              cout << a[i] << endl;
//          }
//      }
//      else
//      {
//          if (i < date)
//          {
//              if (i >= n - j)
//                  cout << endl;
//              f(i + 1, j, date); 
//          }
//          else if (i == date)
//          {
//              if (i >= n - j)
//                  cout << endl;
//              f(i + 1, j, date + 1);
//          }
//      }
//  }
//}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章