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

//
//  4.cpp
//  Algorithms and Data Structures
//
//  Created by kawauso on 2018/04/06.
//

#include <cassert>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

void c_4_2();
void c_4_3();

#define MAIN() c_4_3()
#ifdef MAIN
int main(int argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  MAIN();
  return 0;
}
#else
#include "4.hpp"
#endif

void c_4_3() {
  int n;
  int q;
  int elapsed;

  std::cin >> n;
  std::cin >> q;
  std::queue<std::tuple<std::string, int>> queue;
  for (int i = 0; i < n; i++) {
    std::string name;
    int time;

    std::cin >> name;
    std::cin >> time;
    queue.push(std::make_tuple(name, time));
  }
  elapsed = 0;
  while (queue.size() > 0) {
    int min_qt;

    min_qt = 0;
    for (int i = 0; i < queue.size(); i++) {
      std::tuple<std::string, int> p;

      p = queue.front();
      queue.pop();
      if (std::get<1>(p) <= q) {
        // the process ends
        elapsed += std::get<1>(p);
        std::cout << std::get<0>(p) << " " << elapsed << std::endl;
        continue;
      }
      std::get<1>(p) -= q;
      queue.push(p);
      elapsed += q;
      if (min_qt == 0 || min_qt > std::get<1>(p)) {
        min_qt = std::get<1>(p);
      }
    }
  }
}

void c_4_2() {
  std::string line;
  std::string s;

  // input
  std::getline(std::cin, line);
  std::istringstream iss(line);
  std::vector<std::string> a;
  while (iss >> s) {
    if (s == "+") {
      const auto result = std::stoi(a[1]) + std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else if (s == "-") {
      const auto result = std::stoi(a[1]) - std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else if (s == "*") {
      const auto result = std::stoi(a[1]) * std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else {
      a.insert(a.begin(), s);
    }
  }
  std::cout << a[0] << std::endl;
}
// 正解との相違: 演算子は格納しなくてよいので配列の要素の型は整数でよい
// C++で実装するならstd::stackを使ってよいだろう


Error: TLE

Fixed submission:

//
//  4.cpp
//  Algorithms and Data Structures
//
//  Created by kawauso on 2018/04/06.
//

#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

void c_4_2();
void c_4_3();

#define MAIN() c_4_3()
#ifdef MAIN
int main(int argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  MAIN();
  return 0;
}
#else
#include "4.hpp"
#endif

void c_4_3() {
  int n;
  int q;
  int elapsed;

  std::cin >> n;
  std::cin >> q;
  std::vector<std::tuple<std::string, int>> queue(n);
  for (int i = 0; i < n; i++) {
    std::string name;
    int time;

    std::cin >> name;
    std::cin >> time;
    queue[i] = std::make_tuple(name, time);
  }
  elapsed = 0;
  while (queue.size() > 0) {
    std::tuple<std::string, int> p;

    p = queue[0];
    queue.erase(queue.begin());
    if (std::get<1>(p) <= q) {
      // the process ends
      elapsed += std::get<1>(p);
      std::cout << std::get<0>(p) << " " << elapsed << std::endl;
      continue;
    }
    std::get<1>(p) -= q;
    queue.push_back(p);
    elapsed += q;
  }
}

void c_4_2() {
  std::string line;
  std::string s;

  // input
  std::getline(std::cin, line);
  std::istringstream iss(line);
  std::vector<std::string> a;
  while (iss >> s) {
    if (s == "+") {
      const auto result = std::stoi(a[1]) + std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else if (s == "-") {
      const auto result = std::stoi(a[1]) - std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else if (s == "*") {
      const auto result = std::stoi(a[1]) * std::stoi(a[0]);
      a.erase(a.begin(), a.begin() + 2);
      a.insert(a.begin(), std::to_string(result));
    } else {
      a.insert(a.begin(), s);
    }
  }
  std::cout << a[0] << std::endl;
}
// 正解との相違: 演算子は格納しなくてよいので配列の要素の型は整数でよい
// C++で実装するならstd::stackを使ってよいだろう