Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:求教。为什么会超时?问题已解决数组定义小了。重新附上代码。

Posted by 2326564155 at 2014-08-15 20:38:37 on Problem 1363
In Reply To:求教。为什么会超时? Posted by:2326564155 at 2014-08-15 19:54:30
> #include<cstdio>
> int a[1000],n;
> 
> int ok()
> {
>     int i,top=0,k=1;
>     int stack[1000];
>     for(i=1;i<=n;i++)
>      {
>         stack[++top]=i;
>        while(top&&stack[top]==a[k])
>          {
>            top--;
>            k++;
>          }
>      }
>      if(top)  return 0;
>      else  return 1;
> }
> 
> int main()
> {
>     int f=0,i;
>     while(scanf("%d",&n),n)
>     {
>        if(f)
>        printf("\n");
>        f=1;
>        while(scanf("%d",&a[1]),a[1])
>         {
>           for(i=2;i<=n;i++)
>             scanf("%d",&a[i]);
>           if(ok())
>           printf("Yes\n");
>           else  printf("No\n");
>         }
>      }
>    return 0;
> }
>           
>                            
更改过的,AC 
#include<cstdio>
int a[1010],n;

int ok()
{
    int i,top=0,k=1;
    int b[1010];
    memset(b,0,sizeof(b));
    for(i=1;i<=n;i++)
     {
        b[++top]=i;
       while(top&&b[top]==a[k])
         {
           top--;
           k++;
         }
     }
     if(top)  return 0;
     else  return 1;
}

int main()
{
    int f=0,i;
    while(scanf("%d",&n),n)
    {
       if(f)
       printf("\n");
       f=1;
       while(scanf("%d",&a[1]),a[1])
        {
          for(i=2;i<=n;i++)
            scanf("%d",&a[i]);
          if(ok())
          printf("Yes\n");
          else  printf("No\n");
        }
     }
   return 0;
}
          
                           
       
                           

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator