设为首页 加入收藏

TOP

poj1363Rails(栈模拟)
2015-07-20 17:55:20 来源: 作者: 【 】 浏览:3
Tags:poj1363Rails 模拟

题目链接:

啊哈哈,点我点我

思路:

这道题就是一道简单的栈模拟。。。。我最开始觉得难处理是当出栈后top指针变化了。。当不满足条件时入栈的当前位置怎么办,这时候想到用一个Copy数组保持入栈记录即可。。当满足所有的火车都出栈时或者已经没有火车可以进栈了,那么久跳出。。最后判断
是否出栈的火车是否达到n。。。

题目:

Rails
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24940 Accepted: 9771

Description

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 fZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciB0cmFpbiByZW9yZ2FuaXphdGlvbnMgbXVzdCBrbm93IHdoZXRoZXIgaXQgaXMgcG9zc2libGUgdG8gbWFyc2hhbCBjb2FjaGVzIGNvbnRpbnVpbmcgaW4gdGhlIGRpcmVjdGlvbiBCIHNvIHRoYXQgdGhlaXIgb3JkZXIgd2lsbCBiZSBhMSwgYTIsIC4uLiwgYU4uIEhlbHAgaGltIGFuZCB3cml0ZSBhIHByb2dyYW0gdGhhdCBkZWNpZGVzIHdoZXRoZXIgaXQgaXMgcG9zc2libGUgdG8gZ2V0IHRoZSByZXF1aXJlZCBvcmRlciBvZiBjb2FjaGVzLgogWW91IGNhbiBhc3N1bWUgdGhhdCBzaW5nbGUgY29hY2hlcyBjYW4gYmUgZGlzY29ubmVjdGVkIGZyb20gdGhlIHRyYWluIGJlZm9yZSB0aGV5IGVudGVyIHRoZSBzdGF0aW9uIGFuZCB0aGF0IHRoZXkgY2FuIG1vdmUgdGhlbXNlbHZlcyB1bnRpbCB0aGV5IGFyZSBvbiB0aGUgdHJhY2sgaW4gdGhlIGRpcmVjdGlvbiBCLiBZb3UgY2FuIGFsc28gc3VwcG9zZSB0aGF0IGF0IGFueSB0aW1lIHRoZXJlIGNhbiBiZSBsb2NhdGVkIGFzIG1hbnkgY29hY2hlcwogYXMgbmVjZXNzYXJ5IGluIHRoZSBzdGF0aW9uLiBCdXQgb25jZSBhIGNvYWNoIGhhcyBlbnRlcmVkIHRoZSBzdGF0aW9uIGl0IGNhbm5vdCByZXR1cm4gdG8gdGhlIHRyYWNrIGluIHRoZSBkaXJlY3Rpb24gQSBhbmQgYWxzbyBvbmNlIGl0IGhhcyBsZWZ0IHRoZSBzdGF0aW9uIGluIHRoZSBkaXJlY3Rpb24gQiBpdCBjYW5ub3QgcmV0dXJuIGJhY2sgdG8gdGhlIHN0YXRpb24uIDxicj4KPGJyPgoKPHAgY2xhc3M9"pst">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

Source

Central Europe 1997

代码为:

#include
   
    
#include
    
      #include
     
       using namespace std; const int maxn=1000+10; int in[maxn],out[maxn],temp[maxn]; int main() { int n,tail,top,cal,pd,u,flag; ind:while(~scanf("%d",&n)) { flag=1; if(n==0) return 0; while(1) { cal=0; pd=top=tail=1; for(int i=1;i<=n;i++) { in[i]=i; scanf("%d",&out[i]); if(out[1]==0) { flag=0; break; } } if(flag==0) break; temp[0]=0; temp[pd]=in[pd]; while(cal<=n&&pd<=n) { if(temp[top]==out[tail]) { top--; tail++; cal++; } else temp[++top]=in[++pd]; if(cal>=n||pd>n) break; } if(cal>=n) cout<<"Yes"<
      
       

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 12130 - Summits(BFS+贪心) 下一篇HDU-2258-Continuous Same Game (..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: