• 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.
    当地的传统是,从A方向到B方向的每一列火车都会以某种方式重新组织车厢。
    Assume that the train arriving from the direction A has N 1000 coaches numbered in increasing order 1, 2, . . . , N .
    假设从A方向到达的列车有N 1000节车厢,按1、2、3的顺序递增。…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 .
    列车重组负责人必须知道是否有可能安排继续沿B方向行驶的客车,使其顺序为A1.A2,…的。
    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.
    你可以假设单程车厢在进入车站之前可以与列车断开连接,并且它们可以自行移动,直到它们在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.
    但是,一旦客车进入车站,它就不能返回A方向的轨道,而且一旦它离开B方向的车站,它就不能返回车站。

    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.
    在块的第一行中有上面描述的整数n。
    In each of the next lines of the block there is a permutation of 1, 2, . . . , N .
    在块体的每一行中,都有1、2、、的排列…n。
    The last line of the block contains just ‘0’.
    块的最后一行只包含“0”。
    The last block consists of just one line containing ‘0’.
    最后一个块只包含一行“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.
    如果可以按照输入文件相应行上所需的顺序封送coach,则输出文件的行包含“yes”。
    Otherwise it contains ‘No’.
    否则,它包含“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.
    输出文件中没有对应于输入文件最后一个“空”块的行。

    Sample Input

    5
    1 2 3 4 5
    5 4 1 2 3
    0
    6
    6 5 4 3 2 1
    0
    0

    Sample Output

    Yes
    No

    Yes

    题目理解

    火车站,有n节车厢从A方向驶入车站,按进站顺序编号为1~n判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶出车站。(典型的栈)

    代码分析

    #include <cstdio>
    #include <stack>
    using namespace std;
    const int MAXN=1000+10;
    int n,target[MAXN];
    int main ()
    {
    	while(scanf("%d",&n)==1)
    	{
    		stack<int> s;
    		int A=1,B=1;
    		for(int i=1;i<=n;i++) scanf("%d",&target[i]);
    		int ok=1;
    		while(B<=n)
    		{
    			if(A==target[B]) {A++;B++;}
    			else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;}
    			else if(A<=n) {s.push(A++);}
    			else {ok=0;break;}
    		}
    		printf("%s\n",ok?"Yes":"No");
    	}
    	return 0;
    }
    

    这是汝佳老师给出的答案,但是WA,刚看见的时候简直惊为天人,汝佳老师竟然给出了一个错误答案,我还对照了好几遍,生怕是我哪里写错了。

    看了好几遍边,才发现汝佳老师可能是忘了写最后一个块为0的情况,然后我就改了一下汝佳老师的代码:

    #include <iostream>
    #include <stack>
    using namespace std;
    const int maxn=1111;
    int target[maxn];
    int main()
    {
    	int n;
    	while(cin>>n&&n)
    	{
    		while(1)
    		{
    			cin>>target[1];
    			if(target[1]==0) break;
    			stack<int>s;
    			for(int i=2;i<=n;i++)
    			{
    				cin>>target[i];
    			}
    			bool ok=true;
    			int A=1,B=1;
    			while(B<=n)
    			{
    		 	    if(A==target[B]) {A++;B++;}
    				else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;}
    				else if(A<=n) {s.push(A++);}
    				else {ok=false;break;}
    			}
    			cout<<(ok?"Yes":"No")<<endl;
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    common.js
    数据库_day05_查询语句及JDBC
    数据库_day04_基本的增删改查操作
    Java 抽象类、普通类、接口的区别
    java中ArrayList和LinkedList的区别
    数据库_day03_对数据库的基本操作
    java_day18_集合框架map和list
    java_day17_socket,tcp协议传输
    java_day16_读写锁,fork-join框架
    java_day15_线程,匿名内部类,线程安全
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12339461.html
Copyright © 2020-2023  润新知