Rails,uva 514

Source: Internet
Author: User

Title: Rails
Topic Link: UVa514 link
Title Description:
There is a railway station in a city, and N carriages enter the station from a direction, numbered 1-n in the order of pit stops. Your task is to determine whether they can enter the B-direction rails in a particular order and drive into the station. For example, the stacking order (5 4 1 2 3) is not possible, but (5 4 3 2 1) is possible.
Topic Analysis:
In order to reorganize the car, with the transfer station, for each compartment, once from a to C can not go back to a, once moved from C to B, you can not go back to C, meaning is a->c and c->b. And in transit C, carriages conform to the principle of LIFO. So this can be seen as a stack.

Topic Analysis: It is still very important to feel the thought of this thing, a lot of things unexpectedly

The core of the code given in the book is the use of a variable to record the largest data that has been entered into the stack. If the data to be out of the stack is larger than this data, then the a++ will continue to loop. If it is smaller than this data, but it is not equal to the top of the stack, you can prove that this order is not possible.

#include <iostream>#include<stack>Const intmaxn= ++Ten;intTARGET[MAXN];using namespacestd;intMain () {stack<int>s; intn,t; T=1; target[1]=1;  while(cin>>N) {       while(cin>>target[1]&&target[1]){       for(intI=2; i<=n;i++) {cin>>Target[i]; }      intA=0, b=1; intok=1; Stack<int>s;  while(b<=N) {        if(Target[b] = = a) {a++;b++;} Else if(!s.empty () &&s.top () ==target[b]) {S.pop (); b++;} Else if(a<=n) {S.push (a++);} Else{ok=0; Break; } }    if(ok==1) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; } cout<<Endl; }    return 0;}

Do not know why still cannot ac, say is the expression error ... The heart is tired, asks the big God to instruct ...

Rails,uva 514

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.