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

#include <algorithm>
#include <iostream>
#include <math.h> /* pow */
#include <numeric>
#include <stack>
#include <stdio.h> /* printf */
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

typedef std::pair<int, int> point;
typedef std::vector<point> scenery;
typedef std::vector<int> surface;

bool mount(point i, point j) { return i.second < j.second; };

void print_array(scenery list) {
  for (int i = 0; i < list.size(); ++i) {
    if (i > 0)
      std::cout << " ";
    std::cout << "{" << list[i].first << "," << list[i].second << "}";
  }
  std::cout << std::endl;
}

void print_surface(surface list) {
  for (int i = 0; i < list.size(); ++i) {
    std::cout << " " << list[i];
  }
  std::cout << std::endl;
}

int surfaceTotal(surface list) {
  int surf = 0;
  for (int i = 0; i < list.size(); ++i) {
    surf += list[i];
  }
  return surf;
}

void height(std::string value, scenery &list) {
  list.push_back(std::make_pair(0, 0));
  for (int i = 0; i < value.size(); i++) {
    if (value.compare(i, 1, "\\\\") == 0) {
      list.push_back(std::make_pair(i + 1, list.back().second - 1));
    } else if (value.compare(i, 1, "/") == 0) {
      list.push_back(std::make_pair(i + 1, list.back().second + 1));
    } else {
      list.push_back(std::make_pair(i + 1, list.back().second));
    }
  }
}

std::pair<int, point> highestPeak(scenery &list) {
  int pos = 0;
  point peak = list[pos];
  for (int i = 1; i < list.size(); i++) {
    if (list[i].second > peak.second) {
      peak = list[i];
      pos = i;
    }
  }
  return std::make_pair(pos, peak);
}

void findPeaks(scenery &list, scenery &peaks) {
  std::pair<int, point> peak = highestPeak(list);
  if (peak.first != 0) {
    peaks.push_back(peak.second);
  }
  int i;
  for (i = peak.first + 1; i < list.size(); i++) {
    if (list[i].second < peaks.back().second)
      break;
  }
  if (i == list.size() - 1) {
    return;
  } else {
    scenery::const_iterator firstRight = list.begin() + i;
    scenery::const_iterator lastRight = list.end();
    scenery newVecRight(firstRight, lastRight);
    // print_array(newVec);
    findPeaks(newVecRight, peaks);

    /*std::vector<std::pair<int,int>>::const_iterator firstLeft = list.begin() +
    i; std::vector<std::pair<int,int>>::const_iterator last = list.end();
    std::vector<std::pair<int,int>> newVec(first, last);
    //print_array(newVec);
    findPeaks(newVec,peaks);*/
  }
}

void findPeaks2(scenery &list, scenery &peaks) {
  bool climb = true;
  for (int i = 1; i < list.size(); i++) {
    if (list[i].second > list[i - 1].second) {
      if (i == list.size() - 1) {
        peaks.push_back(list[i]);
      }
      climb = true;
    } else if (list[i].second <= list[i - 1].second) {
      if (climb == true) {
        peaks.push_back(list[i - 1]);
      }
      climb = false;
    }
  }
}

int computeSurface(scenery &list, int begin, int end) {
  int surf = 0;

  for (int i = begin; i < end; i++) {
    if (list[i].second <= list[end].second) {
      if (list[i].second == list[i + 1].second) {
        surf += (list[end].second - list[i + 1].second) * 2;
      } else if (abs(list[end].second - list[i + 1].second) >
                 abs(list[end].second - list[i].second)) {
        surf += (abs(list[end].second - list[i + 1].second) * 2 - 1);
      } else {
        surf += (abs(list[end].second - list[i].second) * 2 - 1);
      }
    }
    // std::cout << surf << std::endl;
  }
  return surf / 2;
}

int computeSurface2(scenery &list, int begin, int end) {
  int surf = 0;

  for (int i = begin; i < end; i++) {
    if (list[i].second <= list[begin].second) {
      if (list[i].second == list[i + 1].second) {
        surf += (list[begin].second - list[i + 1].second) * 2;
      } else if (abs(list[begin].second - list[i + 1].second) >
                 abs(list[begin].second - list[i].second)) {
        surf += (abs(list[begin].second - list[i + 1].second) * 2 - 1);
      } else {
        surf += (abs(list[begin].second - list[i].second) * 2 - 1);
      }
    }
    // std::cout << surf << std::endl;
  }
  return surf / 2;
}

