【Poj】-1363-Rails(栈)
2016-07-20 19:34
337 查看
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 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
Sample Output
这道题和Train Problem I
基本一样,就是照着那道题的思路来写的,详情请见那题解析,嘿嘿
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int a[1010],b[1010];
while(~scanf("%d",&b[1]))
{
if(!b[1]) //n为一个值多次输入b[i],直到b[1]为0
{
printf("\n");
break;
}
int z=1,flag=1;
for(int i=1;i<=1000;i++)
a[i]=i; //进站台的车的排序一定为1,2,3......
for(int i=2;i<=n;i++)
scanf("%d",&b[i]);
stack<int>sk;
for(int i=1;i<=n;i++)
{
if(a[z]==b[i])
{
z++;
continue; //进出相同,继续
}
else if(!sk.empty()&&b[i]==sk.top())
sk.pop(); //果栈里不为空,且目标元素b[i]与栈顶元素相同则弹出栈顶元素
else if(z<=n)
{
sk.push(a[z++]); //面两种情况都不符合且a中有没有进栈的,就进入
i--; // 此时b不动,则i应该减一,恢复原值
}
else
flag=0;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31193 | Accepted: 12115 |
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
这道题和Train Problem I
基本一样,就是照着那道题的思路来写的,详情请见那题解析,嘿嘿
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int a[1010],b[1010];
while(~scanf("%d",&b[1]))
{
if(!b[1]) //n为一个值多次输入b[i],直到b[1]为0
{
printf("\n");
break;
}
int z=1,flag=1;
for(int i=1;i<=1000;i++)
a[i]=i; //进站台的车的排序一定为1,2,3......
for(int i=2;i<=n;i++)
scanf("%d",&b[i]);
stack<int>sk;
for(int i=1;i<=n;i++)
{
if(a[z]==b[i])
{
z++;
continue; //进出相同,继续
}
else if(!sk.empty()&&b[i]==sk.top())
sk.pop(); //果栈里不为空,且目标元素b[i]与栈顶元素相同则弹出栈顶元素
else if(z<=n)
{
sk.push(a[z++]); //面两种情况都不符合且a中有没有进栈的,就进入
i--; // 此时b不动,则i应该减一,恢复原值
}
else
flag=0;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
相关文章推荐
- 2016 Multi-University Training Contest 1 1006 PowMod
- Climbing Stairs
- Fail-Fast机制详解
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
- HDU 4300 Clairewd’s message
- HDU5729 2016 Multi-University Training Contest 1 (dp+连通图)
- Ajax请求response下显示failed to load response data
- 绘制基础知识-canvas paint
- ARC forbids explicit message send of 'release' 'release' is unavailable: not available in automatic
- soft raid5阅读笔记之四--读成员磁盘错误的处理
- soft raid5阅读笔记之三--写数据流程
- soft raid5阅读笔记之二--读数据流程
- 【杭电-oj】-1022-Train Problem I (栈,好)
- 项目运行时,直接在main.m停住
- rails常用语法
- translatesAutoresizingMaskIntoConstraints
- 2016 Multi-University Training Contest 1 1005 Necklace
- poj2387(堆优化dijiskla模版,通过pair写的)
- AIDL的实现