๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

Algorithm242

[๋ฐฑ์ค€] 6986๋ฒˆ: ์ ˆ์‚ฌํ‰๊ท  https://www.acmicpc.net/problem/6986 6986๋ฒˆ: ์ ˆ์‚ฌํ‰๊ท  ์ฒซ์งธ ์ค„์— ์ ˆ์‚ฌํ‰๊ท (N, K)๋ฅผ, ๋‘˜์งธ ์ค„์— ๋ณด์ •ํ‰๊ท (N, K)๋ฅผ ๊ฐ๊ฐ ์†Œ์ˆ˜์ ์ดํ•˜ ์…‹์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฒฐ๊ณผ๊ฐ’์ด 9.667์ธ ๊ฒฝ์šฐ 9.67๋กœ, 5์ธ ๊ฒฝ์šฐ 5.00์œผ๋กœ, 5.5์ธ ๊ฒฝ์šฐ์—๋Š” 5.50์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Arrays; import java.util.Scanner; public class Main { public static double []dArr; public static int N; public static int K; // ์ ˆ์‚ฌ ํ‰๊ท  public static String julsa(int num) { do.. 2020. 2. 14.
[Codeforces] 791A: Bear and Big Brother https://codeforces.com/problemset/problem/791/A Problem - 791A - Codeforces codeforces.com ๋ฒ ์–ด ๋ฆฌ๋งฅ์€ ๊ณฐ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฒƒ์ด ๋˜๊ฑฐ๋‚˜, ์ ์–ด๋„ ๊ทธ์˜ ๋™์ƒ ๋ฐฅ๋ณด๋‹ค ๋” ์ปค์ง€๊ธฐ๋ฅผ ์›ํ•œ๋‹ค. ํ˜„์žฌ ๋ฆฌ๋งฅ๊ณผ ๋ฐฅ์˜ ๋ชธ๋ฌด๊ฒŒ๋Š” ๊ฐ๊ฐ a์™€ b์ด๋‹ค. ๋ฆฌ๋งฅ์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋™์ƒ์˜ ๋ชธ๋ฌด๊ฒŒ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ์„ ๋ณด์ฆํ•œ๋‹ค. ๋ฆฌ๋งฅ์€ ๋งŽ์ด ๋จน๊ณ  ๋ชธ๋ฌด๊ฒŒ๋Š” ํ•ด๋งˆ๋‹ค ์„ธ ๋ฐฐ๊ฐ€ ๋˜๋Š” ๋ฐ˜๋ฉด ๋ฐฅ์˜ ๋ชธ๋ฌด๊ฒŒ๋Š” ํ•ด๋งˆ๋‹ค ๋‘ ๋ฐฐ๊ฐ€ ๋œ๋‹ค. ๋ฆฌ๋งฅ์€ ๋ช‡ ๋…„์„ ๊ผฌ๋ฐ• ์ง€๋‚ธ ํ›„์— ๋ฐฅ๋ณด๋‹ค ์—„๊ฒฉํ•˜๊ฒŒ ๋” ์ปค์ง€๊ฒŒ ๋  ๊ฒƒ์ธ๊ฐ€? ์ž…๋ ฅ ์ž…๋ ฅ์˜ ์œ ์ผํ•œ ์„ ์€ ๊ฐ๊ฐ ๋ฆฌ๋งฅ์˜ ๋ฌด๊ฒŒ์™€ ๋ฐฅ์˜ ๋ฌด๊ฒŒ์ธ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ a์™€ b๋ฅผ ํฌํ•จํ•œ๋‹ค. ์‚ฐ์ถœ๋Ÿ‰ 1๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ธ์‡„ํ•˜๊ณ , ๊ทธ ์ดํ›„์˜ ์ •์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋ฆฌ๋งฅ์€ ๋ฐฅ๋ณด๋‹ค ์—„๊ฒฉํžˆ ์ปค์ง„๋‹ค. ๋ฉ”๋ชจ.. 2020. 2. 13.
[๋ฐฑ์ค€] 1931๋ฒˆ: ํšŒ์˜์‹ค๋ฐฐ์ •(๊ทธ๋ฆฌ๋””, ์ •๋ ฌ) https://www.acmicpc.net/problem/1931 1931๋ฒˆ: ํšŒ์˜์‹ค๋ฐฐ์ • (1,4), (5,7), (8,11), (12,14) ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int[][] arr = new int[N][2];// ํšŒ์˜ ๋ณ„ ์‹œ์ž‘, ์ข…๋ฃŒ ์‹œ๊ฐ„ ์ €์žฅํ•  ๋ฐฐ์—ด for(int i=0; i ์ข…๋ฃŒ ์‹œ๊ฐ„์ด ๊ฐ™์„ ๊ฒฝ์šฐ ์‹œ์ž‘.. 2020. 2. 12.
[๋ฐฑ์ค€] 2468๋ฒˆ: ์•ˆ์ „ ์˜์—ญ(์™„์ „ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„) https://www.acmicpc.net/problem/2468 2468๋ฒˆ: ์•ˆ์ „ ์˜์—ญ ์žฌ๋‚œ๋ฐฉ์žฌ์ฒญ์—์„œ๋Š” ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ฆฌ๋Š” ์žฅ๋งˆ์ฒ ์— ๋Œ€๋น„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ๊ณ„ํšํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ € ์–ด๋–ค ์ง€์—ญ์˜ ๋†’์ด ์ •๋ณด๋ฅผ ํŒŒ์•…ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์— ๊ทธ ์ง€์—ญ์— ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ ธ์„ ๋•Œ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ์•ˆ์ „ํ•œ ์˜์—ญ์ด ์ตœ๋Œ€๋กœ ๋ช‡ ๊ฐœ๊ฐ€ ๋งŒ๋“ค์–ด ์ง€๋Š” ์ง€๋ฅผ ์กฐ์‚ฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ, ๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, ์žฅ๋งˆ์ฒ ์— ๋‚ด๋ฆฌ๋Š” ๋น„์˜ ์–‘์— ๋”ฐ๋ผ ์ผ์ •ํ•œ ๋†’์ด ์ดํ•˜์˜ ๋ชจ๋“  ์ง€์ ์€ ๋ฌผ์— ์ž ๊ธด๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์–ด๋–ค ์ง€์—ญ์˜ ๋†’์ด ์ •๋ณด๋Š” ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ๊ฐ N์ธ 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ฃผ์–ด www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { static int map[][];// 2์ฐจ์› .. 2020. 2. 12.
[Codeforces] 1030A: In Search of an Easy Problem https://codeforces.com/problemset/problem/1030/A Problem - 1030A - Codeforces codeforces.com ์ฝ”๋ฐํฌ๋ ˆ์ด์…˜ ์ฝ”๋””๋„ค์ดํ„ฐ๋“ค์€ ํ† ๋„ˆ๋จผํŠธ๋ฅผ ์ค€๋น„ํ•  ๋•Œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ๋ฅผ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ•œ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ฝ”๋””๋„ค์ดํ„ฐ๊ฐ€ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์„ ๊ณจ๋ผ์„œ n๋ช…์˜ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๊ทธ๋“ค์˜ ์˜๊ฒฌ์— ๋Œ€ํ•ด ๋ฌผ์—ˆ๋‹ค. ์ด ๋ฌธ์ œ๊ฐ€ ์‰ฌ์šด ๊ฒƒ์ธ์ง€ ์–ด๋ ค์šด ๊ฒƒ์ธ์ง€ ๊ฐ์ž๊ฐ€ ๋Œ€๋‹ตํ–ˆ๋‹ค. ์ด n๋ช… ์ค‘ ์ ์–ด๋„ ํ•œ ๋ช…์ด ๋ฌธ์ œ๊ฐ€ ์–ด๋ ต๋‹ค๊ณ  ๋Œ€๋‹ตํ–ˆ๋‹ค๋ฉด ์กฐ์ •์ž๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐ”๊พธ๊ธฐ๋กœ ๊ฒฐ์ •ํ•œ๋‹ค. ์ฃผ์–ด์ง„ ์‘๋‹ต์— ๋Œ€ํ•ด์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์‰ฌ์šด์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋‹จ์ผ ์ •์ˆ˜ n(1 ≤100) - ์ž์‹ ์˜ ์˜๊ฒฌ์„ ์ œ์‹œํ•˜๋ผ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์€ n๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ํฌํ•จํ•˜.. 2020. 2. 12.
[Codeforces] 977A: Wrong Subtraction https://codeforces.com/problemset/problem/977/A Problem - 977A - Codeforces codeforces.com ์–ด๋ฆฐ ์†Œ๋…€ ํƒ€๋ƒ๋Š” ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ์ค„์ด๋Š” ๋ฒ•์„ ๋ฐฐ์šฐ๊ณ  ์žˆ์ง€๋งŒ, ๋‘ ์ž๋ฆฌ ํ˜น์€ ๊ทธ ์ด์ƒ์˜ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ์ˆซ์ž๋กœ ์ž˜๋ชปํ•œ๋‹ค. ํƒ€๋ƒ๋Š” ๋‹ค์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ˆซ์ž์—์„œ ํ•˜๋‚˜๋ฅผ ๋บ€๋‹ค. ์ˆซ์ž์˜ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๊ฐ€ 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ๊ทธ๋…€๋Š” ์ˆซ์ž๋ฅผ 1๋กœ ์ค„์ธ๋‹ค. ์ˆซ์ž์˜ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๊ฐ€ 0์ด๋ฉด, ๊ทธ๋…€๋Š” ์ˆซ์ž๋ฅผ 10์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค. ์ •์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค. ํƒ€๋ƒ๋Š” ๊ทธ๊ฒƒ์—์„œ ํ•˜๋‚˜๋ฅผ ๋บ„ ๊ฒƒ์ด๋‹ค. ๋‹น์‹ ์˜ ์ž„๋ฌด๋Š” ๋ชจ๋“  k์˜ ์†Œ์‚ฐ ํ›„์— ๊ฒฐ๊ณผ๋ฅผ ์ธ์‡„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ณผ๋Š” ์–‘์˜ ์ •์ˆ˜์ผ ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค. ์ž…๋ ฅ ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์€ 2๊ฐœ์˜ ์ •์ˆ˜ n๊ณผ k (109, 1 ≤kl50) - ํƒ€๋ƒ๊ฐ€ ๋นผ๋Š” ์ˆ˜์™€ ๊ทธ.. 2020. 2. 11.
[๋ฐฑ์ค€] 6603๋ฒˆ: ๋กœ๋˜(dfs, ๋ฐฑํŠธ๋ž˜ํ‚น) https://www.acmicpc.net/problem/6603 6603๋ฒˆ: ๋กœ๋˜ ๋ฌธ์ œ ๋…์ผ ๋กœ๋˜๋Š” {1, 2, ..., 49}์—์„œ ์ˆ˜ 6๊ฐœ๋ฅผ ๊ณ ๋ฅธ๋‹ค. ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์ „๋žต์€ 49๊ฐ€์ง€ ์ˆ˜ ์ค‘ k(k>6)๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ณจ๋ผ ์ง‘ํ•ฉ S๋ฅผ ๋งŒ๋“  ๋‹ค์Œ ๊ทธ ์ˆ˜๋งŒ ๊ฐ€์ง€๊ณ  ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, k=8, S={1,2,3,5,8,13,21,34}์ธ ๊ฒฝ์šฐ ์ด ์ง‘ํ•ฉ S์—์„œ ์ˆ˜๋ฅผ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์ด 28๊ฐ€์ง€์ด๋‹ค. ([1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2 www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { static int[] arr; static bool.. 2020. 2. 10.
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค[Java] - ๋น„๋ฐ€์ง€๋„ https://programmers.co.kr/learn/courses/30/lessons/17681 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ๋น„๋ฐ€์ง€๋„ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋น„๋ฐ€์ง€๋„ ๋„ค์˜ค๋Š” ํ‰์†Œ ํ”„๋กœ๋„๊ฐ€ ๋น„์ƒ๊ธˆ์„ ์ˆจ๊ฒจ๋†“๋Š” ์žฅ์†Œ๋ฅผ ์•Œ๋ ค์ค„ ๋น„๋ฐ€์ง€๋„๋ฅผ ์†์— ๋„ฃ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋น„๋ฐ€์ง€๋„๋Š” ์ˆซ์ž๋กœ ์•”ํ˜ธํ™”๋˜์–ด ์žˆ์–ด ์œ„์น˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•”ํ˜ธ๋ฅผ ํ•ด๋…ํ•ด์•ผ ํ•œ๋‹ค. ๋‹คํ–‰ํžˆ ์ง€๋„ ์•”ํ˜ธ๋ฅผ ํ•ด๋…ํ•  ๋ฐฉ๋ฒ•์„ ์ ์–ด๋†“์€ ๋ฉ”๋ชจ๋„ ํ•จ๊ป˜ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ์ง€๋„๋Š” ํ•œ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ n์ธ ์ •์‚ฌ๊ฐํ˜• ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ, ๊ฐ ์นธ์€ ๊ณต๋ฐฑ(" ) ๋˜๋Š”๋ฒฝ(#") ๋‘ ์ข…๋ฅ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ „์ฒด ์ง€๋„๋Š” ๋‘ ์žฅ์˜ ์ง€๋„๋ฅผ ๊ฒน์ณ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ ์ง€๋„ 1๊ณผ ์ง€๋„ 2๋ผ๊ณ  ํ•˜์ž. ์ง€๋„ 1 programmers.co.kr ํ‹€๋ฆฐ ์ฝ”๋“œ import java.util.Arrays; class .. 2020. 2. 9.
[๋ฐฑ์ค€] 3986๋ฒˆ: ์ข‹์€ ๋‹จ์–ด(๋ฌธ์ž์—ด, ์Šคํƒ) https://www.acmicpc.net/problem/3986 3986๋ฒˆ: ์ข‹์€ ๋‹จ์–ด ๋ฌธ์ œ ์ด๋ฒˆ ๊ณ„์ ˆํ•™๊ธฐ์— ์‹ฌ๋ฆฌํ•™ ๊ฐœ๋ก ์„ ์ˆ˜๊ฐ• ์ค‘์ธ ํ‰์„์ด๋Š” ์˜ค๋Š˜ ์ž์ •๊นŒ์ง€ ๋ณด๊ณ ์„œ๋ฅผ ์ œ์ถœํ•ด์•ผ ํ•œ๋‹ค. ๋ณด๊ณ ์„œ ์ž‘์„ฑ์ด ๋„ˆ๋ฌด ์ง€๋ฃจํ–ˆ๋˜ ํ‰์„์ด๋Š” ๋…ธํŠธ๋ถ์— ์—Ž๋“œ๋ ค์„œ ๊พธ๋ฒ…๊พธ๋ฒ… ์กธ๋‹ค๊ฐ€ ์ œ์ถœ ๋งˆ๊ฐ 1์‹œ๊ฐ„ ์ „์— ๊นจ๊ณ  ๋ง์•˜๋‹ค. ์•ˆํƒ€๊น๊ฒŒ๋„ ์ž๋Š” ๋™์•ˆ ํ‚ค๋ณด๋“œ๊ฐ€ ์ž˜๋ชป ๋ˆŒ๋ ค์„œ ๋ณด๊ณ ์„œ์˜ ๋ชจ๋“  ๊ธ€์ž๊ฐ€ A์™€ B๋กœ ๋ฐ”๋€Œ์–ด ๋ฒ„๋ ธ๋‹ค! ๊ทธ๋ž˜์„œ ํ‰์„์ด๋Š” ๋ณด๊ณ ์„œ ์ž‘์„ฑ์„ ๋•Œ๋ ค์น˜์šฐ๊ณ  ๋ณด๊ณ ์„œ์—์„œ '์ข‹์€ ๋‹จ์–ด'๋‚˜ ์„ธ๋ณด๊ธฐ๋กœ ๋งˆ์Œ ๋จน์—ˆ๋‹ค. ํ‰์„์ด๋Š” ๋‹จ์–ด ์œ„๋กœ ์•„์น˜ํ˜• ๊ณก์„ ์„ ๊ทธ์–ด ๊ฐ™์€ ๊ธ€์ž๋ผ๋ฆฌ(A๋Š” A๋ผ๋ฆฌ, B๋Š” www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; import java.util.Stack; public class Main { pu.. 2020. 2. 9.
[๋ฐฑ์ค€] 2012๋ฒˆ: ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ(๊ทธ๋ฆฌ๋””) https://www.acmicpc.net/problem/2012 2012๋ฒˆ: ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 500,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ์‚ฌ๋žŒ์˜ ์˜ˆ์ƒ ๋“ฑ์ˆ˜๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์˜ˆ์ƒ ๋“ฑ์ˆ˜๋Š” 500,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int B = 1;// ์‹ค์ œ ๋“ฑ์ˆ˜ long sum = 0;// ๋ถˆ๋งŒ๋„ ํ•ฉ ์ตœ์†Ÿ๊ฐ’ int[.. 2020. 2. 8.
๋ฐ˜์‘ํ˜•