void highestPeaks(scenery &list, scenery &peaks,
                  scenery::iterator &highestMount, surface &surfaceTot) {
  scenery::iterator secondHighestMount =
      std::max_element(highestMount + 1, peaks.end(), mount);
  int toto =
      computeSurface(list, highestMount->first, secondHighestMount->first);
  if (toto > 0) {
    surfaceTot.push_back(toto);
  }
  // std::cout << highestMount->first << ":" << secondHighestMount->first <<
  // "::surface" << surfaceTotal(surfaceTot) << std::endl;
  if (secondHighestMount != peaks.end() - 1 &&
      highestMount != peaks.end() - 1) {
    // std::cout << secondHighestMount->first << ":" << peaks.end()->first <<
    // std::endl;
    return highestPeaks(list, peaks, secondHighestMount, surfaceTot);
  }
}

int main(void) {

  scenery list;
  surface surfaces;
  std::string value;
  std::cin >> value;

  height(value, list);

  scenery peaks;
  findPeaks2(list, peaks);
  // print_array(list);
  // print_array(peaks);
  scenery::iterator highestMount =
      std::max_element(peaks.begin(), peaks.end(), mount);
  if (highestMount != peaks.begin()) {
    //   std::cout << "bleubleu" << std::endl;
    scenery leftList, leftPeaks;
    int j = 0;
    for (int i = highestMount->first; i >= peaks.begin()->first; i--) {
      leftList.push_back(std::make_pair(j, list[i].second));
      j++;
    }
    findPeaks2(leftList, leftPeaks);
    // print_array(leftList);
    // print_array(leftPeaks);
    scenery::iterator leftHighestMount =
        std::max_element(leftPeaks.begin(), leftPeaks.end(), mount);
    surface leftSurfaces;
    highestPeaks(leftList, leftPeaks, leftHighestMount, leftSurfaces);
    for (int g = leftSurfaces.size() - 1; g >= 0; g--) {
      surfaces.push_back(leftSurfaces[g]);
    }
  }
  if (highestMount != peaks.end() - 1) {
    highestPeaks(list, peaks, highestMount, surfaces);
  }
  std::cout << surfaceTotal(surfaces) << std::endl;
  std::cout << surfaces.size();
  print_surface(surfaces);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h> /* pow */
#include <numeric>
#include <stack>
#include <stdio.h> /* printf */
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

typedef std::pair<int, int> point;
typedef std::vector<point> scenery;
typedef std::vector<int> surface;

bool mount(point i, point j) { return i.second < j.second; };

void print_array(scenery list) {
  for (int i = 0; i < list.size(); ++i) {
    if (i > 0)
      std::cout << " ";
    std::cout << "{" << list[i].first << "," << list[i].second << "}";
  }
  std::cout << std::endl;
}

void print_surface(surface list) {
  for (int i = 0; i < list.size(); ++i) {
    std::cout << " " << list[i];
  }
  std::cout << std::endl;
}

int surfaceTotal(surface list) {
  int surf = 0;
  for (int i = 0; i < list.size(); ++i) {
    surf += list[i];
  }
  return surf;
}

void height(std::string value, scenery &list) {
  list.push_back(std::make_pair(0, 0));
  for (int i = 0; i < value.size(); i++) {
    if (value.compare(i, 1, "\\\\") == 0) {
      list.push_back(std::make_pair(i + 1, list.back().second - 1));
    } else if (value.compare(i, 1, "/") == 0) {
      list.push_back(std::make_pair(i + 1, list.back().second + 1));
    } else {
      list.push_back(std::make_pair(i + 1, list.back().second));
    }
  }
}

std::pair<int, point> highestPeak(scenery &list) {
  int pos = 0;
  point peak = list[pos];
  for (int i = 1; i < list.size(); i++) {
    if (list[i].second > peak.second) {
      peak = list[i];
      pos = i;
    }
  }
  return std::make_pair(pos, peak);
}

void findPeaks(scenery &list, scenery &peaks) {
  std::pair<int, point> peak = highestPeak(list);
  if (peak.first != 0) {
    peaks.push_back(peak.second);
  }
  int i;
  for (i = peak.first + 1; i < list.size(); i++) {
    if (list[i].second < peaks.back().second)
      break;
  }
  if (i == list.size() - 1) {
    return;
  } else {
    scenery::const_iterator firstRight = list.begin() + i;
    scenery::const_iterator lastRight = list.end();
    scenery newVecRight(firstRight, lastRight);
    // print_array(newVec);
    findPeaks(newVecRight, peaks);

    /*std::vector<std::pair<int,int>>::const_iterator firstLeft = list.begin() +
    i; std::vector<std::pair<int,int>>::const_iterator last = list.end();
    std::vector<std::pair<int,int>> newVec(first, last);
    //print_array(newVec);
    findPeaks(newVec,peaks);*/
  }
}

void findPeaks2(scenery &list, scenery &peaks) {
  bool climb = true;
  for (int i = 1; i < list.size(); i++) {
    if (list[i].second > list[i - 1].second) {
      if (i == list.size() - 1) {
        peaks.push_back(list[i]);
      }
      climb = true;
    } else if (list[i].second < list[i - 1].second) {
      if (climb == true) {
        peaks.push_back(list[i - 1]);
      }
      climb = false;
    }
  }
}

int computeSurface(scenery &list, int begin, int end) {
  int surf = 0;

  for (int i = begin; i < end; i++) {
    if (list[i].second <= list[end].second) {
      if (list[i].second == list[i + 1].second) {
        surf += (list[end].second - list[i + 1].second) * 2;
      } else if (abs(list[end].second - list[i + 1].second) >
                 abs(list[end].second - list[i].second)) {
        surf += (abs(list[end].second - list[i + 1].second) * 2 - 1);
      } else {
        surf += (abs(list[end].second - list[i].second) * 2 - 1);
      }
    }
    // std::cout << surf << std::endl;
  }
  return surf / 2;
}

int computeSurface2(scenery &list, int begin, int end) {
  int surf = 0;

  for (int i = begin; i < end; i++) {
    if (list[i].second <= list[begin].second) {
      if (list[i].second == list[i + 1].second) {
        surf += (list[begin].second - list[i + 1].second) * 2;
      } else if (abs(list[begin].second - list[i + 1].second) >
                 abs(list[begin].second - list[i].second)) {
        surf += (abs(list[begin].second - list[i + 1].second) * 2 - 1);
      } else {
        surf += (abs(list[begin].second - list[i].second) * 2 - 1);
      }
    }
    // std::cout << surf << std::endl;
  }
  return surf / 2;
}

void highestPeaks(scenery &list, scenery &peaks,
                  scenery::iterator &highestMount, surface &surfaceTot) {
  scenery::iterator secondHighestMount =
      std::max_element(highestMount + 1, peaks.end(), mount);
  surfaceTot.push_back(
      computeSurface(list, highestMount->first, secondHighestMount->first));
  // std::cout << highestMount->first << ":" << secondHighestMount->first <<
  // "::surface" << surfaceTotal(surfaceTot) << std::endl;
  if (secondHighestMount != peaks.end() - 1) {
    // std::cout << secondHighestMount->first << ":" << peaks.end()->first <<
    // std::endl;
    return highestPeaks(list, peaks, secondHighestMount, surfaceTot);
  }
}

int main(void) {

  scenery list;
  surface surfaces;
  std::string value;
  std::cin >> value;

  height(value, list);

  scenery peaks;
  findPeaks2(list, peaks);
  // print_array(list);
  //  print_array(peaks);
  scenery::iterator highestMount =
      std::max_element(peaks.begin(), peaks.end(), mount);
  if (highestMount != peaks.begin()) {
    surfaces.push_back(
        computeSurface2(list, peaks.begin()->first, highestMount->first));
  }
  highestPeaks(list, peaks, highestMount, surfaces);
  std::cout << surfaceTotal(surfaces) << std::endl;
  std::cout << surfaces.size();
  print_surface(surfaces);
  return 0;
}