Problem p02556 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<long> rightUp(n), rightDown(n);

  for (int i = 0; i < n; i++) {
    long x, y;
    std::cin >> x >> y;
    rightUp[i] = (x + y);
    rightDown[i] = (x - y);
  }

  std::sort(rightUp.begin(), rightUp.end());
  std::sort(rightDown.begin(), rightDown.end());
  std::cout << std::max(rightUp[n - 1] - rightUp[0],
                        rightDown[n - 1] - rightDown[0])
            << std::endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::pair<int, int>> points;

// 2点の傾き
float getGradient(int aIndex, int bIndex) {
  return (points[bIndex].second - points[aIndex].second) * 1.0f /
         (points[bIndex].first - points[aIndex].first);
}

int getDownOuterIndex(int nowIndex) {
  int index = 0;
  float maxGradient = -1000.0f;
  int size = points.size();
  for (int i = 0; i < size; i++) {
    if (points[nowIndex].first < points[i].first) {
      float gradient = getGradient(nowIndex, i);
      if (maxGradient < gradient) {
        index = i;
        maxGradient = gradient;
      }
    }
  }
  return index;
}

int getUpOuterIndex(int nowIndex) {
  int index = 0;
  float minGradient = 1000.0f;
  int size = points.size();
  for (int i = 0; i < size; i++) {
    if (points[nowIndex].first > points[i].first) {
      float gradient = getGradient(nowIndex, i);
      if (minGradient > gradient) {
        index = i;
        minGradient = gradient;
      }
    }
  }
  return index;
}

int main() {
  int n;
  std::cin >> n;

  points.reserve(n);
  int x, y;
  for (int i = 0; i < n; i++) {
    std::cin >> x >> y;
    points.push_back(std::pair<int, int>(x, y));
  }

  std::vector<int> outerPoints;

  auto leftPointIter =
      std::min_element(points.begin(), points.end(),
                       [](std::pair<int, int> a, std::pair<int, int> b) {
                         return a.first < b.first;
                       });
  const int leftPointIndex = std::distance(points.begin(), leftPointIter);

  auto rightPointIter =
      std::max_element(points.begin(), points.end(),
                       [](std::pair<int, int> a, std::pair<int, int> b) {
                         return a.first < b.first;
                       });
  const int rightPointIndex = std::distance(points.begin(), rightPointIter);

  {
    int targetIndex = leftPointIndex;
    while (true) {
      outerPoints.push_back(targetIndex);
      int nextIndex = getDownOuterIndex(targetIndex);
      if (nextIndex == rightPointIndex) {
        break;
      }
      targetIndex = nextIndex;
    }

    targetIndex = rightPointIndex;
    while (true) {
      outerPoints.push_back(targetIndex);
      int nextIndex = getDownOuterIndex(targetIndex);
      if (nextIndex == leftPointIndex) {
        break;
      }
      targetIndex = nextIndex;
    }
  }

  int maxDistance = 0;
  {
    int outerPointsSize = outerPoints.size();
    for (int i = 0; i < outerPointsSize; i++) {
      for (int j = i + 1; j < outerPointsSize; j++) {
        auto distance = std::abs(points[outerPoints[j]].first -
                                 points[outerPoints[i]].first) +
                        std::abs(points[outerPoints[j]].second -
                                 points[outerPoints[i]].second);
        maxDistance = std::max(maxDistance, distance);
      }
    }
  }

  std::cout << maxDistance;
}