Skip to content

Instantly share code, notes, and snippets.

@halitanildonmez
Created July 20, 2020 21:12
Show Gist options
  • Save halitanildonmez/e3a9c873b00fb0e5b82b696b8fbb9ad0 to your computer and use it in GitHub Desktop.
Save halitanildonmez/e3a9c873b00fb0e5b82b696b8fbb9ad0 to your computer and use it in GitHub Desktop.
Solution for Workout problem in Google Kickstart 2020 Round A
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T = sc.nextInt();
for (int testCase = 1; testCase <= T; testCase++) {
int N = sc.nextInt();
int K = sc.nextInt();
int [] workouts = new int[N];
int diff = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
int ai = sc.nextInt();
workouts[i] = ai;
if (i > 0) {
int t = ai - workouts[i-1];
if (t > diff) {
diff = t;
}
}
}
int low = 1;
int high = diff;
while (low < high) {
int ksum = 0;
int mid = (low + high) / 2;
for (int i = 1; i < N; i++) {
int ai = workouts[i] - workouts[i-1];
double v = Math.ceil((double) ai / mid) - 1;
ksum += v;
}
if (ksum <= K) {
high = mid;
} else {
low = mid+1;
}
}
System.out.println("Case #" + testCase + ": " + low);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment