Problem p00837 (C++) — buggy submission, verdict: Runtime Error

/* 2012-11-23T16:06:54 */
#define DEBUG_ON_
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<int>(cerr, " "));                            \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    IFC(s[i] == ' ');
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/

#define KMAX 51

int main() {
  std::ios_base::sync_with_stdio(false);
  int m, c, n;
  while (cin >> m >> c >> n, m || c || n) {
    queue<queue<int>> students;
    REP(i, n) {
      int k;
      cin >> k;
      queue<int> books;
      REP(j, k) {
        int t;
        cin >> t;
        books.push(t);
      }
      students.push(books);
    }

    VI address(100, m); /* place of books */
    VVI desks(m, VI());

    int cost = 0;
    while (!students.empty()) {
      queue<int> s = students.front();
      students.pop();
      int b = s.front();
      s.pop();
      dprt("Request: %d\\n", b);

      /* Get the book */
      cost += address[b] + 1;
      dprt("\\tget it from %d\\n", address[b]);
      if (address[b] != m) {
        desks[address[b]].erase(find(ALL(desks[address[b]]), b));
      }

      /* Store the book */
      if (desks[0].size() < c) {
        cerr << "\\tput it on desk 0" << endl;
        desks[0].PB(b);
        address[b] = 0;
        cost++;
      } else {
        bool shelf = true;
        int tmp;
        int i;
        for (i = 0; i < m; ++i) {
          if (desks[i].size() < c) {
            cost += i + 1;
            tmp = i;
            shelf = false;
            break;
          }
        }
        if (shelf) {
          cost += m + 1;
          tmp = m;
        }
        dprt("\\ttemporary put it on %d\\n", tmp);

        int lru = desks[0][0];
        dprt("\\ttake lru(%d) from desk 0\\n", lru);
        desks[0].erase(desks[0].begin());
        cost++;

        int lruput;
        shelf = true;
        FOR(j, 1, m) {
          if (desks[j].size() + (j == i) < c) {
            desks[j].PB(lru);
            address[lru] = j;
            lruput = j;
            cost += j + 1;
            shelf = false;
            break;
          }
        }
        if (shelf) {
          cost += m + 1;
          lruput = m;
          address[lru] = m;
        }
        dprt("\\tput lru on %d\\n", lruput);

        dprt("\\tretrive book from tmp(%d)\\n", tmp);
        cost += tmp + 1;

        cerr << "\\tput it on desk 0" << endl;
        desks[0].PB(b);
        cost++;
        address[b] = 0;
      }

      if (s.size() > 0) {
        students.push(s);
      }

      dump(cost);
    }

    cout << cost << endl;
  }
}

Error: 0
Request: 50
	get it from 2
	put it on desk 0
 [L214] cost = 4
Request: 50
	get it from 2
	put it on desk 0
 [L214] cost = 4
Request: 60
	get it from 2
	temporary put it on 1
	take lru(50) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 16
Request: 60
	get it from 2
	put it on desk 0
 [L214] cost = 4
Request: 70
	get it from 2
	temporary put it on 1
	take lru(60) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 16
Request: 61
	get it from 2
	temporary put it on 1
	take lru(70) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 28
Request: 60
	get it from 4
	put it on desk 0
 [L214] cost = 6
Request: 70
	get it from 4
	put it on desk 0
 [L214] cost = 12
Request: 80
	get it from 4
	temporary put it on 1
	take lru(60) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 25
Request: 61
	get it from 4
	temporary put it on 1
	take lru(70) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 39
Request: 81
	get it from 4
	temporary put it on 1
	take lru(80) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 53
Request: 62
	get it from 4
	temporary put it on 1
	take lru(61) from desk 0
	put lru on 3
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 68
Request: 60
	get it from 3
	put it on desk 0
 [L214] cost = 5
Request: 70
	get it from 3
	temporary put it on 1
	take lru(60) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 18
Request: 61
	get it from 3
	temporary put it on 1
	take lru(70) from desk 0
	put lru on 3
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 32
Request: 60
	get it from 2
	temporary put it on 1
	take lru(61) from desk 0
	put lru on 2
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 44
Request: 62
	get it from 3
	temporary put it on 1
	take lru(60) from desk 0
	put lru on 3
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 58
Request: 87
	get it from 1
	put it on desk 0
 [L214] cost = 3
