其他分享
首页 > 其他分享> > ZOJ 1945 Stacking Tower

ZOJ 1945 Stacking Tower

作者:互联网

One of the most common children's toys is a stacking tower, which consists of a series of rings of di erent sizes and a tapered rod which can hold the rings. The rings and rod are designed so that when the rings are placed in descending order by size, they fit exactly on the rod. Further, each ring, if placed by itself on the rod will go no lower than its position when all rings are on the rod. The diagram on the left shows an example of this toy which uses five rings, numbered 1 (smallest) to 5 (largest)

Unless endowed with super-genius mental faculties, most infants will place the rings in a random order on the rod, which results in some rings sticking over the top of the rod. The above diagram on the right shows the result of one such random placement. Your job will be to determine the number of rings which sit above the rod given a random ordering of the rings. You may assume that the rings may be stacked arbitrarily high without falling over.


Input

Input will consist of multiple problem instances. Each instance consists of a single line of the form n r1 r2 ... rn. The positive integer n specifies the number of rings (<= 100), and the remaining n integers give the order the rings are put on the rod. A value of n = 0 will terminate input.


Output

For each problem instance, you should output one line containing an integer indicating the number of rings sticking over the top of the rod.


Sample Input

5 5 4 3 2 1
5 4 5 1 3 2
8 1 2 3 4 5 6 7 8
20 11 10 19 7 12 14 5 2 3 1 8 6 13 17 18 9 4 20 15 16
0


Sample Output

0
2
7
11

题解:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,num[105],j;
    while(cin>>n)
    {
        if(n==0) break;
        
        for(int i=0;i<n;i++)
        {
           cin>>num[i];
          
        }
        
        for(int i=0;i<n;i++)
        {
           
           if(i==0) j=num[i];
           else if(num[i]>j-1) j--;
           else j=num[i];
           if(j==1) {n=n-i-1;break;} 
        }
         
        cout<<n<<endl;
        
    }
}

  

这题的关键是要找到落在顶点数1的方块,我们用变量J去指向下一个的方块的落点位置,注意题目中说明了刚好降序排列的时候是每个方块都恰好卡住的。

所以我们用第一个方块的序号一定是卡在指定的位置的,我们的j初值就设置为第一个方块的数字。

之后在判断下面一个方块的数值如果比J大,那么就会与前一个方块紧密连接。

反之则跟新j的值,会出现断层。

 

标签:Stacking,rings,ZOJ,rod,will,int,Tower,over,方块
来源: https://www.cnblogs.com/smallsamllpig/p/15893752.html