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.
Try to stop thinking about whole columns and look row by row. At a fixed height , you just have some occupied cells in a 1D row, and rightward gravity packs them into the rightmost positions.
If you decrease by , which rows actually change? Only the row at height . Everything below is untouched, everything above was already empty. So one operation affects exactly one rowβthatβs the first big crack in the problem.
For a row with occupied columns , after shifting right they end at . If you remove the cube at , cubes before it move one extra step, cubes after it end up in the same places, and the removed cube contributes nothing anymore. Simplify the change carefully.
That simplification gives a very clean formula: where is the number of columns with height at least . So after computing all suffix counts , the best operation is just Now you only still need the initial answer without deletion.
Compute the initial total by counting contributions of pairs of columns. For , column contributes movement across column exactly on heights where , and that happens times. Therefore Scan left to right with two Fenwick trees over heights: one for counts, one for sums. For current , Final answer:
At height , define a binary row:
So the row contains cubes exactly in columns where .
After gravity points right, those cubes slide to the rightmost possible columns, preserving order inside the row. If the occupied columns are
then after shifting they end up at
So the contribution of this row is
That part is standard. The real trick is what happens when we remove one cube.
If we decrease by , we remove the top cube of column , i.e. the cube at height .
That means:
So the whole operation affects only one row. Thatβs the key. If you missed it, the problem looks way nastier than it actually is.
Fix a row with occupied positions
Suppose we remove the cube at .
Originally, that cube ends at position
So its own contribution was
What about the other cubes?
Therefore the total change is
The cancels out like magic:
Now for column , the relevant row is , and in that row
where is the number of columns with height at least .
So the gain from deleting the top cube of column is simply
Thatβs hilariously small given how evil the statement looks.
So if is the initial total movement, the final answer is
We can compute all using a frequency array and suffix sums.
Now we need for the original array.
Hereβs the cleanest viewpoint: look at a pair of columns with . A cube from column contributes movement across column at exactly those heights where:
That means heights
The number of such heights is
Therefore,
So we reduced the geometry mess to a plain array sum. Much nicer.
Scan from left to right. When processing , we need
If among previous elements greater than we know:
then the added contribution is
So maintain two Fenwick trees over height values :
Each step costs .
At a fixed height , after rightward gravity, the occupied cells become the rightmost columns, where .
This is immediate: cubes move right as far as possible, cannot overlap, and keep their height.
If we delete the top cube of column , only row changes.
Below height , the column still contains cubes. At height , the cube disappears. Above that height, the column was already empty. Done.
Deleting the cube at column changes the total movement by
Proof: in row , if the removed cube is the -th occupied cube among occupied cubes, then:
Hence
The initial total movement equals
For each pair , column contributes one unit of movement across column for every height where has a cube and does not, i.e. for exactly heights. Summing over all pairs gives .
Combining Lemmas 3 and 4 gives the full formula.
For each test case:
So total complexity is
per test case, and across all test cases still
which is easily fine for .
long long. The answer can get pretty damn large.#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
struct Fenwick {
int n;
vector<ll> bit;
Fenwick() : n(0) {}
Fenwick(int n) { init(n); }
void init(int n_) {
n = n_;
bit.assign(n + 1, 0);
}
void add(int idx, ll val) {
for (; idx <= n; idx += idx & -idx) bit[idx] += val;
}
ll sumPrefix(int idx) const {
ll res = 0;
for (; idx > 0; idx -= idx & -idx) res += bit[idx];
return res;
}
ll sumRange(int l, int r) const {
if (l > r) return 0;
return sumPrefix(r) - sumPrefix(l - 1);
}
};
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n + 1);
vector<int> freq(n + 2, 0);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
++freq[a[i]];
}
// c[h] = number of columns with height >= h
vector<int> c(n + 2, 0);
for (int h = n; h >= 1; --h) c[h] = c[h + 1] + freq[h];
ll bestDelta = 0; // doing nothing is allowed
for (int i = 1; i <= n; ++i) {
ll delta = 1LL * i + c[a[i]] - n - 1;
bestDelta = max(bestDelta, delta);
}
// Initial total S = sum_{i<j} max(0, a_i - a_j)
Fenwick cntBit(n), sumBit(n);
ll base = 0;
for (int j = 1; j <= n; ++j) {
int x = a[j];
ll cntGreater = cntBit.sumRange(x + 1, n);
ll sumGreater = sumBit.sumRange(x + 1, n);
base += sumGreater - cntGreater * x;
cntBit.add(x, 1);
sumBit.add(x, x);
}
cout << base + bestDelta << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
struct Fenwick {
int n;
vector<ll> bit;
Fenwick() : n(0) {}
Fenwick(int n) { init(n); }
void init(int n_) {
n = n_;
bit.assign(n + 1, 0);
}
void add(int idx, ll val) {
for (; idx <= n; idx += idx & -idx) bit[idx] += val;
}
ll sumPrefix(int idx) const {
ll res = 0;
for (; idx > 0; idx -= idx & -idx) res += bit[idx];
return res;
}
ll sumRange(int l, int r) const {
if (l > r) return 0;
return sumPrefix(r) - sumPrefix(l - 1);
}
};
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n + 1);
vector<int> freq(n + 2, 0);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
++freq[a[i]];
}
// c[h] = number of columns with height >= h
vector<int> c(n + 2, 0);
for (int h = n; h >= 1; --h) c[h] = c[h + 1] + freq[h];
ll bestDelta = 0; // doing nothing is allowed
for (int i = 1; i <= n; ++i) {
ll delta = 1LL * i + c[a[i]] - n - 1;
bestDelta = max(bestDelta, delta);
}
// Initial total S = sum_{i<j} max(0, a_i - a_j)
Fenwick cntBit(n), sumBit(n);
ll base = 0;
for (int j = 1; j <= n; ++j) {
int x = a[j];
ll cntGreater = cntBit.sumRange(x + 1, n);
ll sumGreater = sumBit.sumRange(x + 1, n);
base += sumGreater - cntGreater * x;
cntBit.add(x, 1);
sumBit.add(x, x);
}
cout << base + bestDelta << '\n';
}
}