Problem p03841 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int n, x[500];
int a[100000];
vector<pair<int, int>> col; // first:position,second:value

int main(void) {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> x[i];
  int siz = n * n;
  fill(a, a + siz, -1);
  for (int i = 0; i < n; ++i) {
    a[x[i] - 1] = i + 1;
    col.emplace_back(x[i] - 1, i + 1);
  }
  sort(col.begin(), col.end());
  int gauge = 0;
  for (auto itr = col.begin(); itr != col.end(); ++itr) {
    int umnum = itr->second - 1;
    for (int i = 0; i < umnum; ++i) {
      while (gauge < siz && a[gauge] >= 0)
        ++gauge;
      if (gauge >= siz)
        return 0 * printf("No\n");
      a[gauge] = itr->second;
      if (gauge > itr->first) {
        return 0 * printf("No\n");
      }
    }
  }
  gauge = siz - 1;
  for (auto itr = col.rbegin(); itr != col.rend(); ++itr) {
    int umnum = n - itr->second;
    for (int i = 0; i < umnum; ++i) {
      while (gauge >= 0 && a[gauge] >= 0)
        --gauge;
      if (gauge < 0)
        return 0 * printf("No\n");
      a[gauge] = itr->second;
      if (gauge < itr->first) {
        return 0 * printf("No\n");
      }
    }
  }
  printf("Yes\n");
  for (int i = 0; i < siz; ++i) {
    cout << a[i];
    if (i != siz - 1) {
      cout << " ";
    } else {
      cout << "\n";
    }
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int n, x[500];
int a[1000000];
vector<pair<int, int>> col; // first:position,second:value

int main(void) {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> x[i];
  int siz = n * n;
  fill(a, a + siz, -1);
  for (int i = 0; i < n; ++i) {
    a[x[i] - 1] = i + 1;
    col.emplace_back(x[i] - 1, i + 1);
  }
  sort(col.begin(), col.end());
  int gauge = 0;
  for (auto itr = col.begin(); itr != col.end(); ++itr) {
    int umnum = itr->second - 1;
    for (int i = 0; i < umnum; ++i) {
      while (gauge < siz && a[gauge] >= 0)
        ++gauge;
      if (gauge >= siz)
        return 0 * printf("No\n");
      a[gauge] = itr->second;
      if (gauge > itr->first) {
        return 0 * printf("No\n");
      }
    }
  }
  gauge = siz - 1;
  for (auto itr = col.rbegin(); itr != col.rend(); ++itr) {
    int umnum = n - itr->second;
    for (int i = 0; i < umnum; ++i) {
      while (gauge >= 0 && a[gauge] >= 0)
        --gauge;
      if (gauge < 0)
        return 0 * printf("No\n");
      a[gauge] = itr->second;
      if (gauge < itr->first) {
        return 0 * printf("No\n");
      }
    }
  }
  printf("Yes\n");
  for (int i = 0; i < siz; ++i) {
    cout << a[i];
    if (i != siz - 1) {
      cout << " ";
    } else {
      cout << "\n";
    }
  }
  return 0;
}