国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

UVa 514 Rails(經(jīng)典棧)

2019-11-08 03:27:29
字體:
供稿:網(wǎng)友

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.

/begin{picture}(6774,3429)(0,-10)/put(1789.500,1357.500){/arc{3645.278}{4.7247}......tFigFont{14}{16.8}{/rmdefault}{/mddefault}{/updefault}Station}}}}}/end{picture}

 

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 /le? 1000$  coaches numbered in increasing order  $1, 2, /dots, 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 $a_1. a_2, /dots, a_N$. 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 $1, 2, /dots, N$

 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.

 

 

YesNoYes

火車進出站只有兩種情況

1.一進站就立刻出戰(zhàn)

2.進站后不出站,由C的棧頂車廂出戰(zhàn)

使用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中轉(zhuǎn)站				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();		}	}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀化市| 桑日县| 乌拉特后旗| 金溪县| 沂水县| 奇台县| 平定县| 璧山县| 育儿| 南川市| 金溪县| 阿合奇县| 澄城县| 贺州市| 饶阳县| 德令哈市| 乌拉特中旗| 昭苏县| 兴文县| 山东省| 永顺县| 云林县| 吉水县| 余干县| 山阴县| 普定县| 塔城市| 利津县| 若羌县| 乌海市| 崇义县| 左权县| 故城县| 斗六市| 广水市| 上林县| 白玉县| 永顺县| 瓦房店市| 吕梁市| 财经|