2018学校暑期集训第四天——栈,树,优先队列

例题  ——   UVA - 514

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 file 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 file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last “null” block of the input file.

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
NoYes

题意:在中转站,车厢符合后进先出的原则,因此是一个栈。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;int a[1010];int main(void)
{int n;while(~scanf("%d", &n) && n){memset(a, 0, sizeof(a));stack<int> s;   while(1){scanf("%d", &a[1]);if(a[1]==0){cout << endl;break;}for(int i=2; i<=n; i++)scanf("%d", &a[i]);int x=1, y=1;int judge=1;while(y<=n){if(x==a[y]){x++;y++;}else if(!s.empty() && s.top()==a[y]){s.pop();y++;}else if(x<=n)s.push(x++);else{judge=0;break;}}if(judge)cout << "Yes" << endl;elsecout << "No" << endl; }}return 0;
}

暑期集训4:栈,树,优先队列 例 :  UVA - 514 ​​​​​​​​​​​​​​相关推荐

  1. 暑期集训2:ACM基础算法 例2:POJ-2456

    2018学校暑期集训第二天--ACM基础算法 例二  --   POJ - 2456 Aggressive cows Farmer John has built a new long barn, wi ...

  2. 暑期集训2:ACM基础算法 例1:POJ-1064

    2018学校暑期集训第二天--ACM基础算法 例一  --  POJ - 1064 Cable master Inhabitants of the Wonderland have decided to ...

  3. 暑期集训1:C++STL 例3:UVA-12100

    2018学校暑期集训第一天--C++与STL 例三  --  UVA - 12100 Printer Queue The only printer in the computer science st ...

  4. 暑期集训5:并查集 线段树 练习题A:  HDU - 1232 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题A  --   HDU - 1232 畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅 ...

  5. 暑期集训1:C++STL 例1:UVA-10815

    2018学校暑期集训第一天--C++与STL 例一  --  UVA - 10815 Andy's First Dictionary Description XY学长刚刚立下了再不过CET就直播xx的 ...

  6. 暑期集训1:C++STL 例2:UVA-10935

    2018学校暑期集训第一天--C++与STL 例二  --  UVA - 10935 Throwing cards away I Given is an ordered deck of n cards ...

  7. 暑期集训5:并查集 线段树 练习题G: HDU - 1754

    2018学校暑期集训第五天--并查集 线段树 练习题G  --   HDU - 1754 I Hate It 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让 ...

  8. 暑期集训5:并查集 线段树 练习题F:  HDU - 1166 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题F  --   HDU - 1166 敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A ...

  9. 暑期集训5:并查集 线段树 练习题B: HDU - 1213 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题B  --   HDU - 1213 How Many Tables Today is Ignatius' birthday. He invites ...

最新文章

  1. Stream Processing:滑动窗口的聚集(aggregation)操作的优化算法讲解
  2. html border阴影效果_【开发小技巧】—如何使用HTML和CSS创建浮动框效果?
  3. apache应用进阶
  4. mysql having
  5. 硅谷还是程序员的“圣地”吗?
  6. marmalade android 5.0 JNI 调用失败的解决方案
  7. android学习笔记1-布局的初步了解以及 控件的常见属性
  8. 类图中表达总体与局部的关系_软件工程测试题3
  9. 微信小程序人脸识别认证-微信开放接口
  10. 分享各类优秀与设计师制作公司相关的资源网站
  11. python面试题-如“上海 深圳 深圳 上海“,要求输入一个匹配模式,比如: aabb,判断是否符合
  12. 短视频后期要做哪些内容?注意细节才能做出优质短视频
  13. 35岁以上程序员求职没市场?互联网大厂100道Android面试题助你冲关金三银四!终局之战
  14. 高校先行,科技部公示10个国家级区块链重点项目 | 产业区块链发展周报
  15. 谁是滕尚华?两获哥德尔奖,上交大校友,喜欢「躺平式」科研
  16. 禁止视频网站使用Flash P2P上传
  17. Qt编写可视化大屏电子看板系统5-恢复布局
  18. python应用程序无法正常启动0xc0000142_应用程序无法正常启动(0xc0000142)的解决办法...
  19. 如何让您的第一个物联网产品成功
  20. 类的学习Java写查询星座小程序

热门文章

  1. Rancher部署Traefik实现微服务的快速发现
  2. 懒汉式单例和饿汉式单例优缺点
  3. 性能测试学习过程中遇到的问题与解答1
  4. Android 取得 ListView中每个Item项目的值
  5. 06.SQLServer性能优化之---数据库级日记监控
  6. ipvsadm的几个参数输出的说明
  7. Cisco交换机解决网络蠕虫病毒***问题
  8. Apache2.4.x下proxy_module、proxy_fcgi_module结合PHP-FPM解决内存不足问题
  9. Linux01-基本操作与Shell
  10. 打开云服务器连不上网,云服务器怎么连接网络连接不上