diff --git a/sitou/src/B146_battle.java b/sitou/src/B146_battle.java new file mode 100644 index 0000000000000000000000000000000000000000..43585029e46fcb6fd54b5b290cf066c6ad26c65e --- /dev/null +++ b/sitou/src/B146_battle.java @@ -0,0 +1,57 @@ +package src; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class B147_slime { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + final int singleAttack = sc.nextInt(); + final int allAttack = sc.nextInt(); + final int monsters = sc.nextInt(); + ArrayList monstersHPList = new ArrayList<>(); + + for (int i = 0; i < monsters; i++) { + monstersHPList.add(sc.nextInt()); + } + + int count = attackCount(monstersHPList, allAttack, singleAttack); + System.out.println(count); + sc.close(); + } + + static int attackCount(ArrayList monstersHPList, int allAttack, + int singleAttack) { + int count = 0; + + while (monstersHPList.size() > 0) { + final int maxHP = Collections.max(monstersHPList); + final int maxHPIndex = monstersHPList.indexOf(maxHP); + int allOnlyCount = (int) Math.ceil(maxHP / allAttack); + int singleOnlyCount = 0; + + for (int monsterNo : monstersHPList) { + // doubleがないと、1/10のようなときに正しく動かない + singleOnlyCount += (int) Math + .ceil((double) monstersHPList.get(monsterNo) / (double) singleAttack); + } + + if (allOnlyCount <= singleOnlyCount) { + // 全体攻撃 + monstersHPList.replaceAll(hp -> hp - allAttack); + } else { + // 最大HPの敵に単体攻撃 + monstersHPList.set(maxHPIndex, maxHP - singleAttack); + } + + count++; + // 敵がいなくなるまで続けるため + monstersHPList.removeIf(hp -> hp <= 0); + } + + return count; + } + +}