chillurbrain

9. Коровы - в стойла.

May 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     public static void main(String args[]) {
  6.         try (PrintWriter out = new PrintWriter(System.out)) {
  7.             Scanner in = new Scanner(System.in);
  8.             int n = in.nextInt();
  9.             long k = in.nextLong();
  10.             long max = -1;
  11.             long sum = 0;
  12.             long[] a = new long[n];
  13.             a[0] = in.nextLong();
  14.  
  15.             for (int i = 1; i < n; i++) {
  16.                 a[i] = in.nextLong();
  17.                 sum += a[i] - a[i - 1];
  18.             }
  19.  
  20.             long l = 1;
  21.             long r = sum;
  22.  
  23.             while (l < r) {
  24.                 long m = (l + r) / 2;
  25.  
  26.                 int ind = 1;
  27.                 long count = 1;
  28.                 boolean curFlag = true;
  29.                 while (ind < n && count < k) {
  30.                     long curSum = 0;
  31.                     while (ind < n && curSum < m) {
  32.                         curSum += a[ind] - a[ind - 1];
  33.                         ind++;
  34.                     }
  35.                     if (curSum < m)
  36.                         curFlag = false;
  37.  
  38.                     count++;
  39.                 }
  40.                 if (count == k && curFlag) {
  41.                     l = m + 1;
  42.                     if (max < m)
  43.                         max = m;
  44.                 } else r = m;
  45.             }
  46.  
  47.             out.print(max);
  48.         } catch(Exception e) {
  49.             System.out.println("Exception: " + e);
  50.         }
  51.     }
  52. }
Add Comment
Please, Sign In to add comment