Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code: Array

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ll> a(n);
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        vector<int> ans(n);
        for (int i = 0; i < n; ++i) {
            vector<ll> L, R;
            for (int j = i + 1; j < n; ++j) {
                if (a[j] > a[i]) {
                    ll l = (a[i] + a[j] + 2) / 2;
                    L.push_back(l);
                } else if (a[j] < a[i]) {
                    ll r = (a[i] + a[j] - 1) / 2;
                    R.push_back(r);
                }
            }
            if (L.empty() && R.empty()) {
                ans[i] = 0;
                continue;
            }
            sort(L.begin(), L.end());
            sort(R.begin(), R.end());
            vector<ll> cand = L;
            cand.insert(cand.end(), R.begin(), R.end());
            sort(cand.begin(), cand.end());
            cand.erase(unique(cand.begin(), cand.end()), cand.end());
            int best = 0;
            for (ll k : cand) {
                int cntL = upper_bound(L.begin(), L.end(), k) -
                           L.begin(); // L 中 <= k 的个数
                int cntR = R.size() - (lower_bound(R.begin(), R.end(), k) -
                                       R.begin()); // R 中 >= k 的个数
                best = max(best, cntL + cntR);
            }
            if (!L.empty()) {
                ll k = L.back() + 1;
                int cntL = upper_bound(L.begin(), L.end(), k) - L.begin();
                int cntR =
                    R.size() - (lower_bound(R.begin(), R.end(), k) - R.begin());
                best = max(best, cntL + cntR);
            }
            if (!R.empty()) {
                ll k = R.front() - 1;
                int cntL = upper_bound(L.begin(), L.end(), k) - L.begin();
                int cntR =
                    R.size() - (lower_bound(R.begin(), R.end(), k) - R.begin());
                best = max(best, cntL + cntR);
            }
            ans[i] = best;
        }
        for (int i = 0; i < n; ++i) {
            if (i)
                cout << ' ';
            cout << ans[i];
        }
        cout << '\n';
    }
    return 0;
}