Advertisement
bkit4s0

[CHOPSTICKS]

Jul 7th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class type {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
  8.         String[] input = b.readLine().split(" ");
  9.         int n = Integer.parseInt(input[0]);
  10.         int d = Integer.parseInt(input[1]);
  11.         int[] sticks = new int[n];
  12.         for (int i = 0; i < n; i++) {
  13.             sticks[i] = Integer.parseInt(b.readLine());
  14.         }
  15.         sort(sticks);
  16.         int result = solve(sticks, d);
  17.         System.out.println(result);
  18.     }
  19.  
  20.     private static void sort(int[] a) {
  21.         // TODO Auto-generated method stub
  22.         for (int i = 0; i < a.length - 1; i++) {
  23.             for (int j = i + 1; j < a.length; j++) {
  24.                 if (a[i] > a[j]) {
  25.                     int t = a[i];
  26.                     a[i] = a[j];
  27.                     a[j] = t;
  28.                 }
  29.             }
  30.         }
  31.     }
  32.  
  33.     private static int solve(int[] sticks, int d) {
  34.         // TODO Auto-generated method stub
  35.         int numPair = 0;
  36.         for (int i = 0; i < sticks.length - 1;) {
  37.             if (sticks[i] >= sticks[i + 1] - d) {
  38.                 numPair++;
  39.                 i += 2;
  40.             } else {
  41.                 i += 1;
  42.             }
  43.         }
  44.         return numPair;
  45.     }
  46.    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement