Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static long[] a;
- static int n;
- static int r;
- static int c;
- static boolean isSumAllowed(long curMaxDiff) {
- boolean success;
- int groupsFound = 0;
- int start = 0;
- int end = 0;
- int count = 1;
- while (end < n - 1) {
- long curDiff = a[end + 1] - a[start];
- if (curDiff <= curMaxDiff) {
- end++;
- count++;
- } else while (a[end + 1] - a[start] > curMaxDiff) {
- start++;
- count--;
- }
- if (count >= c) {
- groupsFound++;
- start = ++end;
- count = 1;
- }
- if (groupsFound == r)
- break;
- }
- success = groupsFound >= r;
- return success;
- }
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- n = in.nextInt();
- r = in.nextInt();
- c = in.nextInt();
- a = new long[n];
- for (int i = 0; i < n; i++) a[i] = in.nextLong();
- if (c == 1) {
- System.out.println(0);
- return;
- }
- Arrays.sort(a);
- long left = 0;
- long right = a[n - 1] - a[0];
- while (left < right) {
- long m = (left + right) / 2;
- boolean curSumAllowed = isSumAllowed(m);
- if (curSumAllowed)
- right = m;
- else
- left = m + 1;
- }
- System.out.print(right);
- }
Add Comment
Please, Sign In to add comment