# [hihoCoder 1384]Genius ACM

## Description

$T\leq 12,1\leq n,m\leq 5\times 10^5,0\leq k\leq 10^{18},0\leq P_i\leq 2^{20}$

## Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5+5;
void gi(int &x) {
x = 0; char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
}

int t, n, m, p[N], a[N], kl, kr, tmp[N];
ll k;

void merge(int l, int r, int x, int y) {
int i = l, j = x, k = l;
while (i <= r && j <= y)
if (a[i] < a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
while (i <= r) tmp[k++] = a[i++];
while (j <= y) tmp[k++] = a[j++];
for (int i = l; i <= y; i++) a[i] = tmp[i];
}
bool judge(int l, int r) {
if (kl == l && r > kr) {
for (int i = kr+1; i <= r; i++) a[i] = p[i];
sort(a+kr+1, a+r+1);
merge(l, kr, kr+1, r); kr = r;
} else {
for (int i = l; i <= r; i++) a[i] = p[i];
sort(a+l, a+r+1);
kl = l, kr = r;
}
int t = 0; ll cnt = 0;
while (t < m && l < r) {
cnt += 1ll*(a[r]-a[l])*(a[r]-a[l]);
++l, ++t, --r;
if (cnt > k) return false;
}
return cnt <= k;
}
void work() {
scanf("%d%d%lld", &n, &m, &k);
for (int i = 1; i <= n; i++) gi(p[i]);
int l = 1, r = 1, ans = 0, len, k;
while (l <= n) {
len = 1, k = l;
while (len) {
if (r+len <= n && judge(l, r+len)) {
r += len;
k = r, len <<= 1;
}
else len >>= 1;
}
l = r = k+1, ++ans;
}
printf("%d\n", ans);
}
int main() {
scanf("%d", &t);
while (t--) work();
return 0;
}
posted @ 2019-07-10 23:03  NaVi_Awson  阅读(393)  评论(0编辑  收藏