alexjercan/bugnet huggingface dataset:
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
struct Card {
  char name;
  int number;
};
Card a[100005], b[100005], tmp[100005];
int n;
void merge(Card a[], int l, int m, int r, Card tmp[]) {
  int p1 = l, p2 = m + 1, p3 = 0;
  while (p1 <= m && p2 <= r) {
    if (a[p1].number <= a[p2].number)
      tmp[p3++] = a[p1++];
    else
      tmp[p3++] = a[p2++];
  }
  while (p1 <= m)
    tmp[p3++] = a[p1++];
  while (p2 <= r)
    tmp[p3++] = a[p2++];
  for (int i = 0; i <= r - l; i++)
    a[i + l] = tmp[i];
}
void mergesort(Card a[], int l, int r, Card tmp[]) {
  if (l < r) {
    int m = l + (r - l) / 2;
    mergesort(a, l, m, tmp);
    mergesort(a, m + 1, r, tmp);
    merge(a, l, m, r, tmp);
  }
}
int partition(Card a[], int p, int r) {
  Card x = a[r], t;
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (a[j].number <= x.number) {
      i++;
      t = a[j];
      a[j] = a[i];
      a[i] = t;
    }
  }
  t = a[i + 1];
  a[i + 1] = a[r];
  a[r] = t;
  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 i, stable = 1;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].name >> a[i].number;
    b[i].name = a[i].name;
    b[i].number = a[i].number;
  }
  mergesort(b, 0, n - 1, tmp);
  quicksort(a, 0, n - 1);
  for (i = 0; i < n; i++) {
    if (a[i].name != b[i].name) {
      stable = 0;
      break;
    }
  }
  if (stable == 1)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (i = 0; i < n; i++) {
    cout << a[i].name << " " << a[i].number << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

using namespace std;

int A[500000], C[500000], pos1, pos2, sz;

long long countInversions(int a, int b) {
  if (a == b)
    return 0;

  int c = ((a + b) >> 1);
  long long aux = countInversions(a, c) + countInversions(c + 1, b);

  pos1 = a;
  pos2 = c + 1;
  sz = 0;

  while (pos1 <= c && pos2 <= b) {
    if (A[pos1] < A[pos2])
      C[sz] = A[pos1++];
    else {
      C[sz] = A[pos2++];
      aux += c - pos1 + 1;
    }
    ++sz;
  }

  if (pos1 > c)
    for (; pos2 <= b; ++pos2, ++sz)
      C[sz] = A[pos2];
  else
    for (; pos1 <= c; ++pos1, ++sz)
      C[sz] = A[pos1];

  for (int i = a; i <= b; ++i)
    A[i] = C[i - a];

  return aux;
}

int main() {
  int N;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    for (int i = 0; i < N; ++i)
      scanf("%d", &A[i]);

    printf("%lld\\n", countInversions(0, N - 1));
  }

  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Runtime Error

// #define		FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

#include <deque>
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

#define MAX 100000
struct SN {
  char S;
  int N;
};

SN A[MAX];

void swap(int a, int b) {
  SN temp(A[a]);

  A[a] = A[b];
  A[b] = temp;
}

int Partition(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(i, j);
    }
  }

  swap(i + 1, r);

  return (i + 1);
}

void QuickSort(int p, int r) {
  if (p < r) {
    int q = Partition(p, r);
    QuickSort(p, q - 1);
    QuickSort(q + 1, r);
  }
}

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\\Temp\\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  int n;
  cin >> n;
  unordered_map<int, string> um;
  unordered_map<int, string> um2;
  deque<int> dq;

  for (int i = 0; i < n; ++i) {
    cin >> A[i].S >> A[i].N;
    cin.clear();
    cin.ignore();

    um[A[i].N] += A[i].S;
  }

  QuickSort(0, n - 1);

  for (int i = 0; i < n; ++i) {
    um2[A[i].N] += A[i].S;
  }

  bool isStable = true;
  for (auto it : um) {
    if (it.second != um2[it.first]) {
      isStable = false;
      break;
    }
  }

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

  for (int i = 0; i < n; i++) {
    cout << A[i].S << " " << A[i].N << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#undef FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

#include <deque>
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

#define MAX 100000

struct SN {
  char S;
  int N;
};

SN A[MAX];
string str[MAX];
string str2[MAX];

void swap(int a, int b) {
  SN temp(A[a]);

  A[a] = A[b];
  A[b] = temp;
}

int Partition(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(i, j);
    }
  }

  swap(i + 1, r);

  return (i + 1);
}

