Problem p02259 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// Print each element of a container.
template <typename Container> void print(const Container &container) {
  stringstream sstream;

  string delim("");
  for (const auto &element : container) {
    sstream << delim << element;
    delim = " ";
  }

  cout << sstream.str() << endl;
}

template <typename Container> int bubbleSort(Container &container) {
  int count(0);

  for (auto it(begin(container)); it != end(container); ++it) {
    for (auto jt(prev(end(container))); jt != it; --jt) {
      if (*jt < *prev(jt)) {
        iter_swap(jt, prev(jt));
        ++count;
      }
    }
  }

  return count;
}

int main() {
  vector<int> v;

  int n;
  cin >> n;

  for (size_t i(0); i < n; ++i) {
    int x;
    cin >> x;

    v.push_back(x);
  }

  int count(bubbleSort(v));

  print(v);

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define DEBUG

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Apply a function to each element of a tuple.
template <typename Tuple, typename Function, std::size_t... Indices>
void apply(Tuple &&tuple, Function &&function,
           std::index_sequence<Indices...>) {
  // Don't unpack in function parameters because it's not guaranteed each
  // parameter is evaluated from left to right.
  // [](auto&&...)
  // {}((static_cast<void>(std::forward<Function>(function)(std::get<Indices>(std::forward<Tuple>(tuple)))),
  // 0)...);

  // So, unpack in initializer list.
  static_cast<void>(std::initializer_list<int>{
      (static_cast<void>(std::forward<Function>(function)(
           std::get<Indices>(std::forward<Tuple>(tuple)))),
       0)...});
}

// Apply a function to each element of a tuple. (Helper method)
template <typename Tuple, typename Function>
void apply(Tuple &&tuple, Function &&function) {
  constexpr std::size_t size(
      std::tuple_size<typename std::remove_reference<Tuple>::type>::value);
  apply(std::forward<Tuple>(tuple), std::forward<Function>(function),
        std::make_index_sequence<size>());
}

// Overload operator<< for tuple.
template <typename... Elements>
std::ostream &operator<<(std::ostream &os,
                         const std::tuple<Elements...> &tuple) {
  os << "( ";

  apply(tuple, [&os](const auto &element) { os << element << " "; });

  os << ")";

  return os;
}

// Overload operator<< for pair.
template <typename First, typename Second>
std::ostream &operator<<(std::ostream &os,
                         const std::pair<First, Second> &pair) {
  os << "( " << pair.first << " " << pair.second << " )";

  return os;
}

// Print each element of a container.
template <typename Container>
void print(const Container &container, const std::string &info = "") {
#ifdef DEBUG

  for (auto it(begin(container)); it != end(container); ++it) {
    cout << *it;

    if (it != prev(end(container)))
      cout << " ";
  }

  cout << endl;

#endif
}

int main() {
  auto bubbleSort([](auto &container) {
    bool flag(true);
    int count(0);

    while (flag) {
      flag = false;

      for (auto it(rbegin(container)); it != prev(rend(container)); ++it) {
        if (*it < *next(it)) {
          iter_swap(it, next(it));
          ++count;

          flag = true;
        }
      }
    }

    print(container);
    cout << count << endl;
  });

  vector<int> v;

  int len;
  cin >> len;

  for (size_t i(0); i < len; ++i) {
    int n;
    cin >> n;

    v.push_back(n);
  }

  bubbleSort(v);

  while (true) {
  }

  return 0;
}