POJ1363:Rails
2013-07-22 14:59
351 查看
Description
ThereisafamousrailwaystationinPopPushCity.Countrythereisincrediblyhilly.Thestationwasbuiltinlastcentury.Unfortunately,fundswereextremelylimitedthattime.Itwaspossibletoestablishonlyasurfacetrack.
Moreover,itturnedoutthatthestationcouldbeonlyadead-endone(seepicture)andduetolackofavailablespaceitcouldhaveonlyonetrack.
ThelocaltraditionisthateverytrainarrivingfromthedirectionAcontinuesinthedirectionBwithcoachesreorganizedinsomeway.AssumethatthetrainarrivingfromthedirectionAhasN<=1000coachesnumberedinincreasingorder1,2,...,N.The
chieffortrainreorganizationsmustknowwhetheritispossibletomarshalcoachescontinuinginthedirectionBsothattheirorderwillbea1,a2,...,aN.Helphimandwriteaprogramthatdecideswhetheritispossibletogettherequiredorderofcoaches.
YoucanassumethatsinglecoachescanbedisconnectedfromthetrainbeforetheyenterthestationandthattheycanmovethemselvesuntiltheyareonthetrackinthedirectionB.Youcanalsosupposethatatanytimetherecanbelocatedasmanycoaches
asnecessaryinthestation.ButonceacoachhasenteredthestationitcannotreturntothetrackinthedirectionAandalsoonceithasleftthestationinthedirectionBitcannotreturnbacktothestation.
Input
Theinputconsistsofblocksoflines.Eachblockexceptthelastdescribesonetrainandpossiblymorerequirementsforitsreorganization.InthefirstlineoftheblockthereistheintegerNdescribedabove.Ineachofthe
nextlinesoftheblockthereisapermutationof1,2,...,N.Thelastlineoftheblockcontainsjust0.
Thelastblockconsistsofjustonelinecontaining0.
Output
Theoutputcontainsthelinescorrespondingtothelineswithpermutationsintheinput.AlineoftheoutputcontainsYesifitispossibletomarshalthecoachesintheorderrequiredonthecorrespondinglineoftheinput.Otherwise
itcontainsNo.Inaddition,thereisoneemptylineafterthelinescorrespondingtooneblockoftheinput.Thereisnolineintheoutputcorrespondingtothelast``null''blockoftheinput.
SampleInput
SampleOutput
ThereisafamousrailwaystationinPopPushCity.Countrythereisincrediblyhilly.Thestationwasbuiltinlastcentury.Unfortunately,fundswereextremelylimitedthattime.Itwaspossibletoestablishonlyasurfacetrack.
Moreover,itturnedoutthatthestationcouldbeonlyadead-endone(seepicture)andduetolackofavailablespaceitcouldhaveonlyonetrack.
ThelocaltraditionisthateverytrainarrivingfromthedirectionAcontinuesinthedirectionBwithcoachesreorganizedinsomeway.AssumethatthetrainarrivingfromthedirectionAhasN<=1000coachesnumberedinincreasingorder1,2,...,N.The
chieffortrainreorganizationsmustknowwhetheritispossibletomarshalcoachescontinuinginthedirectionBsothattheirorderwillbea1,a2,...,aN.Helphimandwriteaprogramthatdecideswhetheritispossibletogettherequiredorderofcoaches.
YoucanassumethatsinglecoachescanbedisconnectedfromthetrainbeforetheyenterthestationandthattheycanmovethemselvesuntiltheyareonthetrackinthedirectionB.Youcanalsosupposethatatanytimetherecanbelocatedasmanycoaches
asnecessaryinthestation.ButonceacoachhasenteredthestationitcannotreturntothetrackinthedirectionAandalsoonceithasleftthestationinthedirectionBitcannotreturnbacktothestation.
Input
Theinputconsistsofblocksoflines.Eachblockexceptthelastdescribesonetrainandpossiblymorerequirementsforitsreorganization.InthefirstlineoftheblockthereistheintegerNdescribedabove.Ineachofthe
nextlinesoftheblockthereisapermutationof1,2,...,N.Thelastlineoftheblockcontainsjust0.
Thelastblockconsistsofjustonelinecontaining0.
Output
Theoutputcontainsthelinescorrespondingtothelineswithpermutationsintheinput.AlineoftheoutputcontainsYesifitispossibletomarshalthecoachesintheorderrequiredonthecorrespondinglineoftheinput.Otherwise
itcontainsNo.Inaddition,thereisoneemptylineafterthelinescorrespondingtooneblockoftheinput.Thereisnolineintheoutputcorrespondingtothelast``null''blockoftheinput.
SampleInput
5 12345 54123 0 6 654321 0 0
SampleOutput
Yes No Yes
题意:按1~n进栈,然后给出一个顺序,看能否按照这个顺序出栈
#include<stdio.h>
intmain()
{
inta[1005],b[1005],i,j,k,n;
while(scanf("%d",&n),n)
{
while(scanf("%d",&b[0]),b[0])
{
for(j=1;j<n;j++)
scanf("%d",&b[j]);
for(i=1,j=0,k=0;i<=n&&j<n;i++,k++)
{
a[k]=i;
while(a[k]==b[j])
{
if(k>0)k--;
else
{
a[k]=0,k--;
}
j++;
if(k==-1)break;
}
}
if(j==n)printf("Yes\n");
elseprintf("No\n");
}
printf("\n");
}
}
相关文章推荐
- POJ 1363 Rails
- POJ 1363 Rails(模拟栈的过程)
- poj1363——Rails
- POJ 1363 Rails
- poj-1363-rails
- poj 1363 Rails
- POJ 1363 Rails 笔记
- 1125 POJ#1363 Rails
- Poj 1363 Rails
- poj 1363 Rails(栈)
- UVA 514 - Rails && POJ 1363 (堆栈模拟)
- OpenJudg / Poj 1363 Rails
- poj1363 Rails(栈的应用)
- POJ---1363-Rails(stack)
- poj 1363 Rails
- poj 1363(Stack ) Rails
- poj 1363 Rails
- Rails http://poj.org/problem?id=1363
- POJ 1363 Rails
- poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)