AcWing 109. 天才ACM(Java版)
原题链接
困难
作者:
上杉
,
2021-04-28 13:33:49
,
所有人可见
,
阅读 420
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int N;
static int M;
static long T;
static long[] arr;
static long[] copy;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(bf.readLine());
while (K-- > 0){
String[] opl = bf.readLine().split(" ");
N = Integer.parseInt(opl[0]);
M = Integer.parseInt(opl[1]);
T = Long.parseLong(opl[2]);
arr = new long[N];copy = new long[N];
String[] numString = bf.readLine().split(" ");
for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(numString[i]);
int count = 0;
int l = 0;
while (l < N){
int r = l;
int p = 1;
while (r < N && p > 0){
long sum = cal(l, r + p);
if (sum > T){
p /= 2;
}else {
r += p;
p *= 2;
}
}
count++;
l = r + 1;
}
System.out.println(count);
}
}
private static long cal(int l, int r) {
if (r >= N)return Long.MAX_VALUE;
for (int i = l; i <=r; i++) {
copy[i] = arr[i];
}
Arrays.sort(copy,l,r + 1);
long sum = 0;
int cnt = 0;
while (l < r && cnt < M){
sum += 1L * (copy[r] - copy[l]) * (copy[r] - copy[l]);
r--;l++;
cnt++;
}
return sum;
}
}