SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. class Sort {
  4.     public static boolean less(int year1, int height1, int year2, int height2) {
  5.         if (year1 != year2) return (year1 > year2);
  6.         return height1 < height2;
  7.     }
  8.    
  9.     public static void merge_sort(int a[], int b[], int from, int to) {
  10.         if (from >= to) return; // base case of quick sort recursion
  11.        
  12.         int mid = (from + to) / 2;
  13.         merge_sort(a, b, from, mid);
  14.         merge_sort(a, b, mid + 1, to);
  15.        
  16.         int aa[] = new int[to - from + 1];
  17.         int bb[] = new int[to - from + 1];
  18.         // merge 2 halves to aa, bb
  19.         int i = from, j = mid + 1;
  20.         int k = 0;
  21.         while ((i <= mid) && (j <= to)) {
  22.             if (less(a[i], b[i], a[j], b[j])) {
  23.                 aa[k] = a[i]; bb[k] = b[i];
  24.                 i++; k++;        
  25.             }
  26.             else {
  27.                 aa[k] = a[j]; bb[k] = b[j];
  28.                 j++; k++;
  29.             }
  30.         }
  31.        
  32.         while (i <= mid) { aa[k] = a[i]; bb[k] = b[i]; i++; k++; }
  33.         while (j <= to) { aa[k] = a[j]; bb[k] = b[j]; j++; k++; }
  34.        
  35.         for(i = from; i <= to; ++i) {
  36.             a[i] = aa[i - from];
  37.             b[i] = bb[i - from];
  38.         }
  39.     }
  40.    
  41.     public static void main(String[] arg) {
  42.         Scanner in = new Scanner(System.in);
  43.        
  44.         int n = in.nextInt();
  45.         int a[] = new int[n], b[] = new int[n];
  46.         for(int i = 0; i < n; ++i) a[i] = in.nextInt();
  47.         for(int i = 0; i < n; ++i) b[i] = in.nextInt();
  48.        
  49.         for(int i = 0; i < 2; ++i) {
  50.             int cnt = 0;
  51.             for(int j = i; j < n; j += 2) {
  52.                 cnt++;
  53.             }
  54.            
  55.             int aa[] = new int[cnt], bb[] = new int[cnt];
  56.             int k = 0;
  57.             for(int j = i; j < n; j += 2) {
  58.                 aa[k] = a[j]; bb[k] = b[j];
  59.                 k++;
  60.             }
  61.            
  62.             merge_sort(aa, bb, 0, cnt - 1);
  63.             k = 0;
  64.             for(int j = i; j < n; j += 2) {
  65.                 a[j] = aa[k]; b[j] = bb[k];
  66.                 k++;
  67.             }
  68.         }
  69.        
  70.         for(int i = 0; i < n; ++i) System.out.println(a[i] + " " + b[i]);
  71.     }
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top