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

#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;

int sort(int a[], int n);

int main(void) {
  int n, a[100];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int num = sort(a, n);

  for (int i = 0; i < n; i++) {
    cout << a[i];
    if (i < n - 1)
      cout << " ";
    else
      cout << endl;
  }
  cout << num << endl;

  return 0;
}

int sort(int a[], int n) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        cnt++;
      }
    }
  }

  return cnt;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;

int main(void) {
  int n, a[100];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; i--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        cnt++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << a[i];
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
  cout << cnt << endl;

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

#include <iostream>
using namespace std;
int bubbleSort(int p[], int n) {
  int flag = 1;
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!flag)
      break;
    flag = 0;
    for (int j = n - 1; j > i; j--)
      if (p[j] < p[j - 1]) {
        flag = 1;
        int t = p[j];
        p[j] = p[j - 1];
        p[j - 1] = t;
        count++;
      }
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  int m = bubbleSort(p, n);
  for (int i = 0; i < n - 1; i++)
    cout << p[i] << ' ';
  cout << p[n - 1] << endl;
  cout << m << endl;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int bubbleSort(int p[], int n) {
  int flag = 1;
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!flag)
      break;
    flag = 0;
    for (int j = n - 1; j > i; j++)
      if (p[j] < p[j - 1]) {
        flag = 1;
        int t = p[j];
        p[j] = p[j - 1];
        p[j - 1] = t;
        count++;
      }
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  int m = bubbleSort(p, n);
  for (int i = 0; i < n - 1; i++)
    cout << p[i] << ' ';
  cout << p[n - 1] << endl;
  cout << m << endl;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

const int MAX = 101;
int n;
int a[MAX];
int b[MAX];

int ID = 1;
map<int, int> mp;
int toI(int num) {
  if (!mp.count(num)) {
    return mp[num] = ID++;
  }
  return mp[num];
}

int bit[MAX];

void add(int i, int x) {
  while (i < ID) {
    bit[i] += x;
    i += i & (-i);
  }
}

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  }
  return s;
}

int main() {
  scanf("%d", &n);
  memset(bit, 0, sizeof(bit));
  int ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    toI(a[i]);
  }
  for (int i = 0; i < n; i++) {
    ans += sum(ID - 1) - sum(toI(b[i]));
    add(toI(b[i]), 1);
  }
  for (int i = 0; i < n; i++) {
    if (i > 0)
      printf(" ");
    printf("%d", a[i]);
  }
  printf("\\n");
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX = 101;
const int MAXN = 100000;
int n;
int a[MAX];

int bit[MAXN];

void add(int i, int x) {
  while (i < MAXN) {
    bit[i] += x;
    i += i & (-i);
  }
}

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  }
  return s;
}

int main() {
  scanf("%d", &n);
  memset(bit, 0, sizeof(bit));
  int ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    ans += sum(MAXN - 1) - sum(a[i] - 1);
    add(a[i], 1);
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    if (i > 0)
      printf(" ");
    printf("%d", a[i]);
  }
  printf("\\n");
  printf("%d\\n", ans);
  return 0;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 128
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int INF = 1e9;

int n, t, dat[MAX + 1], a[100000];
void add(int k, int x) {
  if (!k)
    t++;
  else {
    while (k <= n) {
      dat[k] += x;
      k += k & -k;
    }
  }
}

int sum(int k) {
  int ans = 0;
  while (k > 0) {
    ans += dat[k];
    k -= k & -k;
  }
  return ans + t;
}

