Language: C++

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> f(multiset<int> &ms) {
  vector<pair<int, int>> ret;
  for (auto it = ms.begin(); it != ms.end(); it++) {
    if (ret.size() == 0 || ret.back().first == *it) {
      ret.emplace_back(make_pair(*it, 1));
    }
    else {
      ret.back().second++;
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int n;
  cin >> n;
  
  vector<int> a(3 * n);
  vector<vector<int>> x(n); // x[i]: iの出現位置
  vector<int> xit(n, 0);
  for (int i = 0; i < n; i++) x[i].emplace_back(-1); // sentinel
  
  for (int i = 0; i < 3 * n; i++) {
    cin >> a.at(i);
    a.at(i)--;
    x[a.at(i)].emplace_back(i);
  }
  
  for (int i = 0; i < n; i++) {
    x[i].emplace_back(1e9); // sentinel
    x[i].emplace_back(1e9); // sentinel
  }
  
  multiset<int> ms;
  ms.insert(a[0]);
  ms.insert(a[1]);
  xit[a[0]]++;
  xit[a[1]]++;
  
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    // 3i-1, 3i, 3i+1番目を取る
    ms.insert(a[3 * i - 1]);
    ms.insert(a[3 * i]);
    ms.insert(a[3 * i + 1]);
    xit[a[3 * i - 1]]++;
    xit[a[3 * i]]++;
    xit[a[3 * i + 1]]++;
    
    auto vp = f(ms);
    
    bool three = false;
    for (int j = 0; j < vp.size(); j++) {
      if (vp[j].second >= 3) {
        ans++;
        vp[j].second -= 3;
        three = true;
        ms.erase(vp[j].first);
      }
    }
    if (three) continue;
    
    vector<tuple<int, int, int>> next; // 次に揃う位置, 番号, 枚数
    for (int j = 0; j < vp.size(); j++) {
      if (vp[j].second == 0) continue;
      else if (vp[j].second == 1) {
        //if (x[xit[vp[j].first] + 2] != 1e9) {
          next.emplace_back(make_tuple(x[xit[vp[j].first] + 2], vp[j].first), vp[j].second);
        //}
      }
      else if (vp[j].second == 2) {
        //if (x[xit[vp[j].first] + 1] != 1e9) {
          next.emplace_back(make_tuple(x[xit[vp[j].first] + 1], vp[j].first), vp[j].second);
        //}
      }
    }
    sort(next.begin(), next.end());
    int choice1 = get<1>(next[0]), choice2;
    if (get<2>(next[0]) >= 2) choice2 = choice1;
    else choice2 = get<1>(next[1]);
    multiset<int> ms2;
    ms2.insert(choice1);
    ms2.insert(choice2);
    ms = ms2;
  }
  
  vector<int> last;
  for (auto it = ms.begin(); it != ms.end(); it++) {
    last.emplace_back(*it);
  }
  
  if (last[0] == last[2]) ans++;
  
  cout << ans << '\n';
}

Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::tuple<int, int, int>; _Args = {std::tuple<std::vector<int, std::allocator<int> >, int>, int&}; _Tp = std::tuple<int, int, int>]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::tuple<int, int, int>; _Args = {std::tuple<std::vector<int, std::allocator<int> >, int>, int&}; _Tp = std::tuple<int, int, int>; allocator_type = std::allocator<std::tuple<int, int, int> >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::tuple<std::vector<int, std::allocator<int> >, int>, int&}; _Tp = std::tuple<int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int> >; reference = std::tuple<int, int, int>&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:74:28:   required from here
   74 |           next.emplace_back(make_tuple(x[xit[vp[j].first] + 2], vp[j].first), vp[j].second);
      |           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::tuple<int, int, int>::tuple(std::tuple<std::vector<int, std::allocator<int> >, int>, int&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68:
/usr/include/c++/14/tuple:1646:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_Args2 ...>&&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1646 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1646:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1634:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_Args2 ...>&&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1634 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1634:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1623:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_explicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_Args2 ...>&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1623 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1623:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1611:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_implicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_Args2 ...>&) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<const _UElements& ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1611 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1611:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1603:9: note: candidate: 'template<class _Alloc> std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, std::tuple< <template-parameter-1-1> >&&) [with _Elements = {int, int, int}]'
 1603 |         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:1603:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1598:9: note: candidate: 'template<class _Alloc> std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple< <template-parameter-1-1> >&) [with _Elements = {int, int, int}]'
 1598 |         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:1598:9: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/tuple:1591:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, _UElements&& ...) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_explicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1591 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1591:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: error: no matching function for call to 'std::tuple<int, int, int>::__valid_args<>()'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1390:31: note: candidate: 'template<class _Up> static constexpr bool std::tuple< <template-parameter-1-1> >::__valid_args() [with _Elements = {int, int, int}]'
 1390 |         static constexpr bool __valid_args()
      |                               ^~~~~~~~~~~~
/usr/include/c++/14/tuple:1390:31: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: note:   couldn't deduce template parameter '_Up'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1398:31: note: candidate: 'template<class, class, class ... _Tail> static constexpr bool std::tuple< <template-parameter-1-1> >::__valid_args() [with _Tail = <template-parameter-1-1>; _Elements = {int, int, int}]'
 1398 |         static constexpr bool __valid_args()
      |                               ^~~~~~~~~~~~
/usr/include/c++/14/tuple:1398:31: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1587:59: note:   couldn't deduce template parameter '<template-parameter-1-1>'
 1587 |                  bool _Valid = __valid_args<_UElements...>(),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1581:9: note: candidate: 'template<class _Alloc, class ... _UElements, bool _Valid, typename std::enable_if<std::tuple<int, int, int>::_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> > std::tuple< <template-parameter-1-1> >::tuple(std::allocator_arg_t, const _Alloc&, _UElements&& ...) [with _UElements = _Alloc; bool _Valid = {_UElements ...}; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = _Valid; _Elements = {int, int, int}]'
 1581 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:1581:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1578:57: error: no matching function for call to 'std::tuple<int, int, int>::__valid_args<>()'
 1578 |                bool _Valid = __valid_args<_UElements...>(),
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/14/tuple:1390:31: note: candidate: 'template<class _Up> static constexpr bool std::tuple< <template-parameter-1-1> >::__valid_args() [with _Elements = {int, int, int}]'
 1390 |         static constexpr bool __valid_args()
      |                               ^~~~~~~~~~~~
/usr/include/c++/14/tuple:1390:31: note:   template argument deduction/substitution failed:
/usr/include/c++/14/tuple:1578:57: note:   couldn't deduce template parameter '_Up'
 1578 |                bool _Valid = __valid_args<_UElements...>(),
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~