void QuickSort(int p, int r) {
  if (p < r) {
    int q = Partition(p, r);
    QuickSort(p, q - 1);
    QuickSort(q + 1, r);
  }
}

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\\Temp\\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  int n;
  cin >> n;
  unordered_set<int> us;
  deque<int> dq;

  for (int i = 0; i < n; ++i) {
    cin >> A[i].S >> A[i].N;
    cin.clear();
    cin.ignore();
    str[A[i].N] += A[i].S;

    if (!us.insert(A[i].N).second) {
      dq.push_back(A[i].N);
    }
  }

  QuickSort(0, n - 1);

  for (int i = 0; i < n; ++i) {
    str2[A[i].N] += A[i].S;
  }

  bool isStable = true;

  for (auto it : dq) {
    if (str[it] != str2[it]) {
      isStable = false;
      break;
    }
  }

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

  for (int i = 0; i < n; i++) {
    cout << A[i].S << " " << A[i].N << "\\n";
  }

  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 100000
#define SENTINEL 2000000000

struct h {
  char t;
  int m;
};
h A[100005], L[100005], R[100005];
int partition(h A[], int p, int r) {
  int x = A[r].m;
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j].m <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quickSort(h 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(h A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  int i = 0, j = 0;
  L[n1].m = R[n2].m = SENTINEL;
  for (int k = left; k < right; k++) {
    if (L[i].m <= R[j].m) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}
void mergeSort(h A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}
int main() {
  int n;
  cin >> n;
  h c1[100005], c2[100005];
  for (int i = 0; i < n; i++) {
    cin >> c1[i].t >> c1[i].m;
    c2[i] = c1[i];
  }
  mergeSort(c1, n, 0, n);
  quickSort(c2, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (c1[i].t != c2[i].t) {
      cout << "Not stable" << endl;
      break;
    }
    if (i == n - 1)
      cout << "Stable" << endl;
  }
  for (int i = 0; i < n; i++)
    cout << c2[i].t << " " << c2[i].m << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

struct h {
  char t;
  int m;
};
h A[100005];
int partition(h A[], int p, int r) {
  int x = A[r].m;
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j].m <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quickSort(h 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 bubble(h A[], int n) {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (A[j].m < A[j - 1].m) {
        h t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  h c1[100005], c2[100005];
  for (int i = 0; i < n; i++) {
    cin >> c1[i].t >> c1[i].m;
    c2[i] = c1[i];
  }
  bubble(c1, n);
  quickSort(c2, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (c1[i].t != c2[i].t) {
      cout << "Not stable" << endl;
      break;
    }
    if (i == n - 1)
      cout << "Stable" << endl;
  }
  for (int i = 0; i < n; i++)
    cout << c2[i].t << " " << c2[i].m << endl;
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define AMAX 100001

struct card {
  char mark;
  int value;
  int cnt;
};

card a[AMAX];
int partion(int q, int r) {
  int x = a[r].value;
  int i = q - 1;
  for (int j = q; 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(int p, int r) {

  if (p < r) {
    int q;
    q = partion(p, r);
    quicksort(p, q - 1);
    quicksort(q + 1, r);
  }
}

int main() {

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf(" %c %d", &a[i].mark, &a[i].value);
    a[i].cnt = i;
  }

  quicksort(0, n - 1);
  int junge = 0;

  for (int i = 1; i < n; i++) {
    if (a[i - 1].value == a[i].value && a[i - 1].cnt > a[i].cnt)
      junge = 1;
  }
  if (junge == 1)
    printf("Not stable\\n");
  else
    printf("Stable\\n");

  for (int i = 0; i < n; i++) {
    printf("%c %d\\n", a[i].mark, a[i].value);
  }
}

Error: TLE

Fixed submission:

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

#define AMAX 100001

struct card {
  char mark;
  int value;
  int cnt;
};

card a[AMAX];
int partion(int q, int r) {
  int x = a[r].value;
  int i = q - 1;
  for (int j = q; 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(int p, int r) {

  if (p < r) {
    int q;
    q = partion(q, r);
    quicksort(p, q - 1);
    quicksort(q + 1, r);
  }
}

int main() {

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf(" %c %d", &a[i].mark, &a[i].value);
    a[i].cnt = i;
  }

  quicksort(0, n - 1);
  int junge = 0;

  for (int i = 1; i < n; i++) {
    if (a[i - 1].value == a[i].value && a[i - 1].cnt > a[i].cnt)
      junge = 1;
  }
  if (junge == 1)
    printf("Not stable\\n");
  else
    printf("Stable\\n");

  for (int i = 0; i < n; i++) {
    printf("%c %d\\n", a[i].mark, a[i].value);
  }
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 100000
#define INFTY 1000000001

using namespace std;

typedef struct {
  char suit;
  int number;
} Card;

Card A[MAX];
Card B[MAX];
int n;

void swapCard(int a, int b) {
  Card tmp = A[a];
  A[a] = A[b];
  A[b] = tmp;
}

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

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

int partition(int p, int r) {
  int x = A[r].number;
  int i = p - 1;
  int tmp;
  for (int j = p; j < r; j++) {
    if (A[j].number <= x) {
      i++;
      swapCard(i, j);
    }
  }
  swapCard(i + 1, r);
  return i + 1;
}

void quickSort(int p, int r) {
  if (p < r) {
    int q = partition(p, r);
    quickSort(p, q - 1);
    quickSort(q + 1, r);
  }
}

bool isStable() {
  for (int i = 0; i < n; i++) {
    if (A[i].suit != B[i].suit)
      return false;
  }
  return true;
}

int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> A[i].suit >> A[i].number;
    B[i] = A[i];
  }
  quickSort(0, n - 1);
  mergeSort(B, 0, n);
  if (isStable())
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (i = 0; i < n; i++) {
    cout << A[i].suit << " " << A[i].number << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define MAX 100000

using namespace std;

typedef struct {
  char suit;
  int number;
} Card;

Card A[MAX];
Card B[MAX];
int n;

void swapCard(int a, int b) {
  Card tmp = A[a];
  A[a] = A[b];
  A[b] = tmp;
}

void BubbleSort() {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (B[j].number < B[j - 1].number) {
        Card tmp = B[j];
        B[j] = B[j - 1];
        B[j - 1] = tmp;
      }
    }
  }
}

int partition(int p, int r) {
  int x = A[r].number;
  int i = p - 1;
  int tmp;
  for (int j = p; j < r; j++) {
    if (A[j].number <= x) {
      i++;
      swapCard(i, j);
    }
  }
  swapCard(i + 1, r);
  return i + 1;
}

void quickSort(int p, int r) {
  if (p < r) {
    int q = partition(p, r);
    quickSort(p, q - 1);
    quickSort(q + 1, r);
  }
}

bool isStable() {
  for (int i = 0; i < n; i++) {
    if (A[i].suit != B[i].suit)
      return false;
  }
  return true;
}

int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> A[i].suit >> A[i].number;
    B[i] = A[i];
  }
  quickSort(0, n - 1);
  BubbleSort();
  if (isStable())
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (i = 0; i < n; i++) {
    cout << A[i].suit << " " << A[i].number << endl;
  }
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

vector<tuple<int, char, int>> a;
bool stable = true;

int partition(int l, int r) {
  int pivot = get<0>(a[r]);
  int i = l - 1;
  for (int j = l; j < r; j++) {
    if (get<0>(a[j]) <= pivot) {
      i++;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

void q_sort(int l, int r) {
  if (l < r) {
    int q = partition(l, r);
    q_sort(l, q - 1);
    q_sort(q + 1, r);
  }
  return;
}

int main() {
  int n;
  int num;
  char c;
  cin >> n;
  REP(i, n) {
    cin >> c >> num;
    a.push_back(tie(num, c, i));
  }
  q_sort(0, n - 1);
  int i = 0;

  while (i < n) {
    int j = 1;
    while (i + j < n && get<0>(a[i]) == get<0>(a[i + j])) {
      if (get<2>(a[i + j]) < get<2>(a[i])) {
        stable = false;
        break;
      } else {
        j++;
        continue;
      }
    }
    i += j;
  }

  if (stable)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  REP(i, n) cout << get<1>(a[i]) << " " << get<0>(a[i]) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

vector<pair<int, char>> a(100002);
vector<pair<int, char>> ori(100002);

int find(vector<pair<int, char>> target, pair<int, char> query) {
  int i = 0;
  auto iter = target.begin();
  while (iter != target.end()) {
    if (*iter == query)
      break;
    i++;
    iter++;
  }

  return i;
}

int partition(int l, int r) {
  int pivot = a[r].first;
  int i = l - 1;
  for (int j = l; j < r; j++) {
    if (a[j].first <= pivot) {
      i++;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

void q_sort(int l, int r) {
  if (l < r) {
    int q = partition(l, r);
    q_sort(l, q - 1);
    q_sort(q + 1, r);
  }
  return;
}

int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i].second >> a[i].first;
  ori = a;
  q_sort(0, n - 1);
  bool stable = true;
  int i = 0;

  while (i < n) {
    int j = 1;
    while (i + j < n && a[i].first == a[i + j].first) {
      if (find(ori, a[i + j]) < find(ori, a[i])) {
        stable = false;
        break;
      } else {
        j++;
        continue;
      }
    }
    i += j;
  }

  if (stable)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  REP(i, n) cout << a[i].second << " " << a[i].first << endl;

  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
const int MAXN = 100007;
using namespace std;

struct data {
  int num;
  char pic;
  int ind;
};

void BubbleSort(data *B, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (B[j].num < B[j - 1].num) {
        swap(B[j], B[j - 1]);
      }
    }
  }
}

int partition(data 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(data A[], int p, int r) {
  if (p < r) {
    int q;
    q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int main() {
  int n;
  cin >> n;

  data A[MAXN];
  for (int i = 0; i < n; i++) {
    cin >> A[i].pic >> A[i].num;
    A[i].ind = i;
  }

  quicksort(A, 0, n - 1);

  bool flg = true;
  for (int i = 0; i < n; i++) {
    if (A[i].num == A[i + 1].num && A[i].ind > A[i + 1].ind) {
      flg = false;
      break;
    }
  }
  if (flg) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  for (int i = 0; i < n; i++)
    printf("%c %d\\n", A[i].pic, A[i].num);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
const int MAXN = 100007;
using namespace std;

struct data {
  int num;
  char pic;
};

/*void SelectionSort(data B[], int n){
    int mini;
    for(int i=0; i<n; i++){
       mini = i;
       for(int j=i; j<n; j++){
           if(B[j].num < B[mini].num){
               mini = j;
           }
       }
       swap(B[i], B[mini]);
    }
}*/

void BubbleSort(data *B, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (B[j].num < B[j - 1].num) {
        swap(B[j], B[j - 1]);
      }
    }
  }
}

int partition(data 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(data A[], int p, int r) {
  if (p < r) {
    int q;
    q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int main() {
  int n;
  cin >> n;

  data A[MAXN], B[MAXN];
  for (int i = 0; i < n; i++) {
    cin >> A[i].pic >> A[i].num;
    B[i] = A[i];
  }

  quicksort(A, 0, n - 1);
  BubbleSort(B, n);

  bool flg = true;
  for (int i = 0; i < n; i++) {
    if (A[i].pic != B[i].pic) {
      flg = false;
      break;
    }
  }
  if (flg) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  for (int i = 0; i < n; i++)
    printf("%c %d\\n", A[i].pic, A[i].num);
  return 0;
}
Problem p02277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

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

void quicksort(vector<pair<int, char>> &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 solve() {
  int n;
  bool stable = true;
  cin >> n;
  map<int, queue<char>> que;
  vector<int> j(12, 0);
  vector<pair<int, char>> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i].second >> A[i].first;
    que[A[i].first].push(A[i].second);
  }
  quicksort(A, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (que[A[i].first].front() != A[i].second)
      stable = false;
    else
      que[A[i].first].pop();
  }
  if (stable)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i].second << " " << A[i].first << endl;
  }
  // cout << endl;
}

int main() { solve(); }

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

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

void quicksort(vector<pair<int, char>> &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 solve() {
  int n;
  bool stable = true;
  cin >> n;
  map<int, vector<char>> mp;
  vector<int> j(12, 0);
  vector<pair<int, char>> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i].second >> A[i].first;
    mp[A[i].first].push_back(A[i].second);
  }
  quicksort(A, 0, n - 1);
  for (int i = 0; i < n; i++) {
    if (mp[A[i].first][j[A[i].first]++] != A[i].second)
      stable = false;
  }
  if (stable)
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i].second << " " << A[i].first << endl;
  }
  // cout << endl;
}

int main() { solve(); }
Problem p02278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define sf scanf
#define pf printf
#define show(x) cout << "x:" << x << endl;
using namespace std;
typedef long long ll;
const int maxn = 1000;
const int vmaxn = 10000;
const int inf = 1e9 + 5;
int n, a[maxn], s;
int b[maxn], t[vmaxn + 1];

ll solve() {
  int ans = 0;

  bool V[maxn];
  for (int i = 0; i < n; i++)
    b[i] = a[i], V[i] = false;
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    t[b[i]] = i;
  for (int i = 0; i < n; i++) {
    if (V[i])
      continue;
    int cur = i;
    int S = 0;
    int m = vmaxn;
    int an = 0;
    while (1) {
      V[cur] = true;
      an++;
      int v = a[cur];
      m = min(m, v);
      S += v;
      cur = t[v];
      if (V[cur])
        break;
    }
    ans += min(S + (an - 2) * m, m + S + (an + 1) * s);
  }

  return ans;
}

int main() {
  cin >> n;
  s = vmaxn;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s = min(s, a[i]);
  }
  int ans = solve();
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define sf scanf
#define pf printf
#define show(x) cout << "x:" << x << endl;
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 5;
ll a[maxn], p[maxn], n;
vector<int> v;
ll sum = 0;

void solve() {
  vector<int>::iterator it = v.begin();
  int t = *it;
  v.erase(v.begin());
  if (!v.empty())
    it = v.begin();
  while (!v.empty()) {
    int f = 0;
    int b = *it;
    if (b == p[t]) {
      if (t == p[b])
        f = 1;
      swap(a[p[t]], a[p[b]]);
      sum += t + *it + 2;
      v.erase(it);
      if (f == 1 && !v.empty())
        t = v[0], v.erase(v.begin());
      if (!v.empty())
        it = v.begin();
    } else {
      it++;
      if (it == v.end())
        it = v.begin();
    }
    // pf("--t= %d , *it = %d\\n",t,*it);
    // cout<<sum<<endl;
  }
}

int main() {
  sf("%d", &n);
  for (int i = 0; i < n; i++) {
    sf("%d", &a[i]);
    --a[i];
    p[a[i]] = i;
    if (a[i] != i)
      v.push_back(a[i]);
  }
  sort(v.begin(), v.end());

  solve();

  pf("%d\\n", sum);

  return 0;
}