Request: 96
	get it from 1
	put it on desk 0
 [L214] cost = 6
Request: 68
	get it from 1
	temporary put it on 1
	take lru(87) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 16
Request: 3
	get it from 1
	temporary put it on 1
	take lru(96) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 26
Request: 57
	get it from 1
	temporary put it on 1
	take lru(68) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 36
Request: 95
	get it from 1
	temporary put it on 1
	take lru(3) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 46
Request: 71
	get it from 1
	temporary put it on 1
	take lru(57) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 56
Request: 2
	get it from 1
	temporary put it on 1
	take lru(95) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 66
Request: 18
	get it from 1
	temporary put it on 1
	take lru(71) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 76
Request: 2
	get it from 0
	put it on desk 0
 [L214] cost = 78
Request: 35
	get it from 1
	temporary put it on 1
	take lru(18) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 88
Request: 93
	get it from 1
	temporary put it on 1
	take lru(2) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 98
Request: 1
	get it from 2
	put it on desk 0
 [L214] cost = 4
Request: 2
	get it from 2
	put it on desk 0
 [L214] cost = 8
Request: 1
	get it from 0
	put it on desk 0
 [L214] cost = 10
Request: 3
	get it from 2
	temporary put it on 1
	take lru(2) from desk 0
	put lru on 1
	retrive book from tmp(1)
	put it on desk 0
 [L214] cost = 21
Request: 1
	get it from 0
	put it on desk 0
 [L214] cost = 23


Fixed submission:

/* 2012-11-23T16:06:54 */
#define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<int>(cerr, " "));                            \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    IFC(s[i] == ' ');
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/

#define KMAX 51

int main() {
  std::ios_base::sync_with_stdio(false);
  int m, c, n;
  while (cin >> m >> c >> n, m || c || n) {
    queue<queue<int>> students;
    REP(i, n) {
      int k;
      cin >> k;
      queue<int> books;
      REP(j, k) {
        int t;
        cin >> t;
        books.push(t);
      }
      students.push(books);
    }

    VI address(100, m); /* place of books */
    VVI desks(m, VI());

    int cost = 0;
    while (!students.empty()) {
      queue<int> s = students.front();
      students.pop();
      int b = s.front();
      s.pop();
      dprt("Request: %d\\n", b);

      /* Get the book */
      cost += address[b] + 1;
      dprt("\\tget it from %d\\n", address[b]);
      if (address[b] != m) {
        desks[address[b]].erase(find(ALL(desks[address[b]]), b));
      }

      /* Store the book */
      if (desks[0].size() < c) {
        cerr << "\\tput it on desk 0" << endl;
        desks[0].PB(b);
        address[b] = 0;
        cost++;
      } else {
        bool shelf = true;
        int tmp;
        int i;
        for (i = 0; i < m; ++i) {
          if (desks[i].size() < c) {
            cost += i + 1;
            tmp = i;
            shelf = false;
            break;
          }
        }
        if (shelf) {
          cost += m + 1;
          tmp = m;
        }
        dprt("\\ttemporary put it on %d\\n", tmp);

        int lru = desks[0][0];
        dprt("\\ttake lru(%d) from desk 0\\n", lru);
        desks[0].erase(desks[0].begin());
        cost++;

        int lruput;
        shelf = true;
        FOR(j, 1, m) {
          if (desks[j].size() + (j == i) < c) {
            desks[j].PB(lru);
            address[lru] = j;
            lruput = j;
            cost += j + 1;
            shelf = false;
            break;
          }
        }
        if (shelf) {
          cost += m + 1;
          lruput = m;
          address[lru] = m;
        }
        dprt("\\tput lru on %d\\n", lruput);

        dprt("\\tretrive book from tmp(%d)\\n", tmp);
        cost += tmp + 1;

        cerr << "\\tput it on desk 0" << endl;
        desks[0].PB(b);
        cost++;
        address[b] = 0;
      }

      if (s.size() > 0) {
        students.push(s);
      }

      dump(cost);
    }

    cout << cost << endl;
  }
}