Submission #156632


Source Code Expand

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

public class Main {
    static BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);
    
    public static void main(String[] args) throws Exception {

        try {

            String s = solve();
            pw.write(s);
        } finally {
            pw.close();
            br.close();
        }
    
    }

    private static String solve() throws Exception {
        String res = null;

        int n = Integer.parseInt(br.readLine());
        
        int[] array = new int[n + 1];
        for (int i = 0; i < n; i++) {
            array[i] = Integer.parseInt(br.readLine());
        }

        int[] sortArray = Arrays.copyOf(array, n + 1);
        
        Arrays.sort(sortArray);
        
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= n; j++) {
                if (array[i] == sortArray[j]) {
                    array[i] = j;
                }
            }
        }
        
        
        int count = 0;

        
        for (int i = 0; i < n - 2; i++) {
            if (array[i] != array[i + 1] - 1 &&  array[i] == array[i + 2] - 2) {
                count++;
                int aPos = -1;
                int aNum = -1;
                for (int j = 0; j < n; j++) {
                    if (array[j] == array[i] + 1) {
                        count ++;
                        aPos = j;
                        aNum = array[i] + 1;
                        break;
                    }
                }
                if (aPos == -1 ) {
                    throw new RuntimeException();
                }
                
                if (i + 1 > aPos) {
                    for (int j = aPos; j > aPos; j --) {
                        array[j] = array[j - 1];
                    }
                } else {
                    for (int j = aPos; j < aPos; j ++) {
                        array[j] = array[j + 1];
                    }
                }
                array[aPos] = aNum;
            }
        }

        int maxLen = 0;
        int maxPos = 0;
        int len = 0;
        for (int i = 0; i < n - 1; i++) {
            if (array[i] == array[i + 1] - 1) {
                if (i - maxLen == maxPos) {
                    maxLen++;
                } else {
                    len++;
                    if (len > maxLen) {
                        maxPos = i - len;
                        maxLen = len;
                    }
                }
            } else {
                len = 0;
            }
        }
        
//        int minPos = 0;
        boolean isUpdate = true;
        for (int minPos = 0; minPos < n ; minPos++) {
            isUpdate = false;
            int min = array[minPos];
            int rMinPos = minPos;
            for (int i = minPos + 1; i < n; i++) {
                if (min > array[i] && (!( i > maxPos && i < maxPos + maxLen))) {
                    isUpdate = true;
                    min = array[i];
                    rMinPos = i;
                }
            }
            if (isUpdate) {
                count++;
                for (int i = rMinPos; i > minPos; i --) {
                    array[i] = array[i - 1];
                }
            }
            
        }
        
        
        
        res = String.valueOf(Math.max(0, count - maxLen));
        return res;
    }
 

   
}

Submission Info

Submission Time
Task D - トランプ挿入ソート
User tochukaso
Language Java (OpenJDK 1.7.0)
Score 0
Code Size 3642 Byte
Status WA
Exec Time 2041 ms
Memory 28684 KB

Judge Result

