博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1363Rails(栈模拟)
阅读量:5080 次
发布时间:2019-06-12

本文共 3606 字,大约阅读时间需要 12 分钟。

主题链接:

思路:

这道题就是一道简单的栈模拟。

。。

。我最開始认为难处理是当出栈后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 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

51 2 3 4 55 4 1 2 3066 5 4 3 2 100

Sample Output

YesNoYes

Source

代码为:

#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"<

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/hrhguanli/p/4807845.html

你可能感兴趣的文章
测试计划
查看>>
选择器
查看>>
Mysql与Oracle 的对比
查看>>
jquery实现限制textarea输入字数
查看>>
thinkphp5 csv格式导入导出(多数据处理)
查看>>
PHP上传RAR压缩包并解压目录
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
jenkins常用插件汇总
查看>>
c# 泛型+反射
查看>>
第九章 前后查找
查看>>
Python学习资料
查看>>
jQuery 自定义函数
查看>>
jquery datagrid 后台获取datatable处理成正确的json字符串
查看>>
ActiveMQ与spring整合
查看>>
web服务器
查看>>
网卡流量检测.py
查看>>
poj1981 Circle and Points 单位圆覆盖问题
查看>>
POP的Stroke动画
查看>>
SQL语句在查询分析器中可以执行,代码中不能执行
查看>>
yii 1.x 添加 rules 验证url数组
查看>>