永发信息网

zoj 1259,例子过了,wa,求解。。程序有注释哦~~~

答案:2  悬赏:0  手机版
解决时间 2021-03-15 00:53
  • 提问者网友:星軌
  • 2021-03-14 11:47
Rails

--------------------------------------------------------------------------------

Time Limit: 1 Second Memory Limit: 32768 KB

--------------------------------------------------------------------------------

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
最佳答案
  • 五星知识达人网友:未来江山和你
  • 2021-03-14 12:30
#include
#include
#include
using namespace std;
int b[1100];//数组用来存输入的序列
int main()
{
stack a;//保存序列的栈
stack xb;//序列中元素对应的下标的栈
int n,i,p,x;
while(scanf("%d",&n)!=EOF&&n)//序列的个数
{
while(scanf("%d",&b[0])!=EOF&&b[0])//序列的第一个数不为0就继续
{
while(!a.empty())//把栈清空
{
a.pop();
}
while(!xb.empty())//把栈清空
{
xb.pop();
}
p=1;//用来判断序列是否能变成1 2 3 ···n,假设能,p=1,不能,p=0
for(i=1;i {
scanf("%d",&b[i]);//输入剩余的序列
}
xb.push(n);//序列中最后一个位置的数的下标为n
for(i=n-1;i>=0;i--)//从序列的后面往前来
{
while(!a.empty()&&b[i] {
if(a.top()!=xb.top())//如果序列栈顶的值与下标栈顶的值不相同
{
p=0;//则该序列不能变成1 2 3····n
break;//跳出循环
}
a.pop();//序列栈顶元素出栈
x=xb.top();//保存下标栈顶的值
xb.pop();//下标栈顶出栈
xb.push(x-1);//x-1进栈
}
if(p==0)
break;
a.push(b[i]);//当前序列元素进栈
}
if(p==0)
cout<<"No"< else
cout<<"Yes"< }
cout< }
return 0;
}
我AC了这题,主要是用栈,重点部分都注释了,认真看应该没问题,打的很辛苦,求采纳
全部回答
  • 1楼网友:冷風如刀
  • 2021-03-14 12:57
lz你想说什么?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