Set Name smallA smallB all
Score / Max Score 0 / 10 0 / 40 0 / 50
Status
AC × 9
WA × 10
AC × 13
WA × 24
AC × 15
WA × 28
TLE × 12
Set Name Test Cases
smallA test_01_ABC.txt, test_04_ABC.txt, test_07_ABC.txt, test_10_ABC.txt, test_13_ABC.txt, test_16_ABC.txt, test_19_ABC.txt, test_22_ABC.txt, test_25_ABC.txt, test_28_ABC.txt, test_31_ABC.txt, test_32_ABC.txt, test_35_ABC.txt, test_38_ABC.txt, test_41_ABC.txt, test_44_ABC.txt, test_47_ABC.txt, test_50_ABC.txt, test_53_ABC.txt
smallB test_01_ABC.txt, test_02_AB.txt, test_04_ABC.txt, test_05_AB.txt, test_07_ABC.txt, test_08_AB.txt, test_10_ABC.txt, test_11_AB.txt, test_13_ABC.txt, test_14_AB.txt, test_16_ABC.txt, test_17_AB.txt, test_19_ABC.txt, test_20_AB.txt, test_22_ABC.txt, test_23_AB.txt, test_25_ABC.txt, test_26_AB.txt, test_28_ABC.txt, test_29_AB.txt, test_31_ABC.txt, test_32_ABC.txt, test_33_AB.txt, test_35_ABC.txt, test_36_AB.txt, test_38_ABC.txt, test_39_AB.txt, test_41_ABC.txt, test_42_AB.txt, test_44_ABC.txt, test_45_AB.txt, test_47_ABC.txt, test_48_AB.txt, test_50_ABC.txt, test_51_AB.txt, test_53_ABC.txt, test_54_AB.txt
all test_01_ABC.txt, test_02_AB.txt, test_03_A.txt, test_04_ABC.txt, test_05_AB.txt, test_06_A.txt, test_07_ABC.txt, test_08_AB.txt, test_09_A.txt, test_10_ABC.txt, test_11_AB.txt, test_12_A.txt, test_13_ABC.txt, test_14_AB.txt, test_15_A.txt, test_16_ABC.txt, test_17_AB.txt, test_18_A.txt, test_19_ABC.txt, test_20_AB.txt, test_21_A.txt, test_22_ABC.txt, test_23_AB.txt, test_24_A.txt, test_25_ABC.txt, test_26_AB.txt, test_27_A.txt, test_28_ABC.txt, test_29_AB.txt, test_30_A.txt, test_31_ABC.txt, test_32_ABC.txt, test_33_AB.txt, test_34_A.txt, test_35_ABC.txt, test_36_AB.txt, test_37_A.txt, test_38_ABC.txt, test_39_AB.txt, test_40_A.txt, test_41_ABC.txt, test_42_AB.txt, test_43_A.txt, test_44_ABC.txt, test_45_AB.txt, test_46_A.txt, test_47_ABC.txt, test_48_AB.txt, test_49_A.txt, test_50_ABC.txt, test_51_AB.txt, test_52_A.txt, test_53_ABC.txt, test_54_AB.txt, test_55_A.txt
Case Name Status Exec Time Memory
sample_01.txt AC 441 ms 20784 KB
sample_02.txt AC 423 ms 20788 KB
sample_03.txt AC 396 ms 20788 KB
test_01_ABC.txt AC 391 ms 20784 KB
test_02_AB.txt WA 465 ms 23132 KB
test_03_A.txt TLE 2039 ms 28380 KB
test_04_ABC.txt AC 393 ms 20788 KB
test_05_AB.txt WA 477 ms 22752 KB
test_06_A.txt WA 542 ms 27012 KB
test_07_ABC.txt WA 397 ms 20872 KB
test_08_AB.txt WA 456 ms 23072 KB
test_09_A.txt TLE 2040 ms 28088 KB
test_10_ABC.txt WA 398 ms 20784 KB
test_11_AB.txt WA 414 ms 21608 KB
test_12_A.txt WA 1454 ms 26444 KB
test_13_ABC.txt WA 392 ms 20660 KB
test_14_AB.txt WA 448 ms 23000 KB
test_15_A.txt TLE 2041 ms 26604 KB
test_16_ABC.txt WA 407 ms 20780 KB
test_17_AB.txt WA 440 ms 22916 KB
test_18_A.txt WA 491 ms 26328 KB
test_19_ABC.txt WA 403 ms 20788 KB
test_20_AB.txt WA 454 ms 23040 KB
test_21_A.txt TLE 2040 ms 26580 KB
test_22_ABC.txt WA 410 ms 20788 KB
test_23_AB.txt WA 468 ms 22752 KB
test_24_A.txt TLE 2039 ms 26104 KB
test_25_ABC.txt WA 398 ms 20788 KB
test_26_AB.txt WA 443 ms 23004 KB
test_27_A.txt TLE 2040 ms 28272 KB
test_28_ABC.txt WA 399 ms 20788 KB
test_29_AB.txt WA 446 ms 23004 KB
test_30_A.txt TLE 2039 ms 25816 KB
test_31_ABC.txt AC 398 ms 20780 KB
test_32_ABC.txt AC 397 ms 20756 KB
test_33_AB.txt AC 440 ms 22876 KB
test_34_A.txt TLE 2039 ms 27932 KB
test_35_ABC.txt AC 399 ms 20756 KB
test_36_AB.txt AC 493 ms 22744 KB
test_37_A.txt AC 567 ms 24416 KB
test_38_ABC.txt AC 395 ms 20760 KB
test_39_AB.txt AC 458 ms 23000 KB
test_40_A.txt TLE 2039 ms 28088 KB
test_41_ABC.txt AC 408 ms 20792 KB
test_42_AB.txt AC 443 ms 22872 KB
test_43_A.txt AC 526 ms 24376 KB
test_44_ABC.txt WA 397 ms 20764 KB
test_45_AB.txt WA 521 ms 23396 KB
test_46_A.txt TLE 2041 ms 28684 KB
test_47_ABC.txt WA 397 ms 20788 KB
test_48_AB.txt WA 408 ms 21100 KB
test_49_A.txt WA 568 ms 27000 KB
test_50_ABC.txt AC 394 ms 20788 KB
test_51_AB.txt WA 467 ms 22960 KB
test_52_A.txt TLE 2039 ms 26476 KB
test_53_ABC.txt AC 399 ms 20788 KB
test_54_AB.txt WA 411 ms 21096 KB
test_55_A.txt TLE 2041 ms 26268 KB