UVa 514 Rails(经典栈)
2017-02-17 09:01
357 查看
https://vjudge.net/problem/UVA-514
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
coaches
numbered in increasing order
. 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
.
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.
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
The last line of the block contains just 0.
The last block consists of just one line containing 0.
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.
火车进出站只有两种情况
1.一进站就立刻出战
2.进站后不出站,由C的栈顶车厢出战
使用A表示进站口,B表示出站口,s堆栈表示C站,然后模拟列车进出站运动
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(true){
int n = scan.nextInt();
if(n==0)
break;
while(true){
int[] target = new int[n+1];
int m = scan.nextInt();
if(m==0)
break;
target[1] = m;
for(int i=2;i<=n;i++){
target[i] = scan.nextInt();
}
int A = 1,B = 1;//A是进站口,B出站口
boolean ok = true;
Stack<Integer> s = new Stack<Integer>();//模拟C中转站
while(B<=n){
if(A==target[B]){//表示从A进站后就立刻出站
A++;
B++;
}else if(!s.isEmpty()&&s.peek()==target[B]){//表示C中第一个车厢出站
s.pop();
B++;
}else if(A<=n){//表示从A站进站
s.push(A++);
}else{
ok = false;
break;
}
}
System.out.println(ok?"Yes":"No");
}
System.out.println();
}
}
}
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
coaches
numbered in increasing order
. 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
.
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.
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
The last line of the block contains just 0.
The last block consists of just one line containing 0.
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.
Yes No Yes
火车进出站只有两种情况
1.一进站就立刻出战
2.进站后不出站,由C的栈顶车厢出战
使用A表示进站口,B表示出站口,s堆栈表示C站,然后模拟列车进出站运动
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(true){
int n = scan.nextInt();
if(n==0)
break;
while(true){
int[] target = new int[n+1];
int m = scan.nextInt();
if(m==0)
break;
target[1] = m;
for(int i=2;i<=n;i++){
target[i] = scan.nextInt();
}
int A = 1,B = 1;//A是进站口,B出站口
boolean ok = true;
Stack<Integer> s = new Stack<Integer>();//模拟C中转站
while(B<=n){
if(A==target[B]){//表示从A进站后就立刻出站
A++;
B++;
}else if(!s.isEmpty()&&s.peek()==target[B]){//表示C中第一个车厢出站
s.pop();
B++;
}else if(A<=n){//表示从A站进站
s.push(A++);
}else{
ok = false;
break;
}
}
System.out.println(ok?"Yes":"No");
}
System.out.println();
}
}
}
相关文章推荐
- UVa 514 Rails(经典栈)
- UVA - 514 Rails 经典栈使用
- UVa 514 Rails(经典栈)
- 铁轨(Rails,ACM/ICPC CERC 1997.UVa 514)
- Uva514 Rails
- Uva 514 Rails(简单栈)
- Uva514 Rails(铁轨)
- UVA514 Rails(铁轨)
- UVa 514 Rails (water ver.)
- UVA514:Rails(铁轨)
- Uva 514 Rails (STL 栈+队列 模拟)
- Rails UVA - 514
- UVA 514 Rails
- UVA 514 Rails - ACM/ICPC CERC 1997
- Uva 514 Rails(栈应用)
- UVA514 Rails (stack)
- UVa514 Rails
- UVA - 514 Rails
- UVA - 514 Rails
- UVA514(Rails)