Stack ST19
QUESTION DESCRIPTION
You are a waiter at a party. There are N stacked plates on pile Ao .
Each plate has a number written on it. Then there will be q iterations. In i-th iteration, you start picking up the plates in Ai-1 from the top one by one and check whether the number written on the plate is divisible by the -th prime.
If the number is divisible, you stack that plate on pile Bi .
Otherwise, you stack that plate on pile Ai . After Q iterations, plates can only be on pile B1,B2,......Bq, Aq .
Output numbers on these plates from top to bottom of each piles in order of B1,B2,....Bq, Aq . Mandatory Declaration is "vector<int> p"
Input Format
The first line contains two space separated integers, N and Q.
The next line contains N space separated integers representing the initial pile of plates, i.e., Ao .
The leftmost value represents the bottom plate of the pile.
Constraints
1<=N<=5*10^4
2<=number(i)<=10^4
1<=Q<=1200
Output Format
Output N lines. Each line contains a number written on the plate. Printing should be done in the order defined above.
You are a waiter at a party. There are N stacked plates on pile Ao .
Each plate has a number written on it. Then there will be q iterations. In i-th iteration, you start picking up the plates in Ai-1 from the top one by one and check whether the number written on the plate is divisible by the -th prime.
If the number is divisible, you stack that plate on pile Bi .
Otherwise, you stack that plate on pile Ai . After Q iterations, plates can only be on pile B1,B2,......Bq, Aq .
Output numbers on these plates from top to bottom of each piles in order of B1,B2,....Bq, Aq . Mandatory Declaration is "vector<int> p"
Input Format
The first line contains two space separated integers, N and Q.
The next line contains N space separated integers representing the initial pile of plates, i.e., Ao .
The leftmost value represents the bottom plate of the pile.
Constraints
1<=N<=5*10^4
2<=number(i)<=10^4
1<=Q<=1200
Output Format
Output N lines. Each line contains a number written on the plate. Printing should be done in the order defined above.
TEST CASE 2
INPUT
INPUT
6 1
8 1 9 2 7 3
OUTPUT8
2
1
9
7
3
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
int q, n, v;
vector<int> primes;
primes.push_back(2);
primes.push_back(3);
for(int i = 5; i <= 10000; i++)
{
int no = 0;
for(int j = 2; j*j <= i; j++)
{
if(i%j == 0)
no = 1;
}
if(!no)
primes.push_back(i);
}
cin>>n>>q;
stack<int> stack1, stack2, stack3;
for(int i = 0 ; i < n; i++)
{
cin>>v;
stack1.push(v);
}
for(int i = 0 ; i < q; i++)
{
if(stack1.empty())
break;
int cur = primes[i];
while(!stack1.empty())
{
int ele = stack1.top();
stack1.pop();
if(ele%cur == 0)
{
stack2.push(ele);
}
else
{
stack3.push(ele);
}
}
while(!stack2.empty())
{
cout<<stack2.top()<<endl;
stack2.pop();
}
stack1 = stack3;
while(!stack3.empty())
stack3.pop();
}
while(!stack1.empty())
{
cout<<stack1.top()<<endl;
stack1.pop();
}
return 0;
}
Comments
Post a Comment