description: On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly n/3 marked cells of each of three colors! 
  
  Find the largest such k that it's possible to choose k/3 cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold:
  
    * No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be 0.
    * The i-th rectangle contains all the chosen cells of the i-th color and no chosen cells of other colors, for i = 1, 2, 3. 
  
  Input
  
  The first line of the input contains a single integer n — the number of the marked cells (3 ≤ n ≤ 10^5, n is divisible by 3).
  
  The i-th of the following n lines contains three integers x_i, y_i, c_i (|x_i|,|y_i| ≤ 10^9; 1 ≤ c_i ≤ 3), where (x_i, y_i) are the coordinates of the i-th marked cell and c_i is its color.
  
  It's guaranteed that all cells (x_i, y_i) in the input are distinct, and that there are exactly n/3 cells of each color.
  
  Output
  
  Output a single integer k — the largest number of cells you can leave.
  
  Examples
  
  Input
  
  
  9
  2 3 1
  4 1 2
  2 1 3
  3 4 1
  5 3 2
  4 4 3
  2 4 1
  5 2 2
  3 5 3
  
  
  Output
  
  
  6
  
  
  Input
  
  
  3
  1 1 1
  2 2 2
  3 3 3
  
  
  Output
  
  
  3
  
  Note
  
  In the first sample, it's possible to leave 6 cells with indexes 1, 5, 6, 7, 8, 9.
  
  In the second sample, it's possible to leave 3 cells with indexes 1, 2, 3.
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 1e5 + 5, inf = 1e9;
  struct P {
    int x, y, c;
  } p[N];
  int n, ans, R[5];
  void sol1(vector<int>& v) {
    int l = 1, r = n / 3, f = 0;
    while (l <= r) {
      int mid = (l + r) >> 1, j = 0, k = 0;
      for (int i = 0; i < n && j < 3; i++)
        if (v[j] == p[i].c && ++k == mid) ++j, k = 0;
      if (j == 3)
        l = mid + 1, f = mid;
      else
        r = mid - 1;
    }
    ans = max(ans, 3 * f);
  }
  void sol2(int q) {
    int a, b;
    if (q == 1) a = 2, b = 3;
    if (q == 2) a = 1, b = 3;
    if (q == 3) a = 1, b = 2;
    int l = 1, r = n / 3, f = 0;
    while (l <= r) {
      int mid = (l + r) >> 1, s = n, k = 0;
      for (int i = 0; i < n; i++)
        if (p[i].c == q && ++k == mid) {
          s = i + 1;
          break;
        }
      vector<P> v;
      for (int i = s; i < n; i++)
        if (p[i].c != q) v.push_back(p[i]);
      sort(v.begin(), v.end(), [&](P a, P b) { return a.y < b.y; });
      int la = inf, ra = -inf, lb = inf, rb = -inf, t = 0;
      for (auto& it : v)
        if (it.c == a && ++t == mid) {
          la = it.y;
          break;
        }
      t = 0;
      for (auto& it : v)
        if (it.c == b && ++t == mid) {
          lb = it.y;
          break;
        }
      reverse(v.begin(), v.end());
      t = 0;
      for (auto& it : v)
        if (it.c == a && ++t == mid) {
          ra = it.y;
          break;
        }
      t = 0;
      for (auto& it : v)
        if (it.c == b && ++t == mid) {
          rb = it.y;
          break;
        }
      if (la < rb || lb < ra)
        l = mid + 1, f = mid;
      else
        r = mid - 1;
    }
    ans = max(ans, 3 * f);
  }
  int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].c);
    for (int i = 0; i < 4; i++) {
      if (i < 2) {
        vector<int> v = {1, 2, 3};
        do {
          for (int i = 0; i < 3; i++) R[v[i]] = i;
          sort(p, p + n, [&](P a, P b) {
            if (a.x == b.x) return R[a.c] > R[b.c];
            return a.x < b.x;
          });
          sol1(v);
        } while (next_permutation(v.begin(), v.end()));
      }
      for (int j = 1; j <= 3; j++) {
        sort(p, p + n, [&](P a, P b) {
          if (a.x == b.x) return (a.c == j) < (b.c == j);
          return a.x < b.x;
        });
        sol2(j);
      }
      for (int j = 0; j < n; j++) p[j] = {p[j].y, n - p[j].x - 1, p[j].c};
    }
    printf("%d\n", ans);
    return 0;
  }
  