ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1220. Stacks

Time limet Test 10
Posted by refragile 24 Oct 2011 02:30
How to solve?
// nalgoritm.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
//#include "conio.h"
#include "stdio.h"
short *A;
int *B;
int n,i;
    int last;
    int co;

void fun()
{
    int stack=A[i];
    for(int j=0;j<i;j++)
    {
        if(A[j]==stack&&B[j]!=(-1))
        {
        last=B[j];
        co=j;
        }
    }
    A[co]=(-1);
}

int main()
{
int j,nom,zn,k;
    char c;
    int element;
    i=k=0;
    scanf("%d",&n);
    A=new  short int[n];
    B=new  int[n];
    while(i<n){
j=0;
 scanf("%c",&c) ;
        while( c!=' ')
        {
         scanf("%c",&c);
           j++;
        }
        if(j==4)
        {
            scanf("%d",&nom);
        A[k]=nom;
        B[k]=(-1);
        k++;
        }
        else{
        scanf("%d %d",&nom,&zn);
        A[k]=nom;
        B[k]=zn;
        k++;
        }
        i++;
    }
    for(i=0;i<n;i++)
    {
        if(B[i]==(-1))
        {
            fun();
            printf("%d\n",last);

        }
    }
    //getch();
}
Re: Time limet Test 10
Posted by luckysundog 24 Oct 2011 03:34
TLE because you got O(N^2).