CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:planckcons
Submission time:2016-10-06 12:55:16 +0300
Language:Java
Status:READY
Result:56
Feedback
groupverdictscore
#1ACCEPTED19
#2ACCEPTED37
#30
Test results
testverdicttimegroup
#1ACCEPTED0.14 s1details
#2ACCEPTED0.26 s2details
#3--3details

Code

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

public class Jarjestys2 {

    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++;
	}
    }	
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10
9 3 4 7 6 5 10 2 8 1

correct output
32
10 10 9 10 9 8 7 9 4 2 1 4 5 2...

user output
18 10 2 3 8 9 8 9 6 7 7 8 8 9 ...

Test 2

Group: 2

Verdict: ACCEPTED

input
1000
650 716 982 41 133 1000 876 92...

correct output
3984
207 207 206 207 128 127 126 12...

user output
1998 207 80 81 712 713 491 492...

Test 3

Group: 3

Verdict:

input
100000
94703 47808 62366 31885 7091 8...

correct output
399956
98676 98676 98675 98676 62994 ...

user output
(empty)