Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Math;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- */
- public class Main {
- public static int[] quicksort(int[] array, int inicio, int fim) {
- if (inicio < fim) {
- int pivot = partition(array, inicio, fim);
- quicksort(array, inicio, pivot - 1);
- quicksort(array, pivot + 1, fim);
- }
- return array;
- }
- public static int partition(int[] a, int lo, int hi) {
- int pivot = a[hi];
- int i = lo - 1;
- for (int j = lo; j <= hi - 1; j++) {
- if (a[j] <= pivot) {
- i += 1;
- swap(a, i, j);
- }
- }
- swap(a, i + 1, hi);
- return i + 1;
- }
- public static void swap(int[] array, int i, int j) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int test_cases = input.nextInt();
- int[] casas;
- int test_complete = 0;
- int array_size;
- while (test_complete < test_cases) {
- array_size = input.nextInt();
- casas = new int[array_size];
- for (int i = 0; i < casas.length; i++) {
- casas[i] = input.nextInt();
- }
- quicksort(casas, 0, casas.length - 1);
- int middle_home = 0;
- if (casas.length % 2 == 0) {
- middle_home = (casas.length / 2) - 1;
- }
- if (casas.length % 2 == 1) {
- middle_home = (int) Math.floor((casas.length / 2));
- }
- int distance = 0;
- int position = casas.length - 1;
- while (position > middle_home) {
- distance += (casas[position] - casas[middle_home]);
- position--;
- //System.out.println("distance: " + distance);
- }
- position = 0;
- while (position < middle_home) {
- distance += (casas[middle_home] - casas[position]);
- position++;
- //System.out.println("distance:" + distance);
- }
- for (int i = 0; i < casas.length; i++) {
- //System.out.println(casas[i] + " ");
- }
- System.out.println(distance);
- test_complete++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement