diff --git a/yitou/B114.java b/yitou/B114.java new file mode 100644 index 0000000000000000000000000000000000000000..9b5c7108faaf21a272695f9c70c9115738a97d47 --- /dev/null +++ b/yitou/B114.java @@ -0,0 +1,77 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +/** + * 選手の試技結果に基づいて順位付けするプログラムです. + */ +public class B114 { + + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + + // 試技回数と選手数の入力 + final int tryNum = scanner.nextInt(); + final int playerNum = scanner.nextInt(); + + // 各選手の試技結果 + final List> scores = readScores(scanner, playerNum, tryNum); + + // 選手を降順でソートしてインデックスを取得 + final List rankedPlayers = rankPlayers(scores, playerNum); + + // 1位の選手のインデックス出力 + System.out.println(rankedPlayers.get(0) + 1); + + scanner.close(); + } + + /** + * 選手のスコアを昇順にソートしたリストを返す. + */ + private static List> readScores(final Scanner scanner, final int playerNum, final int tryNum) { + final List> scores = new ArrayList<>(); + + for (int i = 0; i < playerNum; i++) { + final List playerScores = new ArrayList<>(); + for (int j = 0; j < tryNum; j++) { + playerScores.add(scanner.nextInt()); + } + Collections.sort(playerScores); + scores.add(playerScores); + } + + return scores; + } + + /** + * 降順にソート. + * + * @param scores 各プレイヤーのスコアのリスト。リストの各要素はプレイヤーのスコアのリストです. + * @param playerNum プレイヤーの数. + * @return スコアに基づいて降順にソートされたプレイヤーのインデックスのリスト. + */ + private static List rankPlayers(final List> scores, final int playerNum) { + final List idxList = new ArrayList<>(); + for (int i = 0; i < playerNum; i++) { + idxList.add(i); // 選手のインデックスを保持 + } + + // スコアに基づいて降順でソート + Collections.sort(idxList, (a, b) -> { + List scoresA = scores.get(a); + List scoresB = scores.get(b); + + for (int i = 0; i < Math.min(scoresA.size(), scoresB.size()); i++) { + int comparison = scoresB.get(i).compareTo(scoresA.get(i)); + if (comparison != 0) { + return comparison; // 降順にする + } + } + // サイズの比較を削除 + return 0; // 同じスコアの場合はインデックス順 + }); + return idxList; + } +} \ No newline at end of file