Problem:
Quick Sort
Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:
Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4     do if A[j] <= x
5        then i = i+1
6            exchange A[i] and A[j] 
7 exchange A[i+1] and A[r]
8 return i+1


Quicksort(A, p, r)
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)
Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.
Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).
Input
The first line contains an integer
n
, the number of cards.
n
cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.
Output
In the first line, print the stability ("
Stable
" or "
Not stable
") of this output.
In the following lines, print the arranged cards in the same manner of that of the input.
Constraints
1 ≤
n
≤ 100,000
1 ≤ the number of a card  ≤ 10
9
There are no identical card in the input
Sample Input 1
6
D 3
H 2
D 1
S 3
D 2
C 1
Sample Output 1
Not stable
D 1
C 1
D 2
H 2
D 3
S 3
Sample Input 2
2
S 1
H 1
Sample Output 2
Stable
S 1
H 1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Card {
    char suit;
    int value;
};

// partition (ALDS1_6_B 準拠)
int partition(vector<Card> &A, int p, int r) {
    int x = A[r].value;
    int i = p - 1;
    for (int j = p; j < r; j++) {
        if (A[j].value <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

void quicksort(vector<Card> &A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quicksort(A, p, q-1);
        quicksort(A, q+1, r);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<Card> A(n), B(n);

    for (int i = 0; i < n; i++) {
        cin >> A[i].suit >> A[i].value;
        B[i] = A[i]; // コピー
    }

    // クイックソート
    quicksort(A, 0, n-1);

    // 安定ソート (std::stable_sort)
    stable_sort(B.begin(), B.end(), [](const Card &c1, const Card &c2){
        return c1.value < c2.value;
    });

    // 安定性判定
    bool stable = true;
    for (int i = 0; i < n; i++) {
        if (A[i].suit != B[i].suit) {
            stable = false;
            break;
        }
    }

    cout << (stable ? "Stable" : "Not stable") << "\n";
    for (int i = 0; i < n; i++) {
        cout << A[i].suit << " " << A[i].value << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4912
final_rank: 1

code_snippet: #include <iostream>
using namespace std;

typedef struct
{
  char c;
  int num;
} Card;

bool flag = 1;

int partition(Card *A, int p, int r)
{
  Card temp;
  int x = A[r].num;
  int i = p - 1;
  for (int j = p; j < r; j++)
  {
    if (A[j].num <= x)
    {
      i++;
      temp = A[j];
      A[j] = A[i];
      A[i] = temp;
    }
  }

  // for (int j = i + 2; j <= r; j++)
  // {
  //   if (A[i + 1].num == A[j].num)
  //   {
  //     flag = 0;
  //   }
  // }

  temp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = temp;
  return i + 1;
}

void quicksort(Card *A, int p, int r)
{
  if (p < r)
  {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int main()
{
  int n;
  cin >> n;
  Card A[n];
  Card Old[n];

  for (int i = 0; i < n; i++)
  {
    cin >> A[i].c >> A[i].num;
  }
  for (int i = 0; i < n; i++)
  {
    Old[i] = A[i];
  }

  quicksort(A, 0, n - 1);

  int pre_num = A[0].num;
  int k;
  char pre_char = A[0].c;
  for (int i = 1; i < n; i++)
  {
    if (A[i].num == pre_num)
    {
      k = 0;
      while (true)
      {
        if (Old[k].num == pre_num && Old[k].c == pre_char)
        {
          break;
        }
        else if (Old[k].num == A[i].num && Old[k].c == A[i].c)
        {
          flag = 0;
          break;
        }
        k++;
      }
    }
    if (!flag)
    {
      break;
    }
    pre_num = A[i].num;
    pre_char = A[i].c;
  }

  if (flag)
  {
    cout << "Stable" << endl;
  }
  else
  {
    cout << "Not stable" << endl;
  }

  for (int i = 0; i < n; i++)
  {
    cout << A[i].c << " " << A[i].num << endl;
  }
}
accuracy: 1
time_ms: 80
memory_kb: 4804
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int partition(vector<pair<char,int>> &A, int p, int r) {
    int x = A[r].second;
    int i = p - 1;
    for (int j = p; j <= r - 1; j++) {
        if (A[j].second <= x) {
            i = i + 1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;
}

void quicksort(vector<pair<char,int>> &A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quicksort(A, p, q - 1);
        quicksort(A, q + 1, r);
    }
}

int main(){
    int n;
    cin >> n;
    vector<pair<char,int>> A;
    for (int i = 0; i < n; i++) {
        char tempchar;
        int tempint;
        cin >> tempchar >> tempint;
        A.push_back({tempchar,tempint});
    }

    vector<pair<char,int>> B = A; 
    
    quicksort(A, 0, n - 1);


    stable_sort(B.begin(), B.end(), [](const pair<char, int>& a, const pair<char, int>& b) {
        return a.second < b.second;
    });

    if(A == B){
        cout << "Stable" << endl;
    }
    else{
        cout << "Not stable" << endl;
    }

    for (int i = 0; i < n; i++) {
        cout << A[i].first << " " << A[i].second << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 5512
final_rank: 11

code_snippet: #include <iostream>
#include <vector>

using namespace std;

struct trump {
    char c;
    int n;
    int order;
};

int partition(vector<trump>& A, int p, int r){
    int x = A[r].n;
    int i = p-1;
    for (int j=p;j<r;j++){
        if (A[j].n <= x){
            i = i+1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

void Qs(vector<trump>& A, int p, int r){
    if (p < r){
        int q = partition(A, p, r);
        Qs(A, p, q-1);
        Qs(A, q+1, r);
    }
}

int main(){
    int n, p;
    cin >> n;
    vector<trump> A(n);
    for (int i=0;i<n;i++){
            cin >> A[i].c;
            cin >> A[i].n;
            A[i].order = i;
    }
    Qs(A, 0, n-1);

    bool check = true;
    p = 0;
    for (int i=0;i<n;i++){
        if (i < p) {continue;}
        if (!check) {
            
            break;
        }
        for (int j=i+1;j<n;j++){
            if (A[i].n == A[j].n){
                if (A[i].order > A[j].order){
                    // cout << i  << "," << j << endl;
                    check = false;
                    break;
                }
            } else {
                p = j;
                break;
            }
        }
    }

    if (check){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }

    for (int i=0;i<n;i++){
        cout << A[i].c << " " << A[i].n << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 4136
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Card {
    char suit;
    int value;
    int id; // 入力順を保持して安定性判定に使用
};

int partition(vector<Card> &A, int p, int r) {
    int x = A[r].value;
    int i = p - 1;
    for (int j = p; j < r; j++) {
        if (A[j].value <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;
}

void quickSort(vector<Card> &A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quickSort(A, p, q - 1);
        quickSort(A, q + 1, r);
    }
}

// 比較のための安定ソート（C++ の stable_sort）
vector<Card> stableSorted(const vector<Card> &A) {
    vector<Card> B = A;
    stable_sort(B.begin(), B.end(),
                [](const Card &c1, const Card &c2) {
                    return c1.value < c2.value;
                });
    return B;
}

int main() {
    int n;
    cin >> n;
    vector<Card> A(n);

    for (int i = 0; i < n; i++) {
        cin >> A[i].suit >> A[i].value;
        A[i].id = i; // 入力順を覚える
    }

    vector<Card> quick = A;
    quickSort(quick, 0, n - 1);

    vector<Card> stable = stableSorted(A);

    // 安定性チェック
    bool isStable = true;
    for (int i = 0; i < n; i++) {
        // 値も同じ、スートも同じ、そして stable_sort の順序と一致するか
        if (quick[i].suit != stable[i].suit ||
            quick[i].value != stable[i].value) {
            isStable = false;
            break;
        }
    }

    cout << (isStable ? "Stable" : "Not stable") << endl;
    for (int i = 0; i < n; i++) {
        cout << quick[i].suit << " " << quick[i].value << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 7076
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int partition(vector<int>&A,vector<char>&B,vector<int>&C,int p, int r){
    int i,x;
    x = A[r];
    i = p-1;
    for(int j = p; j < r; ++j){
        if (A[j] <= x){
            i += 1;
            swap(A[i], A[j]);
            swap(B[i], B[j]);
            swap(C[i], C[j]);
        } 
    }
    swap(A[i+1], A[r]);
    swap(B[i+1], B[r]);
    swap(C[i+1], C[r]);
    return i+1;
}
void quick_sort(vector<int>&A,vector<char>&B,vector<int>&C,int p, int r){
    int q;
    if(p < r){
        q = partition(A, B, C, p, r);
        quick_sort(A, B, C, p, q-1);
        quick_sort(A, B, C, q+1, r);
    }
}
int main(){
    int n;
    cin >> n;
    vector<int> A(n); 
    vector<char> B(n);
    vector<int> C(n);
    for(int i = 0; i < n; ++i){
        cin >> B[i] >> A[i];
        C[i] = i;
    }
    quick_sort(A, B, C, 0, n-1);
    

    int result = 1;
    for(int i = 0; i < n; ++i){
        for(int j = 0;j < 3; ++j){
            if(A[i] == A[i+j] && C[i] > C[i+j]){
                result = 0;
                break;
            }
        }
    }
    if(result == 0){
        cout << "Not stable" << endl;
    }else{
        cout << "Stable" << endl;
    }

    for(int i = 0; i < n; ++i){
        cout << B[i] << " " << A[i] << endl;
    }
    
}
accuracy: 1
time_ms: 90
memory_kb: 3880
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <utility>

using namespace std;

struct card {
    char suut;
    int num;
    int order;
};

int partition(vector<card> &c, int p, int r){
    int x = c[r].num;
    int i = p-1;
    for (int j=p; j<r; j++){
        if (c[j].num <= x){
            i = i+1;
            swap(c[i],c[j]);
        }
    }
    swap(c[i+1],c[r]);

    return i+1;
}

void quickSort(vector<card> &c, int p, int r){
    if (p < r){
        int q = partition(c,p,r);
        quickSort(c,p,q-1);
        quickSort(c,q+1,r);
    }
}

int main(){
    char x0;
    int n,x1;
    vector<card> c;
    bool s = true;

    cin >> n;
    for (int k=0; k<n; k++){
        cin >> x0 >> x1;
        c.push_back({x0,x1,k});
    }

    quickSort(c,0,n-1);

    for(int i=1; i<n; i++){
        if(c[i].num==c[i-1].num && c[i].order<c[i-1].order){
            s = false;
            break;
        }
    }
    if (s == true){
        cout << "Stable" << "\n";
    } else {
        cout << "Not stable" << "\n";
    }

    for (int t=0; t<n; t++){
        cout << c[t].suut << " " << c[t].num << "\n";
    }
}
accuracy: 1
time_ms: 30
memory_kb: 4792
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>

using namespace std;
const int INFTY = std::numeric_limits<int>::max();

struct Card{
    char s; // suit
    int n; // number
};

int partition(vector<Card>& A, int p, int r){
    int x = A[r].n;
    int i = p-1;
    for (int j = p; j < r; j++) {
        if (A[j].n <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i+1;
}

void quickSort(vector<Card>& A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quickSort(A, p, q-1);
        quickSort(A, q+1, r);
    }
}

void merge(vector<Card> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    vector<Card> L(n1 + 1);
    vector<Card> R(n2 + 1);

    for (int i = 0; i < n1; i++) {
        L[i] = A[left + i];
    }
    for (int j = 0; j < n2; j++) {
        R[j] = A[mid + j];
    }

    L[n1].n = INFTY;
    R[n2].n = INFTY;

    int i = 0;
    int j = 0;

    for (int k = left; k < right; k++) {
        if (L[i].n <= R[j].n) {
            A[k] = L[i];
            i += 1;
        } else {
            A[k] = R[j];
            j += 1;
        }
    }
}

void mergeSort(vector<Card> &A, int left, int right) {
    if (left+1 < right) {
        int mid = (left + right) / 2;

        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main() {
    int n;
    cin >> n;
    vector<Card> A(n), B(n);
    for (int i = 0; i < n; i++) {
        cin >> A[i].s;
        cin >> A[i].n;
    }

    B = A;

    quickSort(A, 0, n-1);
    mergeSort(B, 0, n);

    int Nst = 0; // Not stable

    for (int i = 0; i < n; i++) {
        if (A[i].s != B[i].s) {
            Nst = 1;
            break;
        }
    }

    if (Nst == 1) {
        cout << "Not stable" << endl;
    } else {
        cout << "Stable" << endl;
    }

    for (int i = 0; i < n; i++) {
        cout << A[i].s << " " << A[i].n << endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 5336
final_rank: 15

code_snippet: using namespace std;
#include <iostream>
#include <climits>
#include <vector>

struct Card {
    char suit;
    int value;
};

void merge(vector<Card>& A, int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    vector<Card> L(n1 + 1);
    vector<Card> R(n2 + 1);
    for(int i = 0; i < n1; i++){
        L[i] = A[left + i];
    }
    for(int i = 0; i < n2; i++){
        R[i] = A[mid + i];
    }
    L[n1].value = INT_MAX;
    R[n2].value = INT_MAX;
    int i = 0;
    int j = 0;
    for(int k = left; k < right; k++){
        if(L[i].value <= R[j].value){
            A[k] = L[i];
            i = i + 1;
        }else{
            A[k] = R[j];
            j = j + 1;
        }
    }
}

int partition(vector<Card>& A, int p, int r){
    int x = A[r].value;
    int i = p-1;
    for(int j = p; j < r; j++){
        if(A[j].value <= x){
            i = i+1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

void mergeSort(vector<Card>& A, int left, int right){
    if(left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

void quicksort(vector<Card>& A, int p, int r){
    if(p < r){
        int q = partition(A, p, r);
        quicksort(A, p, q-1);
        quicksort(A, q+1, r);
    }
}

bool isStable(const vector<Card>& mergeSorted, const vector<Card>& quickSorted,int n){
    for(int i = 0; i < n; i++){
        if(mergeSorted[i].suit != quickSorted[i].suit || mergeSorted[i].value != quickSorted[i].value){
            return false;
        }
    }
    return true;
}


int main(){
    int n;
    cin >> n;
    vector<Card> cards(n);
    for(int i = 0; i < n; i++){
        cin >> cards[i].suit >> cards[i].value;
    }
    vector<Card> sorted = cards;
    quicksort(sorted,0,n-1);
    mergeSort(cards,0,n);
    
    if(isStable(cards, sorted,n)){
        cout << "Stable" << endl;
    } else {
        cout << "Not stable" << endl;
    }

    for(int i = 0; i < n; i++){
        cout << sorted[i].suit << " " << sorted[i].value << endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 5404
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

int pertition(vector<char>& A, vector<int>& B, vector<int>& C, int p, int r){
    int x = B[r];
    int i = p - 1;
    for (int j = p; j < r; j++){
        if (B[j] <= x){
            i++;
            swap(A[i], A[j]);
            swap(B[i], B[j]);
            swap(C[i], C[j]);
        }
    }
    swap(A[i + 1], A[r]);
    swap(B[i + 1], B[r]);
    swap(C[i + 1], C[r]);

    return i + 1;
}

void quicksort(vector<char>& A, vector<int>& B, vector<int>& C, int p, int r){
    if (p < r) {
        int q = pertition(A, B, C, p, r);
        quicksort(A, B, C, p, q - 1);
        quicksort(A, B, C, q + 1, r);
    }
}

int main(void){
    int n;
    cin >> n;
    vector<char> A_suit(n);
    vector<int> A_value(n);
    vector<int> A_order(n);

    for(int i = 0; i < n; i++){
        cin >> A_suit[i] >> A_value[i];
        A_order[i] = i;
    }

    quicksort(A_suit, A_value, A_order, 0, n - 1);

    bool stable = true;
    int j = 0;
    while(stable){
        j++;
        if (A_value[j - 1] == A_value[j]) {
            if (A_order[j - 1] > A_order[j]) {
                stable = false;
            }
        }
        if (j == n) {
            break;
        }
    }

    if (stable) {
        cout << "Stable" << endl;
    } else {
        cout << "Not stable" << endl;
    }

    for (int i = 0; i < n; i++) {
        cout << A_suit[i] << " " << A_value[i] << endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 3780
final_rank: 7

code_snippet: #include<iostream>
#include <climits>
using namespace std;

struct Card{
    char al;
    int num;
};

void merge( struct Card A[], int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    Card L[n1+1];
    Card R[n2+1];
  
  for(int i = 0; i < n1; i++){
      L[i] = A[left + i];
  }
  for(int i = 0; i < n2; i++){
      R[i] = A[mid + i];   
  }
  L[n1].num = INT_MAX;
  R[n2].num = INT_MAX;
  int i = 0;
  int j = 0;
  for(int k = left; k < right; k++){
    if (L[i].num <= R[j].num){
      A[k] = L[i];
      i = i + 1;
    }else{ 
      A[k] = R[j];
      j = j + 1;
    }
  }
}

void mergeSort(struct Card A[], int left, int right){
    int mid = (left + right)/2;
    if(left+1 < right){
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    }
    merge(A, left, mid, right);
}


int partition(struct Card A[],int p,int r){
    int x = A[r].num;
    int i = p-1;
    for(int j = p; j < r;j++){
        if(A[j].num<=x){
            i++;
            Card b;
            b = A[j];
            A[j] = A[i];
            A[i] = b;
        }
    }
    Card c = A[i+1];
    A[i+1] = A[r];
    A[r] = c;
    return i+1;
}
void quicksort(struct Card A[],int p,int r){
    if(p<r){
        int q = partition(A,p,r);
        quicksort(A,p,q-1);
        quicksort(A,q+1,r);
    }
}

int main(){
    int n;
    cin >> n;
    Card A[n],B[n];
    for(int i = 0;i<n;i++){
        cin >> A[i].al >> A[i].num;
        B[i] = A[i];
    }
    quicksort(A,0,n-1);
    mergeSort(B,0,n);
    
    bool Stable = true;
    for(int i = 0; i < n; i++ ){
        if(A[i].al != B[i].al){
            Stable = false;
            break;
        }
    }
    if(Stable){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }
    
    for(int i = 0; i<n;i++){
        cout << A[i].al << " " << A[i].num << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 5776
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <utility>
using namespace std;

struct Card{
    char style;
    int num;
    int index;
};

int partition(vector<Card>&A,int p,int r){
    int x=A[r].num;
    int i=p-1;
    for(int j=p; j<r; j++){
        if(A[j].num<=x){
            i++;
            swap(A[i],A[j]);
        }
    }
    swap(A[i+1],A[r]);
    return i+1;
}

void quickSort(vector<Card>&A,int p,int r){
    if (p < r){
        int q = partition(A,p,r);
        quickSort(A,p,q-1);
        quickSort(A,q+1,r);
    }
}

bool isStableDirect(vector<Card>&A) {
    for (int i = 1; i < A.size(); i++) {
        if (A[i-1].num == A[i].num) {
            if (A[i-1].index > A[i].index) {
                return false;
            }
        }
    }
    return true;
}

int main(){
    int n;
    cin >> n;
    vector<Card> card(n); 
    for(int i=0;i<n;i++){
        cin >> card[i].style >> card[i].num;
        card[i].index = i;
    }

    quickSort(card,0,n-1);

    if(isStableDirect(card)){
        cout << "Stable" << endl;
    }
    else{
        cout << "Not stable" << endl;
    }
    for(int i=0;i<n;i++){
        cout << card[i].style << " " << card[i].num << endl;
    }
}
accuracy: 1
time_ms: 80
memory_kb: 4044
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
using namespace std;



struct card{
    char s;
    int v;
};

const int INF =2000000000;
int partition(vector<card>&A, int p, int r){
    card x = A[r];
    int i = p - 1;
    for (int j = p; j < r; j++){
        if (A[j].v <= x.v){
            i = i + 1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1],A[r]);
    return i+1;
}

void quicksort(vector<card>&A,int p,int r){
    if (p < r){
    int q = partition(A,p,r);
    quicksort(A,p,q-1);
    quicksort(A,q+1,r);
    }

}

void merge(vector<card> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;
    vector<card> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];
    L[n1].v = INF;
    R[n2].v = INF;  // 番兵

    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        if (L[i].v <= R[j].v) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
        }
    }
}

void mergeSort(vector<card> &A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main(){
    int n;
    cin >> n;
    vector<card> A(n), B(n);
    for (int i = 0; i < n; i++){
        cin >> A[i].s >> A[i].v;
        B[i] = A[i];
    }
    quicksort(A,0,n-1);
    mergeSort(B,0,n);

    bool stable =true;
    for (int i =0; i < n; i++ ){
         if (A[i].v != B[i].v || A[i].s != B[i].s) {
        stable = false;
            stable = false;
        }
    }

    if(stable == true)cout << "Stable";
    else             cout << "Not stable";
    cout << endl;
    for(int i = 0; i < n; i++){
        cout << A[i].s << " " << A[i].v << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5352
final_rank: 16

code_snippet: #include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

int partition(vector<pair<char, int>>& A, int p, int r) {
    int x = A[r - 1].second;
    int i = p - 1;
    for (int j = p; j < r - 1; ++j) {
        if (A[j].second <= x) {
            ++i;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r - 1]);
    return i + 1;
}

void quickSort(vector<pair<char, int>>& A, int p, int r) {
    if (p < r - 1) {
        int q = partition(A, p, r);
        quickSort(A, p, q);
        quickSort(A, q + 1, r);
    }
}

int main() {
    int n;
    cin >> n;
    vector<pair<char, int>> A(n);
    unordered_map<int, string> M;
    for (int i = 0; i < n; ++i) {
        cin >> A[i].first >> A[i].second;
        M[A[i].second] += A[i].first;
    }

    bool isStable = true;
    quickSort(A, 0, A.size());
    int num = A[0].second;
    string ord = "";
    for (int i = 0; i < n; ++i) {
        if (A[i].second != num) {
            if (M[num] != ord) {
                isStable = false;
                break;
            }
            num = A[i].second;
            ord = "";
        }
        ord += A[i].first;
    }

    if (isStable) {
        cout << "Stable" << endl;
    } else {
        cout << "Not stable" << endl;
    }

    for (int i = 0; i < n; ++i) {
        cout << A[i].first << " " << A[i].second << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 5752
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Card {
    char suit;
    int value;
};

int partition(vector<Card> &A, int p, int r){
    int x = A[r].value;
    int i = p-1;
    
    for(int j=p;j<r;++j){
        if(A[j].value<=x){
            i = i+1;
            swap(A[i],A[j]);
        }
    }
    swap(A[i+1],A[r]);
    
    return i+1;
}

void quickSort(vector<Card> &A, int p, int r){
   if(p<r){
   int q = partition(A,p,r); 
   quickSort(A, p, q-1);
   quickSort(A, q+1, r);
   }
}

bool isStable(const vector<Card> &quicked, const vector<Card> &stable){
    int n = quicked.size();
    for(int i = 0;i<n;++i){
        if(quicked[i].suit != stable[i].suit){
            return false;
        }
    }
    return true;
}

int main(){
    int n;
    cin >> n;
    
    vector<Card> A(n);
    vector<Card> B(n);
    
    for(int i=0;i<n;++i){
        cin >> A[i].suit >> A[i].value;
        B[i] = A[i];
    }
    
    stable_sort(B.begin(), B.end(), [](const Card &a, const Card &b) {
        return a.value < b.value;
    });

    quickSort(A, 0, n-1);
    
    if(isStable(A,B)){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }


    for(int i=0;i<n;i++){
        cout << A[i].suit << " " << A[i].value << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4884
final_rank: 14

code_snippet: #include<iostream>
#include<vector>
using namespace std;

int partition(vector<int> &A,vector<char>& v,int p,int r){
    int i = p-1;
    for(int j=p;j<r;j++){
        if(A[j]<=A[r]){
            i++;
            swap(A[i],A[j]);
            swap(v[i],v[j]);
        }
    }
    swap(A[i+1],A[r]);
    swap(v[i+1],v[r]);
    return i+1;
}
void quicksort(vector<int> &A,vector<char> &v,int p,int r){
    if(p<r){
        int q = partition(A,v,p,r);
        quicksort(A,v,p,q-1);
        quicksort(A,v,q+1,r);
    }
}
bool isStable(vector<int> &A,vector<int> &A1,vector<char> &v,vector<char> &v1){
    int N=A.size();
    for(int i=0;i<N;i++){
        for(int j=0;j<A1.size();j++){
            if(A[i]==A1[j]){
                if(v[i]==v1[j]){
                    v1.erase(v1.begin()+j);
                    A1.erase(A1.begin()+j);
                    break;
                }else{
                    return false;
                }
            }
            }
        }
        return true;
    }

int main(){
    int N,tmp;
    char tmpc;
    vector<int> A;
    vector<int> A1;
    vector<char> v;
    vector<char> v1;
    cin>>N;
    for (int i=0;i<N;i++){
        cin>>tmpc;
        v.push_back(tmpc);
        v1.push_back(tmpc);
        cin>>tmp;
        A.push_back(tmp);
        A1.push_back(tmp);
        
    }
    quicksort(A,v,0,N-1);


    if(isStable(A,A1,v,v1)){
        cout<<"Stable"<<endl;
    }else{
        cout<<"Not stable"<<endl;
    }
    
    for(int k=0;k<N;k++){
        cout<< v[k]<<" "<<A[k]<<endl;
    }
}
accuracy: 1
time_ms: 80
memory_kb: 4096
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& A, vector<char>& B,vector<int>& C, int p, int r){
    int x = A[r];
    int i = p - 1;
    for(int j = p; j <= r - 1; j++){
        if(A[j] <= x){
            i = i + 1;
            int q1 = A[j];
            char o1 = B[j];
            int s1 = C[j];
            A[j] = A[i];
            A[i] = q1;
            B[j] = B[i];
            B[i] = o1;
            C[j] = C[i];
            C[i] = s1;
        }
    }
    int q2 = A[i + 1];
    char o2 = B[i + 1];
    int s2 = C[i + 1];
    A[i + 1] = A[r];
    A[r] = q2;
    B[i + 1] = B[r];
    B[r] = o2;
    C[i + 1] = C[r];
    C[r] = s2;

    return i + 1;
}

void quickSort(vector<int>& A,vector<char>& B,vector<int>& C,int p, int r){
    if(p < r){
        int q = partition(A, B, C, p, r);
        quickSort(A, B, C, p, q - 1);
        quickSort(A, B, C, q + 1, r);
    }
}


int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    vector<char> B(n);
    vector<int> memo(n);
    for(int i = 0; i < n; i++){
        cin >> B[i] >> A[i];
        memo[i] = i;
    }

    quickSort(A,B, memo, 0, n - 1);

    int Stability = 0;

    for(int i = 0; i < n - 1; i++){
        if((A[i] == A[i + 1]) && (memo[i] > memo[i + 1])){
            Stability = Stability - 1;
        }
    }

    if(Stability >= 0){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }

    for(int i = 0; i < n; i++){
        cout << B[i] << " " << A[i] << endl;
    }

    return 0;

}
accuracy: 1
time_ms: 80
memory_kb: 3820
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#include "atcoder/dsu.hpp"
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
using ll= long long;
using ull=unsigned long long;
using ldo =long double;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_sum(_1, _2, _3,_4, name, ...) name
template<typename T>ll sum1(T &a){ll ans = 0;for(ll i=0;i<a.size();i++)ans+=a[i];return ans;}
template<typename T>ll sum2(T &a,ll start){ll ans = 0;for(ll i=start;i<a.size();i++)ans+=a[i];return ans;}
template<typename T>ll sum3(T &a,ll start,ll en){ll ans = 0;for(ll i=start;i<en;i++)ans+=a[i];return ans;}
template<typename T>ll sum4(T &a,ll start,ll en,ll tolerance){ll ans = 0;for(ll i=start;i<en;i+=tolerance)ans+=a[i];return ans;}
#define sum(...) OVERLOAD_sum(__VA_ARGS__,sum4, sum3, sum2, sum1)(__VA_ARGS__)
#define OVERLOAD_rep(_1, _2, _3,_4, name, ...) name
#define rep(i,n,k) for(ll i = k; i < (ll)(n); i++)

int  partition(vector<pair<char,int>> &A, int p, int r){
   int x = A[r].second;
   int i = p-1;
   for(int j = p; j<r; j++){
     if(A[j].second <= x){
       i = i+1;
       swap(A[i],A[j]);
     }
   }
   swap(A[i+1],A[r]);
   return i+1;
 }
 
void quicksort(vector<pair<char,int>> &A,int p,int r){
  if (p < r) {
    int q=partition(A,p,r);
    quicksort(A,p,q-1);
    quicksort(A,q+1,r);
  }
}
 
int main(){
  int n;
  cin>>n;
  vector<pair<char,int>>A(n);
  map<int,vector<char>> al;
  for(int i=0;i<n;i++){
    char a;
    int b;
    cin>>a>>b;
    al[b].push_back(a);
    A[i]=make_pair(a,b);
  }
  quicksort(A,0,n-1);
  map<int,vector<char>>al_a;
  for(int i=0;i<n;i++)al_a[A[i].second].push_back(A[i].first);
  bool ch=true;
  for(auto it = al.begin(); it != al.end(); ++it) {
      int key = it->first;
      vector<char> old_vec = it->second;
      vector<char> new_vec = al_a[key];
      for(int j = 0;j<al_a[key].size();j++){
        if(old_vec[j] != new_vec[j]){
          ch = false;
          break;
        }
      }
      if(!ch)break;
  }
  if(ch)cout<<"Stable"<<endl;
  else cout<<"Not stable"<<endl;
  
  for(int i=0;i<n;i++)cout<<A[i].first<<" "<<A[i].second<<endl;
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9288
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <tuple>
using namespace std;

int partition(vector<pair<string, int>>& A, int p, int r) {
    int x = A[r].second;
    int i = p-1;
    for (int j = p; j < r; j++) {
        if (A[j].second <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    
    return i+1;
}

void quicksort(vector<pair<string, int>>& A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quicksort(A, p, q-1);
        quicksort(A, q+1, r);
    }
}

bool comparePairs(const pair<string, int>& a, const pair<string, int>& b) {
    return a.second < b.second;
}

int main() {
    int n;
    cin >> n;
    vector<pair<string, int>> card_quick;

    for (int i = 0; i < n; i++) {
        string a;
        int num;
        cin >> a >> num;
        card_quick.push_back(make_pair(a, num));
    }

    vector<pair<string, int>> card_stable = card_quick;
    quicksort(card_quick, 0, n-1);
    stable_sort(card_stable.begin(), card_stable.end(), comparePairs);
    
    bool is_stable = true;
    for (int i = 0; i < n; i++) {
        if (card_quick[i] != card_stable[i]) {
            is_stable = false;
            break;
        }
    }

    if (is_stable) {
        cout << "Stable" << endl;
    } else {
        cout << "Not stable" << endl;
    }

    for (int i = 0; i < n-1; i++) {
        string a;
        int num;
        tie(a, num) = card_quick.at(i);
        cout << card_quick[i].first << " " << card_quick[i].second << endl;
    }

    cout << card_quick[n-1].first << " " << card_quick[n-1].second << endl;

    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 16120
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Card{
    char value;
    int num;
};

int partition(vector<Card>& A, int p, int r){
    int x =A[r].num;
    int i = p-1;
    
    for (int j =p; j<= r-1; ++j){
        if (A[j].num <= x){
            ++i;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

void quickSort(vector<Card>& A, int p, int r){
    if(p<r){
        int q = partition(A, p, r);
        quickSort(A, p, q-1);
        quickSort(A, q+1, r);
    }
}

bool isStable(vector<Card>& Sort, vector<Card>& Stable){
    int a = Sort.size();
    for (int i= 0; i<a ; ++i){
        if (Sort[i].value != Stable[i].value){
            return false;
        }
    }
    return true;
}

int main(){
    int n;
    cin >> n;
    vector<Card> S(n), S_c(n);
    
    for (int i = 0; i<n; i++){
        cin >> S[i].value >> S[i].num;
        S_c[i] = S[i];
    }
    
    quickSort(S, 0, n-1);
    
    stable_sort(S_c.begin(), S_c.end(), [](const Card& a, const Card& b) {
        return a.num < b.num;
    });
    
    if (isStable(S, S_c)) {
        cout << "Stable" << endl;
    } else {
        cout << "Not stable" << endl;
    }
    
    for (int i = 0; i<n ; ++i){
        cout << S[i].value << " " << S[i].num << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 4784
final_rank: 10