永发信息网

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

答案:2  悬赏:0  手机版
解决时间 2021-03-15 00:53
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
最佳答案
#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了这题,主要是用栈,重点部分都注释了,认真看应该没问题,打的很辛苦,求采纳
全部回答
lz你想说什么?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
求图片:玛莎百货logou
求初音未来桌面精灵会唱歌,会跳舞,可以聊天的
大家买的吉利新远景1.5手动幸福版是多少钱
金山陶瓷城地址在什么地方,想过去办事
vivo智能手机手机我主题设置成了那个小羊。但
喀什地区科技情报所我想知道这个在什么地方
125乘以0.8有小数吗
宝马x5车乘坐算上司机6人算不算超员
从慈湖路口到新桥镇怎么坐公交车,最快需要多
小学四年级四百字作文
新代系统自动运行控制状态怎么使用
恶儿媳妇不让公婆住新房.回到娘家后·她大嫂
绿色宝典地址有知道的么?有点事想过去
解签 中平签 第二十一前
アイロニ是什么意思?是歌名
推荐资讯
汽车上的玻璃可以用刀片刮吗
打架把人打死了会怎么判,请求解答!(细节在
彩膜壁纸怎么弄啊,买了彩膜卖家没有壁纸怎么
小刘2011年7月10日在中国建设银行存款20000元
天龙八部 前几集 武林上有很多人死在自己的成
来几张男生挥手再见的图片悲凉点的图片
天王河鱼饭店这个地址在什么地方,我要处理点
三星ps—rxa100c音箱如何拆装
天正打印机耗材批发在哪里啊,我有事要去这个
我的三星s5电信4G怎么查SN,还有,我的手机*#
岩家沟地址有知道的么?有点事想过去
vivo手机与OPPO手机有什么不同
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?