POJ 1363

简介: Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18846   Accepted: 7515 Description There is a famous railway station in PopPush City.
Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18846   Accepted: 7515

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
//我的第一个C++栈模板,值得纪念下 ,输入格式搞得我头大 
#include<cstdio>
#include<stack>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
    int i,j,k,T;
    int num,flag;
    while(scanf("%d",&T),T)
    {
        flag=0;//必须再次初始化为0 
        while(1)
        {
            stack <int> s;//定义成局部的。每次使用,便会重新清空 
            int temp=1;
            s.push(temp);
            for(i=1;i<=T;i++)
            {   
                scanf("%d",&num);
                //printf("num=%d\n",num);
                if(0==num)
                {
                    flag=1;
                    break;
                }
                if(s.empty())
                {
                    temp++;
                    s.push(temp);
                }
                int top=s.top();
                while(top!=num)
                {
                    if(temp==T)
                       break;
                    temp++;
                    s.push(temp);
                    top=s.top();
                }
                if(top==num)
                    s.pop();
            }
            if(flag)
                break;
            if(s.empty())
                puts("Yes");
            else
                puts("No");
        }
        printf("\n");
    }
    system("pause"); 
    return 0;
}
                
            
                
                
            
        
        
        

 

目录
相关文章
|
5月前
|
SQL Java 关系型数据库
【📕分布式锁通关指南 01】从解决库存超卖开始加锁的初体验
本文通过电商场景中的库存超卖问题,深入探讨了JVM锁、MySQL悲观锁和乐观锁的实现及其局限性。首先介绍了单次访问下库存扣减逻辑的正常运行,但在高并发场景下出现了超卖问题。接着分析了JVM锁在多例模式、事务模式和集群模式下的失效情况,并提出了使用数据库锁机制(如悲观锁和乐观锁)来解决并发问题。 悲观锁通过`update`语句或`select for update`实现,能有效防止超卖,但存在锁范围过大、性能差等问题。乐观锁则通过版本号或时间戳实现,适合读多写少的场景,但也面临高并发写操作性能低和ABA问题。 最终,文章强调没有完美的方案,只有根据具体业务场景选择合适的锁机制。
119 12
【📕分布式锁通关指南 01】从解决库存超卖开始加锁的初体验
|
7月前
|
人工智能 自然语言处理 安全
通义灵码新功能体验分享
通义灵码新功能体验分享
340 1
|
C++ Python 容器
C++中pair用法
⭐pair的简介 pair是C++STL(标准模板库)中的一个现有容器,它将2个数据整合成一组数据,当我们类似需求的时候就可以使用到pair啦!pair其实有点像Python中字典中的键值对(Key-Value),一个Key对应着一个Value。pair的本质其实就是个结构体,它含有两个成员变量first和second。因为使用的是struct不是class,所以在定义后是可以直接使用pair中的成员变量的。 其标准库类型–pair类型定义在#include< utility > 头文件中
487 0
|
机器学习/深度学习 算法 数据可视化
如何入门机器学习?需要学习哪些方面的知识
如何入门机器学习?需要学习哪些方面的知识
|
缓存 开发工具 Docker
debian部署docker(傻瓜式)
debian部署docker(傻瓜式)
458 0
|
缓存 监控 小程序
基于Spring Boot+VUE Java小程序商城项目(附源码),接私活利器
平台简介 基于RuoYi-Vue二开,集成了MybatisPlus、Avue、WxJava SDK MIT开源的微信二开利器,放心使用 专业的微信管理框架并加入小程序商城,是用来学习和实际项目的不二选择 前端采用Vue、Element UI、Avue。 后端采用Spring Boot、Spring Security、Redis & Jwt、Mybatis Plus、WxJava。
|
存储 C++ 容器
C++中deque的用法(超详细,入门必看)
⭐一、deque的简介 deque是一个双向队列(double-ended queue),可以在队列的两端进行元素的插入和删除操作。deque的全称是double-ended queue,翻译过来就是双端队列,也有人称之为双向队列,这两个名称都可以表示该数据结构。deque是C++STL(标准模板库)中的一种容器,可以用于存储各种类型的元素。deque的特点是可以在队列的两端进行元素的操作,并且可以高效地在队列的任意位置进行元素的插入和删除操作。 可以说deque几乎涵盖了queue(队列)、stack(堆栈)、vector(向量 )等的全部用法,功能非常的强大。
1206 0
|
C++
C++ 你会使用cmath库里的宏常量吗?(π、e、ln2、√2、(2/√π) 等等)
C++ 你会使用cmath库里的宏常量吗?(π、e、ln2、√2、(2/√π) 等等)
490 0
|
C语言 C++ 容器
C++中stack的用法(超详细,入门必看)
⭐一、stack的简介 stack的中文译为堆栈,堆栈一种数据结构。C语言中堆栈的定义及初始化以及一些相关操作实现起来较为繁琐,而C++的stack让这些都变得简便易实现。因为C++中有许多关于stack的方法函数。 堆栈(stack)最大的特点就是先进后出(后进先出)。就是说先放入stack容器的元素一定要先等比它后进入的元素出去后它才能出去。呃这样说可能有点绕哈哈,举个生活中的例子吧。 某一天,天气炎热,你买了一个冰淇淋甜筒,而这个冰淇淋甜筒在制作过程时冰淇淋是不是先进入到甜筒的底部然后到最上面呢,但是你在吃的过程中要从最上面吃起到最后才能吃到甜筒底部的冰淇淋,但底部的冰淇淋是先进入甜筒
2238 0
|
C语言 C++ 容器
C++中queue的用法(超详细,入门必看)
⭐一、queue的简介 queue的中文译为队列,队列是一种数据结构。C语言中队列的定义及初始化以及一些相关操作实现起来较为繁琐,而C++的queue让这些都变得简便易实现。因为C++中有着许多关于queue的方法函数。 队列(queue)最大的特点就是先进先出。就是说先放入queue容器的元素一定是要先出队列之后,比它后进入队列的元素才能够出队列。 举个生活中的例子吧。
2182 0
OSZAR »