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 INF = 1e9 + 3;
  vector<pair<int, int> > px[3];
  vector<pair<int, int> > py[3];
  int ctx(int c, int x) {
    return lower_bound(px[c].begin(), px[c].end(), pair<int, int>{x, INF}) -
           px[c].begin();
  }
  int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    int h = n / 3;
    for (int i = 0; i < n; i++) {
      int x, y, c;
      cin >> x >> y >> c;
      px[c - 1].push_back({x, y});
      py[c - 1].push_back({y, x});
    }
    int fans = 0;
    for (int di = 0; di < 4; di++) {
      for (int c = 0; c < 3; c++) {
        sort(px[c].begin(), px[c].end());
        sort(py[c].begin(), py[c].end());
      }
      int st = 1, en = h;
      while (st < en) {
        int mi = (st + en + 1) / 2;
        int p[3] = {0, 1, 2};
        bool yes = 0;
        do {
          bool legit = 1;
          int cx = -INF - 1;
          for (int i = 0; i < 3; i++) {
            int curpos = ctx(p[i], cx);
            if (curpos + mi > h) {
              legit = 0;
              break;
            }
            cx = px[p[i]][curpos + mi - 1].first;
          }
          if (legit) {
            yes = 1;
            break;
          }
        } while (next_permutation(p, p + 3));
        if (yes) {
          st = mi;
          continue;
        }
        do {
          bool legit = 1;
          int cx = px[p[0]][mi - 1].first;
          int ny = 0, cy1 = INF, cy2 = -INF;
          for (int j = 0; j < h; j++) {
            pair<int, int> pt = py[p[1]][j];
            if (pt.second <= cx) continue;
            ny++;
            if (ny >= mi) {
              cy1 = pt.first;
              break;
            }
          }
          ny = 0;
          for (int j = h - 1; j >= 0; j--) {
            pair<int, int> pt = py[p[2]][j];
            if (pt.second <= cx) continue;
            ny++;
            if (ny >= mi) {
              cy2 = pt.first;
              break;
            }
          }
          legit = cy1 < cy2;
          if (legit) {
            yes = 1;
            break;
          }
        } while (next_permutation(p, p + 3));
        if (yes) {
          st = mi;
        } else {
          en = mi - 1;
        }
      }
      fans = max(fans, st);
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < h; j++) {
          pair<int, int> &pt = px[i][j];
          pt = {-pt.second, pt.first};
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < h; j++) {
          pair<int, int> &pt = py[i][j];
          pt = {pt.second, -pt.first};
        }
      }
    }
    cout << fans * 3;
  }
  