CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:planckcons
Submission time:2016-10-06 12:51:31 +0300
Language:Java
Status:COMPILE ERROR

Compiler report

input/Jarjestys2.java:6: error: class Jarjestys is public, should be declared in a file named Jarjestys.java
public class Jarjestys {
       ^
1 error

Code

import java.util.Scanner;
import static java.lang.System.arraycopy;
import java.util.Arrays;
import java.util.Collections;

public class Jarjestys {

    static int[] arr;
    static int n;
    static int c;    

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        int uno = 0;
        
        arr = new int[n];
       
//        n = arr.length;
        for(int i = 0; i < n; i++)
        {
            arr[i] = scan.nextInt();
            if(arr[i] == 1)
                uno = i + 1;
        }
        
        String fin = uno + " ";
        
        reverse(arr, 0, uno);
        for(int i = 0; i < arr.length; i++)
            System.out.print(arr[i]);
        System.out.println();
        for(int i = 2; i < n; i++)
        {
            fin += turner(i);      
        }
        fin += (n-1);
	System.out.println((c+2) + " " + fin);
    }
    
    public static String turner(int i)
    {
        for(int j = i - 1; j < n; j++)
        {
            if(arr[j] == i)
            {
                arr = swapD(j, arr);
                c += 2;
                return j + " " + (j+1) + " ";
            }
        }
        
        return "";
    }
	
	public static int[] swapD(int p, int[] arr1)
    {
        int[] arr2 = new int[n];
        arraycopy(arr1, p+1, arr2, p+1, n-(p + 1));
        arraycopy(arr1, 0, arr2, 1, p);
        arraycopy(arr1, p, arr2, 0, 1);
        for(int i = 0; i < arr2.length; i++)
            System.out.print(arr2[i]);
        System.out.println();
        return arr2;
    }
	
    public static void reverse(final int[] array, final int startIndexInclusive, final int endIndexExclusive) 
    {
	if (array == null) {
	    return;
	}
	int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
	int j = Math.min(array.length, endIndexExclusive) - 1;
	int tmp;
	while (j > i) {
	    tmp = array[j];
	    array[j] = array[i];
	    array[i] = tmp;
	    j--;
	    i++;
	}
    }	
}