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

/*  -*- coding: utf-8 -*-
 *
 * f.cc: F - Contrast
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N], ds[MAX_N];
int acs[MAX_N + 1], bcs[MAX_N + 1];

/* subroutines */

void calccs(int n, int vs[], int cs[]) {
  cs[0] = 0;
  for (int v = 1, i = 0; v <= n; v++) {
    while (i < n && vs[i] == v)
      i++;
    cs[v] = i;
  }
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", as + i);
  for (int i = 0; i < n; i++)
    scanf("%d", bs + i);

  calccs(n, as, acs);
  calccs(n, bs, bcs);
  // for (int i = 0; i <= n; i++) printf("%d ", acs[i]); putchar('\\n');
  // for (int i = 0; i <= n; i++) printf("%d ", bcs[i]); putchar('\\n');

  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    if (acs[i] - acs[i - 1] + bcs[i] - bcs[i - 1] > n) {
      puts("No");
      return 0;
    }

    int x = acs[i] - bcs[i - 1];
    if (maxx < x)
      maxx = x;
  }
  // printf("maxx=%d\\n", maxx);

  for (int i = 0; i < n; i++)
    ds[(i + maxx) % n] = bs[i];

  puts("Yes");
  for (int i = 0; i < n; i++) {
    printf("%d", ds[i]);
    putchar(i + 1 < n ? ' ' : '\\n');
  }
  return 0;
}


Error: 0

Fixed submission:

/*  -*- coding: utf-8 -*-
 *
 * f.cc: F - Contrast
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

struct Elm {
  int i, j, l, v, u;
  Elm() {}
  Elm(int _i, int _j, int _v) : i(_i), j(_j), l(_j - _i), v(_v), u(0) {}

  bool operator<(const Elm &e) const {
    return u < e.u || (u == e.u && l < e.l);
  }

  void print() { printf("Elm(%d,%d,%d,%d,%d)\\n", i, j, l, v, u); }
};

/* global variables */

int as[MAX_N], bs[MAX_N], ds[MAX_N];
Elm eas[MAX_N], ebs[MAX_N];

/* subroutines */

int calces(int n, int vs[], Elm es[]) {
  int m = 0;
  for (int i = 0; i < n;) {
    int j = i + 1;
    while (j < n && vs[i] == vs[j])
      j++;
    es[m++] = Elm(i, j, vs[i]);
    i = j;
  }
  return m;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", as + i);
  for (int i = 0; i < n; i++)
    scanf("%d", bs + i);

  int an = calces(n, as, eas);
  int bn = calces(n, bs, ebs);

  for (int i = 0, j = 0; i < an && j < bn;) {
    Elm &ei = eas[i], &ej = ebs[j];
    if (ei.v < ej.v)
      i++;
    else if (ei.v > ej.v)
      j++;
    else {
      if (ei.l + ej.l > n) {
        puts("No");
        return 0;
      }
      ei.u = ej.u = 1;
      i++, j++;
    }
  }
  sort(eas, eas + an);
  sort(ebs, ebs + bn);
  // for (int i = 0; i < an; i++) eas[i].print();
  // for (int i = 0; i < bn; i++) ebs[i].print();

  memset(ds, -1, sizeof(ds));

  for (int i = 0; i < an && !eas[i].u; i++) {
    Elm &ea = eas[i];
    int l = ea.l, p = ea.i;
    while (l > 0) {
      Elm &eb = ebs[bn - 1];
      int k = min(l, eb.l);
      l -= k, eb.l -= k;
      while (k-- > 0)
        ds[p++] = eb.v;
      if (eb.l == 0)
        bn--;
    }
  }

  for (int i = an - 1; i >= 0 && eas[i].u; i--) {
    Elm &ea = eas[i];
    int l = ea.l, p = ea.i, j = bn - 1;
    while (l > 0) {
      Elm &eb = ebs[j];
      if (ea.v != eb.v) {
        int k = min(l, eb.l);
        l -= k, eb.l -= k;
        while (k-- > 0)
          ds[p++] = eb.v;
      }
      j--;
    }

    while (bn > 0 && ebs[bn - 1].l == 0)
      bn--;
  }

  puts("Yes");
  for (int i = 0; i < n; i++) {
    printf("%d", ds[i]);
    putchar(i + 1 < n ? ' ' : '\\n');
  }
  return 0;
}