F - 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 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 contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.
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
由于station符合后进先出规则,所以可以用一个stack<int>来表示station。
 3 然后采用模拟的规则来写,因为进入station是按照编号递增进入,所以可以用aId变量表示。
 4 接下来就是模拟时候应该注意的条件,我们知道有以下两种情况,一种是一进来station,就出station
 5 也就是 aId == coaches[lenB]; 一种是进来时还不出站,那么这时候就要s.push(aId),但是这一种的条件呢?
 6 我们如果可以排除掉第一种,那么无非就是第二种了。但是我们要知道,在进站之前,如果站台里有车它是可以先出站的
 7 ,这种状态就条件就是 !s.empty() && s.top() == coaches[lenB].所以只要按照顺序判断这几个条件
 8 就可以写出这个模拟程序了。
 9 
10 
11 总结:
12 解决问题的关键点,在于构建问题的模型、大部分都是可以用现有的基础数据结构。构造完对应的数据结构,特别是对于模拟题
13 无非就是状态见的转移处理,这是就要在基于模型的基础上、先写出映射到个个状态的唯一条件,然后按照问题逻辑一一先后判断
14 即可。
15 */
16 #include <iostream>
17 #include <stack>
18 
19 using namespace std;
20 const int len = 1024;
21 int coaches[len];
22 
23 int main() {
24 
25     int n;
26     while (cin >> n, n) { 
27         stack<int> s;
28         // read the required permutaion
29 
30         while (    cin >> coaches[0], coaches[0]) {
31 
32             for (int i = 1; i < n; i++) {
33                 cin >> coaches[i];
34             }
35             int lenB = 0, aId = 1;
36             bool ok = true;
37             while (lenB < n) {
38 
39                 if (aId == coaches[lenB]) {  aId++; lenB++; }
40                 else if(!s.empty() && s.top() == coaches[lenB]) { s.pop(); lenB++; }
41                 else if(aId <= n) s.push(aId++);
42                 else { ok = false; break; }
43 
44             }
45 
46             cout << (ok ? "Yes" : "No") << endl;
47         }
48         cout << endl;
49     }
50     
51     return 0;
52 }
2015/3/30下午3:03:52


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值