結果

問題 No.2740 Old Maid
ユーザー tentententen
提出日時 2024-04-22 13:29:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,019 ms / 2,000 ms
コード長 1,892 bytes
コンパイル時間 3,960 ms
コンパイル使用メモリ 84,412 KB
実行使用メモリ 70,656 KB
最終ジャッジ日時 2024-04-22 13:30:46
合計ジャッジ時間 37,128 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 777 ms
68,916 KB
testcase_01 AC 778 ms
70,540 KB
testcase_02 AC 785 ms
68,788 KB
testcase_03 AC 780 ms
68,880 KB
testcase_04 AC 791 ms
69,520 KB
testcase_05 AC 770 ms
70,500 KB
testcase_06 AC 785 ms
69,560 KB
testcase_07 AC 870 ms
68,872 KB
testcase_08 AC 851 ms
70,656 KB
testcase_09 AC 862 ms
70,340 KB
testcase_10 AC 782 ms
70,340 KB
testcase_11 AC 725 ms
69,028 KB
testcase_12 AC 768 ms
65,492 KB
testcase_13 AC 810 ms
68,784 KB
testcase_14 AC 353 ms
48,692 KB
testcase_15 AC 398 ms
50,996 KB
testcase_16 AC 667 ms
57,112 KB
testcase_17 AC 347 ms
49,308 KB
testcase_18 AC 785 ms
67,588 KB
testcase_19 AC 483 ms
53,284 KB
testcase_20 AC 1,003 ms
68,824 KB
testcase_21 AC 398 ms
48,192 KB
testcase_22 AC 637 ms
58,144 KB
testcase_23 AC 284 ms
46,072 KB
testcase_24 AC 446 ms
53,700 KB
testcase_25 AC 1,019 ms
68,512 KB
testcase_26 AC 137 ms
41,080 KB
testcase_27 AC 248 ms
45,640 KB
testcase_28 AC 634 ms
57,112 KB
testcase_29 AC 620 ms
57,640 KB
testcase_30 AC 182 ms
41,964 KB
testcase_31 AC 977 ms
68,016 KB
testcase_32 AC 471 ms
54,524 KB
testcase_33 AC 868 ms
67,808 KB
testcase_34 AC 656 ms
57,736 KB
testcase_35 AC 466 ms
53,404 KB
testcase_36 AC 486 ms
53,120 KB
testcase_37 AC 536 ms
56,668 KB
testcase_38 AC 437 ms
50,692 KB
testcase_39 AC 324 ms
49,240 KB
testcase_40 AC 704 ms
61,828 KB
testcase_41 AC 998 ms
68,544 KB
testcase_42 AC 69 ms
37,796 KB
testcase_43 AC 70 ms
37,848 KB
testcase_44 AC 72 ms
38,012 KB
testcase_45 AC 70 ms
38,332 KB
testcase_46 AC 73 ms
37,788 KB
testcase_47 AC 69 ms
38,156 KB
testcase_48 AC 68 ms
38,164 KB
testcase_49 AC 70 ms
37,964 KB
testcase_50 AC 71 ms
37,520 KB
testcase_51 AC 69 ms
37,656 KB
testcase_52 AC 72 ms
38,028 KB
testcase_53 AC 72 ms
38,008 KB
testcase_54 AC 72 ms
37,776 KB
testcase_55 AC 71 ms
37,788 KB
testcase_56 AC 71 ms
37,772 KB
testcase_57 AC 68 ms
38,176 KB
testcase_58 AC 72 ms
38,136 KB
testcase_59 AC 72 ms
37,688 KB
testcase_60 AC 69 ms
37,948 KB
testcase_61 AC 69 ms
37,964 KB
testcase_62 AC 69 ms
38,156 KB
testcase_63 AC 68 ms
38,064 KB
testcase_64 AC 68 ms
37,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        int[] positions = new int[n];
        TreeSet<Integer> remain = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt() - 1;
            positions[values[i]] = i;
            remain.add(i);
        }
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (!remain.contains(positions[i]) || remain.last() == positions[i]) {
                continue;
            }
            int next = remain.higher(positions[i]);
            ans.add(i + 1);
            ans.add(values[next] + 1);
            remain.remove(positions[i]);
            remain.remove(next);
        }
        System.out.println(ans.stream().map(x -> x.toString()).collect(Collectors.joining(" ")));
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0