Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Alarm {
  4.  
  5.     public static class Pair {
  6.         long x;
  7.         long y;
  8.  
  9.         Pair(long x, long y) {
  10.             this.x = x;
  11.             this.y = y;
  12.         }
  13.     }
  14.  
  15.     static void merge(Pair arr[], int l, int m, int r)
  16.     {
  17.         int n1 = m - l + 1;
  18.         int n2 = r - m;
  19.         Pair L[] = new Pair [n1];
  20.         Pair R[] = new Pair [n2];
  21.         for (int i=0; i<n1; ++i)
  22.             L[i] = arr[l + i];
  23.         for (int j=0; j<n2; ++j)
  24.             R[j] = arr[m + 1 + j];
  25.         int i = 0, j = 0;
  26.         int k = l;
  27.         while (i < n1 && j < n2)
  28.         {
  29.             if (L[i].y < R[j].y || (L[i].y == R[j].y && L[i].x < R[j].x))
  30.             {
  31.                 arr[k] = L[i];
  32.                 i++;
  33.             }
  34.             else
  35.             {
  36.                 arr[k] = R[j];
  37.                 j++;
  38.             }
  39.             k++;
  40.         }
  41.         while (i < n1)
  42.         {
  43.             arr[k] = L[i];
  44.             i++;
  45.             k++;
  46.         }
  47.         while (j < n2)
  48.         {
  49.             arr[k] = R[j];
  50.             j++;
  51.             k++;
  52.         }
  53.     }
  54.  
  55.     static void sort(Pair arr[], int l, int r)
  56.     {
  57.         if (l < r)
  58.         {
  59.             int m = (l+r)/2;
  60.             sort(arr, l, m);
  61.             sort(arr , m+1, r);
  62.             merge(arr, l, m, r);
  63.         }
  64.     }
  65.  
  66.     public static void main(String[] args) {
  67.         Scanner in = new Scanner(System.in);
  68.         int n = in.nextInt();
  69.         long x = in.nextLong();
  70.         long k = in.nextLong();
  71.         Pair numbers[] = new Pair [n];
  72.         for (int i = 0; i < n; i++)
  73.         {
  74.             long input = in.nextLong();
  75.             Pair pair = new Pair(input, input % x);
  76.             numbers[i] = pair;
  77.         }
  78.         sort(numbers, 0, numbers.length - 1);
  79.         long lo = 0;
  80.         long hi = (long)Math.pow(10, 18);
  81.         while (lo < hi) {
  82.             long mid = (lo + hi) / 2;
  83.             long col = 0;
  84.             long last = -1;
  85.             for (Pair b : numbers) {
  86.                 if (b.y != last && mid >=b.x) {
  87.                     col += (mid - b.x) / x + 1;
  88.                     last = b.y;
  89.                 }
  90.             }
  91.             if (k <= col)
  92.                 hi = mid;
  93.             else
  94.                 lo = mid + 1;
  95.         }
  96.         System.out.println(lo);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement