POJ1363

思路:1~N个数每来一个数就入栈,若栈顶元素与 给出序列中l所指向的元素相等则出栈直到栈为空或不相等为止,如最后栈为空则Yes不为空则No

#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=1005;
int a[MAXN];
int N;
int s[MAXN];
int cnt;
int main()
{
    while(scanf("%d",&N)!=EOF&&N)
    {
        while(scanf("%d",&a[1])!=EOF&&a[1]!=0)
        {
            cnt=0;
            for(int i=2;i<=N;i++)
            {
                scanf("%d",&a[i]);
            }
            int l=1;
            for(int i=1;i<=N;i++)
            {
                s[cnt++]=i;
                while(s[cnt-1]==a[l]&&cnt!=0)
                {
                    cnt--;
                    l++;
                }

            }
            if(cnt==0)    printf("Yes\n");
            else         printf("No\n");
        }
        printf("\n");
    }

    return 0;
}
时间: 2024-10-05 17:57:14

POJ1363的相关文章

POJ1363 Rails 验证出栈序列问题(转)

题目地址: http://poj.org/problem?id=1363 此题只需验证是否为合法的出栈序列. 有两个思路:1.每个已出栈之后的数且小于此数的数都必须按降序排列.复杂度O(n^2),适合人脑. //思路 1 不对!!! 例如 数据 ,               3 5 2 4 1              --------                正确答案为 no 2.另一个思路就是直接模拟入栈出栈过程.虽然模拟毫无技巧可言,但复杂度O(n),优于算法1.适合电脑. 代码如

火车进出站(POJ1363)

题目链接:http://poj.org/problem?id=1363 #include <stdio.h> #include <stack> using namespace std; int ans[10000]; ///出栈秩序 int main() { int t; ///有多少车厢 while(scanf("%d",&t),t) { stack<int>s; while(scanf("%d",&ans[0]

POJ1363【栈】

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. Moreove

poj1363——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. Moreove

POJ1363 Rails【stack】【栈】

Rails Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25638 Accepted: 10068 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

poj1363 Rails(栈模拟)

D - Rails Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1363 Appoint description: System Crawler (2016-05-09) Description There is a famous railway stati