1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| const int maxn = 500000 + 10; ll dat[maxn], a[maxn], b[maxn]; int n, m; ll T;
inline ll sq(ll x) { return x*x; }
void init() { memset(dat, 0, sizeof dat); memset(a, 0, sizeof a); memset(b, 0, sizeof b); }
inline void merge(int L, int mid, int R) { int i = L, j = mid+1; _rep(k, L, R) { if (j > R || (i <= mid && a[i] <= a[j])) b[k] = a[i++]; else b[k] = a[j++]; } }
inline ll calc(int L, int r, int R) { if (R > n) R = n; int tot = min(m, (R-L+1)>>1);
// a[r+1...R] is new segment _rep(i, r+1, R) a[i] = dat[i]; sort(a+r+1, a+R+1); merge(L, r, R);
// quick sort + merge sort ==> b[...] sorted ll val = 0; _for(i, 0, tot) val += sq(b[R-i] - b[L+i]); return val; }
void solve(int &ans) { int L = 1, R = 1; a[1] = dat[1];
while (L <= n) { int p = 1; while (p) { ll val = calc(L, R, R+p); if (val <= T) { // valid, update a[...] R = min(R+p, n); _rep(i, L, R) a[i] = b[i]; if (R >= n) break; p <<= 1; } else p >>= 1; }
ans++; L = R+1; } }
int main() { freopen("input.txt", "r", stdin); int kase; cin >> kase;
while (kase--) { init();
// get data scanf("%d%d%lld", &n, &m, &T); _rep(i, 1, n) scanf("%lld", &dat[i]);
// then solve int ans = 0; solve(ans); printf("%d\n", ans); } }
|