Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For a fixed , draw and one later value on a number line. For which side of their midpoint is closer to than is?
The inequality becomes simple: if , then ; if , then . If , it never works.
As moves from to , later elements smaller than stop being counted, while later elements larger than start being counted. So this is a sweep over thresholds.
For fixed , all thresholds are ordered exactly like the values , because increases with . So sort the suffix values and sweep them.
Start with , the count for very small . Then process sorted suffix values: subtract for , add for , ignore equals, and take the maximum ever reaches.
Fix an index and call . For a later value , we need
That means is closer to than to . The boundary is their midpoint.
If , then wins when
If , then wins when
If , it never wins, because the distances are always equal. Brutal, but fair.
Imagine moving from very small to very large.
At , every later value is counted, and every later value is not counted.
When crosses the midpoint with some :
So for each fixed , the answer is just the maximum active count during this sweep.
Since is fixed, the midpoint
is ordered the same way as . Therefore we can sort the suffix values and process them in increasing order.
For each :
This is the count for very small .
The integer restriction on does not ruin this. The count only changes at midpoint thresholds, and the best sweep state can be achieved by choosing an integer sufficiently far left/right or between relevant threshold groups.
For each , sorting the suffix costs . Overall:
per test case, with extra memory. Since
this easily passes.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n);
for (ll &x : a) cin >> x;
vector<int> ans(n, 0);
for (int i = 0; i < n; i++) {
vector<ll> b;
b.reserve(n - i - 1);
for (int j = i + 1; j < n; j++) b.push_back(a[j]);
sort(b.begin(), b.end());
int cur = 0;
for (ll y : b) {
if (y < a[i]) cur++;
}
ans[i] = cur;
for (ll y : b) {
if (y < a[i]) cur--;
else if (y > a[i]) cur++;
ans[i] = max(ans[i], cur);
}
}
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n);
for (ll &x : a) cin >> x;
vector<int> ans(n, 0);
for (int i = 0; i < n; i++) {
vector<ll> b;
b.reserve(n - i - 1);
for (int j = i + 1; j < n; j++) b.push_back(a[j]);
sort(b.begin(), b.end());
int cur = 0;
for (ll y : b) {
if (y < a[i]) cur++;
}
ans[i] = cur;
for (ll y : b) {
if (y < a[i]) cur--;
else if (y > a[i]) cur++;
ans[i] = max(ans[i], cur);
}
}
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
return 0;
}