UVa514 Rails(栈+思维)

题目链接

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has
N ≤ 1000 coaches numbered in increasing order 1, 2, . . . , N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1.a2, . . . , aN . Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once
it has left the station in the direction B it cannot return back to the station.

Input
The input file consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, . . . , N. The last line of the block contains just ‘0’. The last block consists of just one line containing ‘0’.

Output
The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last “null” block of the input file.

刚开始看题知道了要用栈,就直接这样写,用三个栈分别表示A,B,C三个站点,然后样例没过,原来是自己只看了样例,没能想一个复杂点的例子验证猜想是否正确,大意了。读了LRJ的思路发现特别简单新奇,说实话要不是例题,自己做真的还会费劲

思路:

1.首先用两个变量l,r分别从1开始,l是因为A站的车序号都是从1到n的,r是存要比较的数组的下标,本以为栈是用来存B车站的,可是实际上栈只是C站的一个工具,暂时保存其中需要进C站的车,若符合条件,C站的车还会一一开出

2.while循环的判断条件是r<=n,除了不符合的break外,符合的l和r都会遍历到n,这是毋庸置疑的。第一个条件肯定是l和a[r]是否相等啊,相等了直接都++,这辆车算是开走了不用管了。然后第二三四个判断条件一定不要弄反,第二个是再看C站也就是栈是否存着你当前l序号的车,存了就开走哇(栈pop),第三判断也就是前两个都不对,那就先存在C站,栈push,再判断下一个序号啊。当54321的情况实际是比较特殊的,读者可以自己模拟一遍,就懂其中的奥妙了。最后一个当然是break了

3.这里输入和输出都比较易错,代码中标记出了

代码:

#include <iostream>
#include <stack>
using namespace std;
int a[1005];
int main()
{
    int n;
    while(~scanf("%d",&n)!=EOF&&n){ ///输入易错1
        while(scanf("%d",&a[1])!=EOF&&a[1]){ ///输入易错2
            for(int i=2;i<=n;i++){
                scanf("%d",&a[i]);
            }
            int l=1,r=1,flag=1;
            stack<int> s;
            while(r<=n){
                if(l==a[r]){
                    l++;r++;
                }else if(!s.empty()&&s.top()==a[r]){
                    s.pop();
                    r++;
                }else if(l<=n) s.push(l++);
                else {
                    flag=0;break;
                }
            }
            printf("%s\n",flag?"Yes":"No");
        }
        printf("\n"); ///输出易错
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值