chillurbrain

7. Субботник

May 25th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     static long[] a;
  5.     static int n;
  6.     static int r;
  7.     static int c;
  8.  
  9.     static boolean isSumAllowed(long curMaxDiff) {
  10.         boolean success;
  11.  
  12.         int groupsFound = 0;
  13.         int start = 0;
  14.         int end = 0;
  15.         int count = 1;
  16.  
  17.         while (end < n - 1) {
  18.             long curDiff = a[end + 1] - a[start];
  19.             if (curDiff <= curMaxDiff) {
  20.                 end++;
  21.                 count++;
  22.             } else while (a[end + 1] - a[start] > curMaxDiff) {
  23.                 start++;
  24.                 count--;
  25.             }
  26.  
  27.             if (count >= c) {
  28.                 groupsFound++;
  29.                 start = ++end;
  30.                 count = 1;
  31.             }
  32.  
  33.             if (groupsFound == r)
  34.                 break;
  35.         }
  36.  
  37.         success = groupsFound >= r;
  38.         return success;
  39.     }
  40.  
  41.     public static void main(String args[]) {
  42.         Scanner in = new Scanner(System.in);
  43.         n = in.nextInt();
  44.         r = in.nextInt();
  45.         c = in.nextInt();
  46.         a = new long[n];
  47.  
  48.         for (int i = 0; i < n; i++) a[i] = in.nextLong();
  49.  
  50.         if (c == 1) {
  51.             System.out.println(0);
  52.             return;
  53.         }
  54.  
  55.         Arrays.sort(a);
  56.  
  57.         long left = 0;
  58.         long right = a[n - 1] - a[0];
  59.  
  60.         while (left < right) {
  61.             long m = (left + right) / 2;
  62.             boolean curSumAllowed = isSumAllowed(m);
  63.             if (curSumAllowed)
  64.                 right = m;
  65.             else
  66.                 left = m + 1;
  67.         }
  68.         System.out.print(right);
  69.     }
Add Comment
Please, Sign In to add comment