Algorithm of 11995 - I Can Guess the Data Structure!

Problem Description Link
Algorithm:
for this problem n=number of line x=element  you can use STL stack , queue, priority_queue
and when get 1 insert data each STL and increase  value for each STL variable(i.e s, q,p)
when get 2 chaeck
    1. for stack check stack not empty and stack top element = x 
        if condition true the pop from stack.
    2. for que  check queue not empty and queue front = x
        if condition true the pop from queue.
    3. for priority queue check priority_queue not empty and  priority_queue top=x
        if condition true the pop from priority_queue.
for each pop increase value that data(ie. s for stack pop ..)
finally check that value and n equal or not if equal then follow that data structure
(if n=6 )
6
1 1  //s=1,q=1,p=1;
1 2 //s=2,q=2,p=2;
1 3 //s=3,q=3,p=3;
2 1 //s=1,q=4,p=1;
2 2 //s=1,q=5,p=1;
2 3 // s=1,q=6,p=1;
so only q=6 so it is a queue
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience