@xunuo
2017-02-18T21:10:17.000000Z
字数 2315
阅读 1158
time limit per test 2 seconds memory limit per test 256 megabytes
暴力
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.
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.
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.
input
3
3 1 2
output
3
2 1
input
5
4 5 1 2 3
output
|
|5 4///这行前面还有一行
|
|
|3 2 1
In 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 size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
题意
其实这道题主要就是题意问题:有n天,每天都会掉一个零食下来,从小到大开始堆;
如第二组样例:n=5;说明接下来有1-5这5个数,输入的次序是4 5 1 2 3;
第一天,输入的是4,但是最大的是5,所以第一天不放,第一行为空--"\n";
第二天,输入的是5,是最大的数,所以第二天放5 4;
第三天,输入的是1,但是最大的数应该是3,所以第三行为空行;
第四天,输入的是2,但是最大的数应该是3,所以第四行为空行;
第五天,输入的是3,则应该在第五行输出3 2 1;
完整代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[100010];
int b[100010];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int k=n;
memset(b,-1,sizeof(b));
for(int i=0;i<n;i++)
{
if(a[i]!=k)
{
b[a[i]]=a[i];
printf("\n");
}
else
{
k--;
printf("%d",a[i]);
if(i!=0)
{
for(int j=k;j>=n-i;j--)
{
if(b[j]==-1)
break;
else
{
k--;
printf(" %d",b[j]);
b[j]=-1;
}
}
}
printf("\n");
}
}
}
return 0;
}