From 4961ca7e502043e1139e6a446a3ded618d271f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 9 Jul 2024 15:05:20 +0900 Subject: [PATCH 1/5] =?UTF-8?q?paiza=E3=81=AEB147=E3=81=AE=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B147_slime.java | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sitou/src/B147_slime.java diff --git a/sitou/src/B147_slime.java b/sitou/src/B147_slime.java new file mode 100644 index 0000000..abfee16 --- /dev/null +++ b/sitou/src/B147_slime.java @@ -0,0 +1,50 @@ +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 slimeNum = sc.nextInt(); + int mySize = sc.nextInt(); + ArrayList slimesSizeList = new ArrayList<>(); + for (int i = 0; i < slimeNum; i++) { + slimesSizeList.add(sc.nextInt()); + } + + int count = mergeSlimes(slimeNum, mySize, slimesSizeList); + System.out.println(count); + sc.close(); + } + + private static int mergeSlimes(int slimeNum, int mySize, ArrayList slimesSizeList) { + int count = 0; + Collections.sort(slimesSizeList, Collections.reverseOrder()); //サイズが大きいものから順に比較するため + + while (!slimesSizeList.isEmpty() && mySize <= slimesSizeList.get(0)) { //自分が最大サイズになったら終わり + int mergedIndex = findMergableSlime(mySize, slimesSizeList); + + if (mergedIndex != -1) { + mySize += slimesSizeList.get(mergedIndex); + slimesSizeList.remove(mergedIndex); + count++; + } else { + count = -1; + break; + } + } + + return count; + } + + private static int findMergableSlime(int mySize, ArrayList slimesSizeList) { + for (int i = 0; i < slimesSizeList.size(); i++) { //sizeが可変だと拡張forはできない? + スライムのsizeとリストの.sizeが紛らわしい? + if (mySize >= slimesSizeList.get(i)) { + return i; + } + } + return -1; + } +} -- GitLab From 56a8ddcb4c721bbd6fec4eb64fbea6095b415ab3 Mon Sep 17 00:00:00 2001 From: sitou Date: Tue, 9 Jul 2024 07:28:54 +0000 Subject: [PATCH 2/5] =?UTF-8?q?paiza=E3=81=AEB147=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B147_slime.java | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/sitou/src/B147_slime.java b/sitou/src/B147_slime.java index abfee16..b82e521 100644 --- a/sitou/src/B147_slime.java +++ b/sitou/src/B147_slime.java @@ -5,33 +5,38 @@ import java.util.Collections; import java.util.Scanner; public class B147_slime { + static final int failed = -1; + public static void main(String[] args) { - Scanner sc = new Scanner(System.in); + final Scanner sc = new Scanner(System.in); final int slimeNum = sc.nextInt(); - int mySize = sc.nextInt(); + final int mySize = sc.nextInt(); ArrayList slimesSizeList = new ArrayList<>(); for (int i = 0; i < slimeNum; i++) { slimesSizeList.add(sc.nextInt()); } - int count = mergeSlimes(slimeNum, mySize, slimesSizeList); + final int count = mergeSlimes(mySize, slimesSizeList); System.out.println(count); sc.close(); } - private static int mergeSlimes(int slimeNum, int mySize, ArrayList slimesSizeList) { + private static int mergeSlimes(final int mySize, final ArrayList slimesSizeList) { int count = 0; - Collections.sort(slimesSizeList, Collections.reverseOrder()); //サイズが大きいものから順に比較するため - - while (!slimesSizeList.isEmpty() && mySize <= slimesSizeList.get(0)) { //自分が最大サイズになったら終わり - int mergedIndex = findMergableSlime(mySize, slimesSizeList); - - if (mergedIndex != -1) { - mySize += slimesSizeList.get(mergedIndex); - slimesSizeList.remove(mergedIndex); + Collections.sort(slimesSizeList, Collections.reverseOrder()); // サイズが大きいものから順に比較するため + + int nowMySize = mySize; + ArrayList nowSlimesSizeList = new ArrayList<>(slimesSizeList); + + while (!nowSlimesSizeList.isEmpty() && nowMySize <= nowSlimesSizeList.get(0)) { // 自分が最大サイズになったら終わり + int mergedIndex = findMergableSlime(nowMySize, nowSlimesSizeList); + + if (mergedIndex != failed) { + nowMySize += nowSlimesSizeList.get(mergedIndex); + nowSlimesSizeList.remove(mergedIndex); count++; } else { - count = -1; + count = failed; break; } } @@ -39,12 +44,12 @@ public class B147_slime { return count; } - private static int findMergableSlime(int mySize, ArrayList slimesSizeList) { - for (int i = 0; i < slimesSizeList.size(); i++) { //sizeが可変だと拡張forはできない? + スライムのsizeとリストの.sizeが紛らわしい? - if (mySize >= slimesSizeList.get(i)) { + private static int findMergableSlime(final int nowMySize, final ArrayList nowSlimesSizeList) { + for (int i = 0; i < nowSlimesSizeList.size(); i++) { // sizeが可変だと拡張forはできない? + スライムのsizeとリストの.sizeが紛らわしい? + if (nowMySize >= nowSlimesSizeList.get(i)) { return i; } } - return -1; + return failed; } } -- GitLab From 30c6f1e99e293fd0578977e30c72a68e2d4fad4e Mon Sep 17 00:00:00 2001 From: sitou Date: Tue, 9 Jul 2024 08:18:08 +0000 Subject: [PATCH 3/5] Update B147_slime.java --- sitou/src/B147_slime.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sitou/src/B147_slime.java b/sitou/src/B147_slime.java index b82e521..0273a26 100644 --- a/sitou/src/B147_slime.java +++ b/sitou/src/B147_slime.java @@ -23,7 +23,7 @@ public class B147_slime { private static int mergeSlimes(final int mySize, final ArrayList slimesSizeList) { int count = 0; - Collections.sort(slimesSizeList, Collections.reverseOrder()); // サイズが大きいものから順に比較するため + Collections.sort(newSlimesSizeList, Collections.reverseOrder()); // サイズが大きいものから順に比較するため int nowMySize = mySize; ArrayList nowSlimesSizeList = new ArrayList<>(slimesSizeList); -- GitLab From 81a2934c232a4116f8bd9f36842ff438d57b58e6 Mon Sep 17 00:00:00 2001 From: sitou Date: Tue, 9 Jul 2024 08:20:18 +0000 Subject: [PATCH 4/5] =?UTF-8?q?B147=E3=81=AE=E4=BF=AE=E6=AD=A3=E2=91=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B147_slime.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sitou/src/B147_slime.java b/sitou/src/B147_slime.java index 0273a26..263d433 100644 --- a/sitou/src/B147_slime.java +++ b/sitou/src/B147_slime.java @@ -5,7 +5,7 @@ import java.util.Collections; import java.util.Scanner; public class B147_slime { - static final int failed = -1; + static final int FAILED = -1; public static void main(String[] args) { final Scanner sc = new Scanner(System.in); @@ -31,12 +31,12 @@ public class B147_slime { while (!nowSlimesSizeList.isEmpty() && nowMySize <= nowSlimesSizeList.get(0)) { // 自分が最大サイズになったら終わり int mergedIndex = findMergableSlime(nowMySize, nowSlimesSizeList); - if (mergedIndex != failed) { + if (mergedIndex != FAILED) { nowMySize += nowSlimesSizeList.get(mergedIndex); nowSlimesSizeList.remove(mergedIndex); count++; } else { - count = failed; + count = FAILED; break; } } @@ -50,6 +50,6 @@ public class B147_slime { return i; } } - return failed; + return FAILED; } } -- GitLab From 456ef49a813a07efbaf686001a545b39a508a08d Mon Sep 17 00:00:00 2001 From: sitou Date: Tue, 9 Jul 2024 08:28:49 +0000 Subject: [PATCH 5/5] =?UTF-8?q?paiza=E3=81=AEB147=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E2=91=A2=EF=BC=88=E4=BF=AE=E6=AD=A3=E2=91=A1=E3=81=AE?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=97=E3=83=9F=E3=82=B9=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B147_slime.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sitou/src/B147_slime.java b/sitou/src/B147_slime.java index 263d433..b84b288 100644 --- a/sitou/src/B147_slime.java +++ b/sitou/src/B147_slime.java @@ -23,10 +23,10 @@ public class B147_slime { private static int mergeSlimes(final int mySize, final ArrayList slimesSizeList) { int count = 0; - Collections.sort(newSlimesSizeList, Collections.reverseOrder()); // サイズが大きいものから順に比較するため int nowMySize = mySize; ArrayList nowSlimesSizeList = new ArrayList<>(slimesSizeList); + Collections.sort(nowSlimesSizeList, Collections.reverseOrder()); // サイズが大きいものから順に比較するため while (!nowSlimesSizeList.isEmpty() && nowMySize <= nowSlimesSizeList.get(0)) { // 自分が最大サイズになったら終わり int mergedIndex = findMergableSlime(nowMySize, nowSlimesSizeList); -- GitLab