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


#include <algorithm>
#include <iostream>
#include <list>
#include <stdio.h>
#include <utility>
#include <vector>

int main(void) {

  using namespace std;

  while (true) {

    // 入力を受け取る

    int n, w, d, p, s;
    cin >> n >> w >> d;
    if (!(n | w | d))
      break;

    // listの宣言　int, int = 横, 縦
    list<pair<int, int>> l;
    l.push_back(make_pair(w, d));

    for (int i = 0; i < n; i++) {

      cin >> p >> s;
      int indx = 0;
      list<pair<int, int>>::iterator it = l.begin();

      // インデックスの走査
      for (; it != l.end(); ++it) {
        if (indx == p - 1)
          break;
        indx++;
      }

      // カット位置の定義
      if ((2 * (*it).first + 2 * (*it).second) > 0) {
        s %= (2 * (*it).first + 2 * (*it).second);
      }

      pair<pair<int, int>, pair<int, int>> p;

      if (s < (*it).first || (s < (*it).second + (*it).first * 2 &&
                              s > (*it).first + (*it).second)) {

        if ((s < (*it).second + (*it).first * 2 &&
             s > (*it).first + (*it).second)) {

          s -= (*it).first + (*it).second;
        }

        p = make_pair(make_pair(s, (*it).second),
                      make_pair(abs((*it).first - s), (*it).second));

      } else {

        if (s > (*it).first && s < (*it).first + (*it).second) {

          s -= (*it).first;

        } else {

          s -= (*it).first * 2 + (*it).second;
        }

        p = make_pair(make_pair((*it).first, abs((*it).second - s)),
                      make_pair((*it).first, s));
      }

      it = l.erase(it);

      if (p.first.first * p.first.second < p.second.first * p.second.second) {
        l.push_back(p.first);
        l.push_back(p.second);

      } else {
        l.push_back(p.second);
        l.push_back(p.first);
      }
    }
    vector<int> sq;

    for (list<pair<int, int>>::iterator it = l.begin(); it != l.end(); ++it) {

      sq.push_back((*it).first * (*it).second);
    }

    sort(sq.begin(), sq.end());

    for (int i = 0; i < sq.size(); i++) {
      if (i > 0)
        cout << " " << sq[i];
      else
        cout << sq[i];
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <iostream>
#include <list>
#include <stdio.h>
#include <utility>
#include <vector>

int main(void) {

  using namespace std;

  while (true) {

    // 入力を受け取る

    int n, w, d, p, s;
    cin >> n >> w >> d;
    if (!(n | w | d))
      break;

    // listの宣言　int, int = 横, 縦
    list<pair<int, int>> l;
    l.push_back(make_pair(w, d));

    for (int i = 0; i < n; i++) {

      cin >> p >> s;
      int indx = 0;
      list<pair<int, int>>::iterator it = l.begin();
      pair<int, int> target;

      // インデックスの走査
      for (; it != l.end(); ++it) {
        if (indx == p - 1)
          break;
        indx++;
      }

      // カット位置の定義
      s %= (2 * (*it).first + 2 * (*it).second);

      pair<pair<int, int>, pair<int, int>> p;

      if (s < (*it).first || (s < (*it).second + (*it).first * 2 &&
                              s > (*it).first + (*it).second)) {

        if ((s < (*it).second + (*it).first * 2 &&
             s > (*it).first + (*it).second)) {

          s -= (*it).first + (*it).second;
        }

        p = make_pair(make_pair(s, (*it).second),
                      make_pair(abs((*it).first - s), (*it).second));

      } else {

        if (s < (*it).first + (*it).second) {

          s -= (*it).first;

        } else {

          s -= (*it).first * 2 + (*it).second;
        }

        p = make_pair(make_pair((*it).first, abs((*it).second - s)),
                      make_pair((*it).first, s));
      }

      it = l.erase(it);
      if (p.first.first * p.first.second < p.second.first * p.second.second) {
        l.insert(it, p.first);
        l.insert(it, p.second);
      } else {
        l.insert(it, p.second);
        l.insert(it, p.first);
      }
    }
    vector<int> sq;

    for (list<pair<int, int>>::iterator it = l.begin(); it != l.end(); ++it) {

      sq.push_back((*it).first * (*it).second);
    }

    sort(sq.begin(), sq.end());

    for (int i = 0; i < sq.size(); i++) {
      if (i > 0)
        cout << " " << sq[i];
      else
        cout << sq[i];
    }
    cout << endl;
  }

  return 0;
}