Language: C++

#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <utility>
#include <map>

using namespace std;

template <class RandomIt, class T>
ptrdiff_t num_element(RandomIt first, RandomIt last, T value, T lb_, T ub_) {
    RandomIt ub=upper_bound(first, last, make_pair(value, ub_));
    RandomIt lb=lower_bound(first ,last, make_pair(value, lb_));
    return ub-lb;
}

bool testcase_ends() {
    size_t N;
    int M;
    if (scanf("%zu %d", &N, &M) == EOF)
        return 1;

    vector<pair<int, int>> X(N);
    map<int, int> maxrem;
    for (size_t i=0; i<N; ++i) {
        scanf("%d", &X[i].first);
        X[i].second = X[i].first/M;
        X[i].first %= M;
        maxrem[X[i].first] = max(maxrem[X[i].first], X[i].second);
    }

    sort(X.begin(), X.end());

    size_t res=0;
    for (int i=0; i<=M/2; ++i) {
        ptrdiff_t upper=num_element(X.begin(), X.end(), M-i, 0, 200000);
        ptrdiff_t lower=num_element(X.begin(), X.end(), i, 0, 200000);

        if (i == M-i || i == 0) {
            res += lower/2;
            continue;
        }

        long long pairs=min(upper, lower);
        long long remain=abs(upper-lower);

        if (remain < 2) {
            res += pairs;
            continue;
        }

        int k=(lower<upper? M-i:i);
        int ub=maxrem[k];
        long long cur=0;
        for (int j=0; j<=ub&&cur<min(upper, lower); ++j)
            cur += num_element(X.begin(), X.end(), k, j, j)/2;

        res += min(cur+pairs, (upper+lower)/2);
    }

    printf("%zu\n", res);
    return 0;
}

int main() {
    bool solved=false;
    while (!testcase_ends())
        solved |= 1;

    return !solved;
}


Compiler output:
a.cc: In function 'bool testcase_ends()':
a.cc:59:19: error: no matching function for call to 'min(long long int, ptrdiff_t)'
   59 |         res += min(cur+pairs, (upper+lower)/2);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:59:19: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'ptrdiff_t' {aka 'long int'})
   59 |         res += min(cur+pairs, (upper+lower)/2);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:59:19: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   59 |         res += min(cur+pairs, (upper+lower)/2);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~