Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zadanie2;
- import java.io.*;
- import java.util.*;
- public class Zad2 {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner sc = new Scanner(new File("OD_in_ps6_czernecki.txt"));
- PrintWriter pw = new PrintWriter(new File("OD_out_ps6_czernecki.txt"));
- int n = sc.nextInt();
- int[] station = new int[n];
- for (int i = 0; i < station.length; i++) {
- station[i] = sc.nextInt();
- }
- station = Sort.Mergesort_rec(station);
- int sum = 0;
- for(int i = 0; i < station.length; ++i)
- {
- sum += Math.abs(station[station.length/2] - station[i]);
- }
- //System.out.println(sum);
- pw.print(station.length/2+1);
- pw.print(sum);
- pw.close();
- sc.close();
- }
- }
- class Sort {
- static public int[] Mergesort_rec(int[] table)
- {
- int[] Array1 = Arrays.copyOfRange(table, 0, ((table.length)/2));
- int[] Array2 = Arrays.copyOfRange(table, (table.length)/2, table.length);
- if(Array1.length > 1)
- {
- Array1 = Mergesort_rec(Array1);
- }
- if(Array2.length > 1)
- {
- Array2 = Mergesort_rec(Array2);
- }
- if(Array1.length == 0)
- {
- return Array2;
- }
- else if(Array2.length == 0)
- {
- return Array1;
- }
- else
- {
- return Merge(Array1, Array2);
- }
- }
- private static int[] Merge(int[] left, int[] right)
- {
- int i = 0;
- int j = 0;
- int i_margin = left.length;
- int j_margin = right.length;
- int[] MergeTable = new int[left.length + right.length];
- for(int k = 0; k < MergeTable.length; ++k)
- {
- if(left[i] < right[j])
- {
- MergeTable[k] = left[i];
- ++i;
- }
- else if(left[i] > right[j])
- {
- MergeTable[k] = right[j];
- j++;
- }
- if(i == i_margin) // TUTAJ sprawdzam to przepelnienie po tych ifach wyzej a powinno byc odwrotnie i wtedy bym nei musial tych petli jebac w tym ifie i nastepnym <<
- {
- for(int l = (k+1); l < MergeTable.length; ++l)
- {
- MergeTable[l] = right[j];
- j++;
- }
- break;
- }
- else if(j == j_margin)
- {
- for(int l = (k+1); l < MergeTable.length; ++l)
- {
- MergeTable[l] = left[i];
- i++;
- }
- break;
- }
- }
- return MergeTable;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement