POJ - 1363 - Rails(栈)

POJ 1363 Rails (值得一记的栈好题) Rails Time Limit: 1000MS Memory Limit: 10000K 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 possibl 阅读详情

POJ 1363.Rails

Time Limit: 1000MS      Memory Limit: 10000K

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

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


题意:让n个数进栈,看能否按给的数字序列出栈


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;

stack<int>s;

int main() {
    int n, t[1005];
    while(scanf("%d", &n)!=EOF && n){
        while(1){
            scanf("%d", &t[0]);
            if(!t[0]){
                printf("\n");
                break;
            }

            for(int i = 1; i < n; i++)
                scanf("%d", &t[i]);

            int flag = 1, num = 1;
            for(int i = 0; i < n;){
                if(num == t[i]){        //如果数字相同,不用进栈,直接判断下一个 
                    i++, num++;
                }
                else if(!s.empty() && s.top()==t[i]){   //如果栈不为空,且栈头和给定的数相同 
                    i++;
                    s.pop();
                }
                else if(num <= n){    //若上述都不符合,则让数字进栈 
                    s.push(num);
                    num++;
                }
                else{
                    flag = 0;
                    break;
                }
            }
            if(flag)    printf("Yes\n");
            else    printf("No\n");

            while(!s.empty())
                s.pop();
        }
    }
    return 0;
}
使用字典树和Hashtable两种方法解POJ 2503(JAVA) NULL 博文链接:https://128kj.iteye.com/blog/1734586 立即下载

相关推荐

【多模态大模型】GLIP:零样本目标检测新范式与视觉语言理解

本文深入解析了多模态大模型GLIP在零样本目标检测和视觉语言理解领域的创新应用。GLIP通过统一目标检测与自然语言理解任务,实现了零样本学习能力,能够精准定位复杂文本描述对应的图像区域。文章详细介绍了GLIP的核心技术、数据策略及实战表现,并对比了与其他多模态模型的差异,为AI视觉理解提供了新范式。

weixin_29186249的博客 263

Rails

FJNU.1221DescriptionThere 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

linyq@BambooFan ~ 729

CS5212芯片实战:手把手教你设计DP转VGA转换电路(附完整原理图)

本文详细介绍了CS5212芯片在DP转VGA转换电路设计中的实战应用,包括芯片架构解析、核心电路设计要点、PCB布局与电磁兼容设计、调试技巧及进阶应用开发。通过完整的原理图和实用设计指南,帮助工程师快速掌握高可靠转换器的设计方法,特别适合需要国产化替代方案的场景。

weixin_30576827的博客 393

(stack)Rails(P1363)

#include #include #include using namespace std; int go[1000000]; int main() { int i,j,k; int n; while (cin>>n,n) { while (true) { cin>>go[0]; if (go[0]==0) break; for (i=1;i<n

jiangjiashi的博客 907

YOLOv11 改进 - 下采样 | 轻量化突破:ADown 下采样让 YOLOv11 参量减、精度升

本文介绍了一种轻量级的特征下采样模块 ADown,它结合平均池化与最大池化策略,实现更有效的信息保留与压缩。在传统卷积网络中,特征下采样常造成信息损失,而 ADown 通过双通道并行结构优化了下采样效果,提升了模型的表达能力。在 YOLOv11 中引入 ADown 替换原有的下采样模块后,网络在保持高效性的同时,显著提升了目标检测的精度与稳定性。文章详细介绍了 ADown 的核心代码实现、模块注册流程以及 YOLOv11-ADown 模型的配置和训练方法,为轻量化目标检测模型的优化提供了实用参考。

魔改工程师的博客 1480

提交 ieee 手稿出现:Do not include the Reviewer PDF with my submission 怎么办

IEEE投稿Reviewer PDF选项说明 投稿时出现的"Do not include the Reviewer PDF with my submission"选项控制是否附带评审专用PDF文件。Reviewer PDF是系统自动生成包含所有投稿材料的完整PDF,用于同行评审。建议保持默认不勾选状态,除非确认编辑部不需要该文件,否则可能影响审稿流程。勾选表示不附带此PDF,不勾选则会正常生成并提交该评审文件。

ZhangJiqun&191cm&85kg 2209

POJ1363

Rails\POJ1363](http://poj.org/problem?id=1363)DescriptionThere is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, fund

ACM2017 975

红米K70至尊版修复“nv损坏”主板电阻图示 mtk芯片工程固件刷写与步骤说明

💝💝💝红米K70至尊版 机型代码:rothko,搭载天玑9300+旗舰芯片.后置5000万像素索尼IMX906高动态OIS主摄,800万像素超广角镜头,200万像素微距镜头,前置2000万像素摄像头,也适用于以下型号的小米机型:2407FPN8EG,2407FPN8ER,XIG06,A402XM,2407FRK8EC。 💝💝💝通过博文了解 1💝💝💝-----此机型工程固件的资源刷写注意事项 2💝💝💝-----此机型刷写工程固件预览界面 3💝💝💝------红米K

小马哥的专栏 1461

poj1363

堆栈的模拟,给定序列,1,2,3,4,...判断堆栈出栈顺序是否合法 5 //5个数入栈1 2 3 4 5 //出栈顺序5 4 1 2 3 //出栈顺序0 //5个数的结束6 //6个数的入栈6 5 4 3 2 10 //6个数的结束0 //输入结束 数组作栈 a栈放目标出栈顺序,让1,2,3,4依次入stack栈,如果a[pos]和stack[top]比较,相同,pos++,top--即表...

weixin_30396699的博客 151

Redhawk:如何gds2def?

使用redhawk gds2def功能 这其中gds_layer.map的书写方式如下: m代表metal,v代表via。 ICC/ICC2用户可以从tech file中获取layer number。 innovus用户可以从stream out layer map中获取layer number。 最后运行gds2def -m gds2def.config即可。 ...

m0_61544122的博客 5346

把 MCP Server 直接装进 SAP ABAP:用 Unlimited Display Tools 打通 Agentic SDK 与 SAP ECC / S/4HANA 的最后一公里

摘要:AI与SAP集成的MCP解决方案 企业在AI集成中面临工具链复杂、数据对接繁琐的挑战。Model Context Protocol (MCP)通过标准化工具与数据暴露方式,将集成复杂度从N×M降至N+M。在SAP生态中,MCP Server for SAP直接部署于ABAP层,利用DDIC/CDS视图对接各类Agent SDK,避免中间件风险。 MCP核心包含三类对象: Tools:带元数据的可执行动作 Resources:结构化上下文数据 Prompts:可参数化提示模板 ABAP层实现MCP Se

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 685

POJ 1363 Rails 模拟栈

来源:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1到n,只能由一段进站,另一端出站。现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否可能。 思路:其实就是一个模拟栈的过程,先进后出。但是由于会出现这样的情况,比如说:1 2 3进栈,然后3出栈,然后4 5进栈,所以该出栈的顺序是3 5 4 2 1,所以在模拟的过程中需要注意一下。 还有就是

2009

Altium Designer(AD24/25)如何在PCB设计阶段做阻抗匹配

目前阻抗匹配多直接有制版厂进行,PCB设计师在设计阶段可不关注阻抗设计,在加工阶段只需标注需要控制的阻抗线,制板厂即可按照要求对阻抗线进行调整和控制。但是对于设计师来讲掌握阻抗线的设计方法也是非常必要的。本文介绍Altium Designer(AD24/25)如何在PCB设计阶段做阻抗匹配。

fydar的博客 894

POJ_1363_Rails(栈模拟)

题型:模拟 题意:火车车厢以1~n的顺序开进车站,有一个铁轨用于暂时停放车厢,问是否可以按输入的火车顺序开出。 分析:用栈模拟即可。 1、   先用循环将当前车厢编号放入栈中,若遇到符合驶出序号的则弹出top。 2、   然后再将stack再查一遍,有符合的就pop。 3、   最后剩在stack里的元素只能按部就班的乖乖进行比较了,有就pop,木有就break; 注意点: 1、

Light_starlight 836

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

poj1363 Rails(栈的应用)

题意: 给出火车出站序列,问顺序是否合理,就是判断出栈合理不 算法:模拟栈       1.开辟一个数组存储出栈序列a[..];       2.开辟一个栈 stacks,按顺序存储当前比a[i]小的车厢       3.如果a[i=0 -> n] == j = 0 ->..说明车厢直接出栈       4.如果3不成立,说明下一车厢有可能大于,有可能小于当前车厢编号

水神 1374

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

POJ - 1363 Rails(stack,栈)

POJ - 1363 E - Rails #include<iostream> #include<cstdio> #include<stack> using namespace std; int n,x,a[1000010]; int main() { while(scanf("%d",&n)&&n) { while(scanf("%d",&a[1])&&a[1]) { stack<int>s

00后的博客 233

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

poj 1363 Rails 【栈】

Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31405   Accepted: 12204 Description There is a famous railway station in PopPush City. Country there is

R 298

POJ 1363-Rails 简单栈

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 wa

岁月流风、、 1204

POJ1363 Rails(栈)

题目链接:点击打开链接 给你一个n,接下来一行是n个数字,代表出火车站的顺序,进火车站的顺序是1-n,问你是否可以按照给定的顺序出火车站。 火车站好比一个栈,火车进站代表入栈,火车出站代表出栈,可以模拟这个过程来判断是否符合题意。 不用stack的写法:         对读入数据后的for循环进行分析,每一个cnt代表是火车的序号,火车每出站和入站匹配时就匹配下一

GKHack's Blog 1442

POJ1363Rails队列和栈应用

C++实现POJ栈的老题1363 Rails。掌握该处的输入要求。同时练习把队列queue作为函数的形参。这里的问题是:如何把队列作为形参传给函数呢?

阿正的梦工坊 706

【POJ 1363 --- Rails 】栈

【POJ 1363 --- Rails 】栈 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 limi...

来日浅谈的博客 543

POJ Problem 1363 Rails 【栈】

Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31441   Accepted: 12222 Description There is a famous railway station in PopPush City. Country there is

博客已搬迁到http://www.cnblogs.com/cniwoq/ 365

POJ 1363 Rails (栈的基本应用)

题意:一组数据(1,2,3,4,……n……),要进栈,要求判断题目中给的数据是不是出栈的数据的可能情况。 分析:    首先注意一下输入输出问题,n个数字的测试数据,n为0时,结束,然后输入可能的出栈数据,如果第一个数据为0,结束这次循环,重新输入有多少个数字的测试数据。    其次,就是根据栈的一些知识来处理这一题了。栈中比栈顶元素小的数据一定要按逆序排列,才是正确的。 模拟一下压栈出栈过程就很

绝对快乐一生 575

POJ1363 Rails 栈+模拟

1.题目描述: Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33317   Accepted: 13003 Description There is a famous railway station in PopPush C

终有绿洲摇曳在沙漠 353

【Poj】-1363-Rails(栈)

Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31193   Accepted: 12115 Description There is a famous railway station in PopPush City. Country there

Bear1998的博客 463
上一篇: hihoCoder - 1038 - 01背包
下一篇: HDU - 1237 - 简单计算器(栈)
Violet-Guo
博客等级 码龄11年 585粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值