int main() {
  int m, x, ans = 0;
  scanf("%d", &m);
  n = MAX;
  t = 0;
  rep(i, m) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  sort(a, a + m);
  rep(i, m - 1) printf("%d ", a[i]);
  printf("%d\\n", a[m - 1]);
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 128
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int INF = 1e9;

int n, dat[MAX + 1], a[100000];
void add(int k, int x) {
  while (k <= n) {
    dat[k] += x;
    k += k & -k;
  }
}

int sum(int k) {
  int ans = 0;
  while (k > 0) {
    ans += dat[k];
    k -= k & -k;
  }
  return ans;
}

int main() {
  int m, x, ans = 0;
  scanf("%d", &m);
  n = MAX;
  rep(i, m) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  sort(a, a + m);
  rep(i, m - 1) printf("%d ", a[i]);
  printf("%d\\n", a[m - 1]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

// bubble sort
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, d, i1, i2, times;
  cin >> n;
  int a[n];

  for (i1 = 0; i1 < n; i1++) {
    cin >> a[i1];
  }
  times = 0;
  i2 = n;
  for (i2 = n; i2 > 0; i2--) {
    i1 = n;
    while (1) {
      i1--;
      if (a[i1] < a[i1 - 1]) {
        d = a[i1];
        a[i1] = a[i1 - 1];
        a[i1 - 1] = d;
        times++;
      }
      if (i1 <= n - i2) {
        break;
      }
    }
  }

  i1 = 0;
  // cout
  while (1) {
    if (i1 == 0) {
      cout << a[i1];
      if (n == 1) {
        break;
      } else {
        i1++;
        continue;
      }
    }
    cout << " " << a[i1];
    i1++;
    if (i1 == n) {
      break;
    }
  }
  cout << endl << times << endl;

  return 0;
}

Error: TLE

Fixed submission:

// bubble sort
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, d, i1, i2, times;
  cin >> n;
  int a[n];

  for (i1 = 0; i1 < n; i1++) {
    cin >> a[i1];
  }
  times = 0;
  i2 = n;
  for (i2 = n; i2 > 0; i2--) {
    i1 = n;
    while (1) {
      i1--;
      if (a[i1] < a[i1 - 1]) {
        d = a[i1];
        a[i1] = a[i1 - 1];
        a[i1 - 1] = d;
        times++;
      }
      if (i1 <= n - i2) {
        break;
      }
    }
  }

  i1 = 0;
  // cout
  while (1) {
    if (i1 == 0) {
      cout << a[i1];
      continue;
    }
    cout << " " << a[i1];
    i1++;
    if (i1 == n) {
      break;
    }
  }
  cout << endl << times << endl;

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

//
//  main.cpp
//  ajo
//
//  Created by Hashimoto Ryo on 2015/06/20.
//  Copyright (c) 2015??´ Hashimoto Ryo. All rights reserved.
//

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 100;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main() {

  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;
  return 0;
}

//
// int main(){
//
//    int n;
//    int a[MAX];
//    int count = 0;
//
//    //?????¬????????\\???
//    cin >> n;
//    for(int i = 0;i < n; i++){
//        cin >> a[i];
//    }
//
//    //?????¬???????????????
//    for(int i = 0;i < n;i++){
//
//        int j = n - 1;//?????????????????????????????????
//        while(j > i){
//            if(a[j] < a[j-1]){
//                int temp = a[j];
//                a[j+1] = temp;
//                a[j] = a[j+1];
//                count++;
//            }
//            j--;
//        }
//    }
//
//    for(int i = 0;i < n-1;i++){
//        cout << a[i] << " ";
//    }
//    cout << a[n-1] << endl;
//    cout << count << endl;
//
//    return 0;
//}

Error: TLE

Fixed submission:

//
//  main.cpp
//  ajo
//
//  Created by Hashimoto Ryo on 2015/06/20.
//  Copyright (c) 2015??´ Hashimoto Ryo. All rights reserved.
//

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 100;

int main() {

  int n;
  int a[MAX];
  int count = 0;

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

  for (int i = 0; i < n; i++) {
    int j = n - 1;
    while (j > i) {
      if (a[j] < a[j - 1]) {
        int temp = a[j];
        a[j + 1] = temp;
        a[j] = a[j + 1];
        count++;
      }
    }
  }

  for (int i = 0; i < n - 1; i++) {
    cout << a[i];
  }
  cout << a[n - 1] << endl;
  cout << count << endl;

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
using namespace std;

//-----------------------------------------------------------------------

int N, bit[110];

int add(int k, int x) {
  for (; k <= N; k += k & -k)
    bit[k] += x;
}
int getSum(int k) {
  int ret = 0;
  for (; k > 0; k -= k & -k)
    ret += bit[k];
  return ret;
}

signed main() {
  cin >> N;
  vector<int> a(N);
  REP(i, N) { cin >> a[i]; }

  vector<int> b, c;
  b = c = a;
  sort(ALL(b));
  b.erase(unique(ALL(b)), b.end());
  REP(i, N) { c[i] = lower_bound(ALL(b), c[i]) - b.begin() + 1; }

  int ans = 0;
  REP(i, N) {
    ans += i - getSum(c[i]);
    add(c[i], 1);
  }

  sort(ALL(a));
  REP(i, N) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
using namespace std;

//-----------------------------------------------------------------------

int N, bit[110];

int add(int k, int x) {
  for (; k <= N; k += k & -k)
    bit[k] += x;
}
int getSum(int k) {
  int ret = 0;
  for (; k > 0; k -= k & -k)
    ret += bit[k];
  return ret;
}

signed main() {
  cin >> N;
  vector<int> a(N);
  REP(i, N) { cin >> a[i]; }

  int ans = 0;
  REP(i, N) {
    ans += i - getSum(a[i]);
    add(a[i], 1);
  }

  sort(ALL(a));
  REP(i, N) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl;
  cout << ans << endl;
}
Problem p02259 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main(void) {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j++) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main(void) {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int cnt = 0;
void trace(int a[], int n) {
  for (int i = 0; i < n - 1; ++i)
    printf("%d ", a[i]);
  printf("%d\\n", a[n - 1]);
}

void insertionSort(int a[], int n) {
  int v, j;
  for (int i = 1; i < n; ++i) {
    v = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    trace(a, n);
  }
}

void bubbleSort(int a[], int n) {
  int flag, i;
  flag = 1;
  while (flag) {
    flag = 0;
    for (int j = n - 1; j >= i + 1; --j) {
      int tmp;
      if (a[j] < a[j - 1]) {
        tmp = a[j];
        a[j] = a[j - 1];
        a[j - 1] = tmp;
        ++cnt;
        flag = 1;
      }
    }
    ++i;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int a[maxn];
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  bubbleSort(a, n);
  for (int i = 0; i < n - 1; ++i)
    printf("%d ", a[i]);
  printf("%d\\n", a[n - 1]);
  printf("%d\\n", cnt);
  return 0;
}


Error: -11

Fixed submission:

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

typedef vector<int> vi;
typedef set<char> setchar;
typedef set<int> sett;
typedef long long ll;
#define vec(a) a.begin(), a.end()
#define pb push_back
void init() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
}

int main() {
  init();
  freopen("input.txt", "r", stdin);
  int t, n, k;

  cin >> n;
  int arr[n], c = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (arr[j] < arr[j - 1]) {
        int temp = arr[j];
        arr[j] = arr[j - 1];
        arr[j - 1] = temp;
        c++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << arr[i];
    if (i == n - 1)
      continue;
    else
      cout << " ";
  }
  cout << endl;
  cout << c << endl;

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

#include <iostream>
using namespace std;

int A[100], n;

int bubbleSort(int A[], int n) {
  int s = 0;
  int flag;
  flag = 1;
  while (flag) {
    flag = 0;
    for (int j = n - 1; j >= 1; j--) {
      if (A[j] < A[j - 1]) {
        s++;
        swap(A[j], A[j - 1]);
        flag = 1;
      }
    }
  }

  return s;
}

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

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

  ans = bubbleSort(A, n);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << A[i];
  }
  cout << endl;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

// "Bubble Sort"  ??????????????????
#include <iostream>
using namespace std;

inline int bubbleSort(int A[], int n);

int main() {
  int n, A[100], sw;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  sw = bubbleSort(A, n);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;

  return 0;
}

int bubbleSort(int A[], int n) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = n - 1; j >= i + 1; i--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}
Problem p02259 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, a[100], ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        ans++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << a[i] << "\\n";
    else
      cout << a[i] << " ";
  }
  cout << ans << "\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, a[100], ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
      ans++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << a[i] << "\\n";
    else
      cout << a[i] << " ";
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

int n;
int a[101];

int calc() {
  int ret = 0;
  rep(i, n - 1) {
    rep(j, n - 1) {
      if (a[j] > a[j + 1])
        ++ret, std::swap(a[j], a[j + 1]);
    }
  }
  return ret;
}

int main() {
  std::cin >> n;
  rep(i, n) std::cin >> a[i];
  int ans = calc();
  std::cout << a[0];
  REP(i, 1, n) std::cout << " " << a[i];
  std::cout << std::endl;
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

const int maxN = 200001;

// 1:indexed
class Bit {
public:
  int n;
  int dat[maxN + 1];

  void init(int x) { n = x; }

  int sum(int k) {
    int ret = 0;
    while (k > 0) {
      ret += dat[k];
      k -= k & -k;
    }
    return ret;
  }

  void add(int k, int x) {
    while (k <= n) {
      dat[k] += x;
      k += k & -k;
    }
  }
};

int n;
int a[101];
Bit b;

int main() {
  std::cin >> n;
  b.init(n);
  rep(i, n) std::cin >> a[i];
  int ans = 0;
  rep(i, n) ans += i - b.sum(a[i]), b.add(a[i], 1);
  std::sort(a, a + n);
  std::cout << a[0];
  REP(i, 1, n) std::cout << " " << a[i];
  std::cout << std::endl;
  std::cout << ans << std::endl;

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

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int kaisu = 0;
  int l[100];
  int n, tmp;
  int d = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  if (n == 1) {
    cout << l[0] << endl << 0 << endl;
  } else {
    for (;;) {
      for (int i = n - 1; i > 0; i--) {
        if (l[i] < l[i - 1]) {
          tmp = l[i];
          l[i] = l[i - 1];
          l[i - 1] = tmp;
          kaisu += 1;
        }
      }
      d += 1;
      if (d == n - 1) {
        break;
      }
    }
    sort(l, l + n);
    for (int i = 0; i < n; i++) {
      cout << l[i];
      if (i == n - 1) {
        cout << endl;
      } else
        cout << " ";
    }
    cout << kaisu << endl;
  }
  char c;
  cin >> c;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int kaisu = 0;
  int l[100];
  int n, tmp;
  int d = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  for (;;) {
    for (int i = n - 1; i > 0; i--) {
      if (l[i] < l[i - 1]) {
        tmp = l[i];
        l[i] = l[i - 1];
        l[i - 1] = tmp;
        kaisu += 1;
      }
    }
    d += 1;
    if (d == n - 1) {
      break;
    }
  }
  sort(l, l + n);
  for (int i = 0; i < n; i++) {
    cout << l[i];
    if (i == n - 1) {
      cout << endl;
    } else
      cout << " ";
  }
  cout << kaisu << endl;
  char c;
  cin >> c;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n, a) for (int i = a; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n];
  rep(i, n) scanf("%d", &a[i]);
  int flag = 1;
  while (flag) {
    flag = 0;
    for (j = n - 1; j > 0; --j) {
      if (a[j] < a[j - 1]) {
        int t;
        t = a[j], a[j] = a[j - 1], a[j - 1] = t;
        flag = 1;
        ++ans;
      }
    }
  }
  rep(i, n) {
    if (i)
      printf(" %d", a[i]);
    else
      printf("%d", a[i]);
  }
  printf("\\n%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n, a) for (int i = a; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n];
  rep(i, n) scanf("%d", &a[i]);
  int flag = 1;
  while (flag)
    for (j = n - 1; j > 0; --j)
      if (a[j] < a[j - 1]) {
        int t;
        t = a[j], a[j] = a[j - 1], a[j - 1] = t;
        flag = 1;
        ++ans;
      }
  rep(i, n) {
    if (i)
      printf(" %d", a[i]);
    else
      printf("%d", a[i]);
  }
  printf("\\n%d\\n", ans);
  return 0;
}
Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

int main() {

  int n;
  cin >> n;

  int A[2000];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (A[j] > A[j + 1]) {
        ans++;
        swap(A[j], A[j + 1]);
      }
    }
  }

  sort(A, A + n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

int N = 10000 - 1; // 必要な数よりも大きい2^nよりも大きい数を入れるとよい
int BIT[10000];

void add(int a, int w) {

  // a番目の要素にwを足す O(log N)

  for (int x = a; x <= N; x += x & -x) {
    BIT[x] += w;
  }
}

int sum(int a) {

  // 1からaまでの要素の合計を返す O(log N)

  int ret = 0;
  for (int x = a; x > 0; x -= x & -x) {
    ret += BIT[x];
  }
  return ret;
}

void init() {
  for (int i = 0; i < N; i++) {
    BIT[i] = 0;
  }
}

int n;
int A[10000];

// バブルソートの交換回数、ind[i] が交換後のインデックス
long long int solve() {
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += i - sum(A[i]);
    add(A[i], 1);
  }
  return ans;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  init();
  int ans = solve();

  sort(A, A + n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;

  cout << ans << endl;

  return 0;
}