#include<bits/stdc++.h> // to include all the library functions
using namespace std; // to avoid writing the std:: repeatadly
struct card // using structure so that we can take the int and char at the same time
{
char suit; // for the character input
int num; // for the number inpput
int id; // to store original position for stability checking
};
int Partition(card A[],int p, int r) // partition function will be returning the required index to break the arrays into two parts
{
int x = A[r].num; // taking x as the pivot= last element of the array
int i = p-1; // following the given pseudocode
for(int j = p; j<=r-1;j++) // running the loop according to the pseudocode
{
if(A[j].num<= x) // comparing the integer values
{
i++; // increment
swap(A[i],A[j]); // exchanging the values of the array
}
}
swap(A[i+1],A[r]); // exchange the values , this will take the (x) to its required position
return i+1; // returning the value so that based on that we can do partition
}
/*Quicksort(A, p, r) --> copied it from the question so that i can stay at codeblocks and focus on writing the code
1 if p < r
2 then q = Partition(A, p, r)
3 run Quicksort(A, p, q-1)
4 run Quicksort(A, q+1, r) */
void Quicksort(card A[],int p, int r) // here is the main function that will do all the recursive stuffs
{
if(p<r) // initial condition to perform [similar to ( left<right) ]
{
int q = Partition(A,p,r); // collecting the index so that we can perform Quicksort on the two arrays
Quicksort(A,p,q-1); // recursive calling for the left sub-array
Quicksort(A,q+1,r); // recursive call for the right sub-array
}
}
int main() // main function -- > the code starts from here
{
int n; // declaring n as the size
cin >> n; // taking n from the user
card A[100005]; // main array (safe size for VJudge)
for(int i= 0; i<n;i++) // traversing the array using for loop
{
cin >> A[i].suit; // taking the character part input
cin >> A[i].num; // input the number part
A[i].id = i; // storing original position
}
Quicksort(A,0,n-1); // calling the function to do the operation [ here p = 0 & r = n-1 --> according to the question ]
bool stable = true; // to check the stability
for(int i = 1; i < n; i++) // checking stability properly
{
if(A[i].num == A[i-1].num) // only compare equal numbers
{
if(A[i].id < A[i-1].id) // order changed
{
stable = false; // if the condition is true then stable == false
break; // no need to check further if it is already false
}
}
}
if(stable) // just checking the conditions
cout << "Stable" << endl; // printing the ans
else // if not stable
cout << "Not stable" << endl; // printing the ans
for(int i = 0;i<n;i++) // loop to show the output
{
cout << A[i].suit << " " << A[i].num << endl; // showing the outputs
}
return 0; // end of the code
}