[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ณผ์ œ 4. 1000๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ํ€ต ์ •๋ ฌ ์‹œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 3. 28. 10:42

Quick sort '์•Œ๊ณ ๋ฆฌ์ฆ˜' ์ „๊ณต ์ˆ˜์—…์‹œ๊ฐ„์— ๋‚˜์˜จ ๊ณผ์ œ์ธ '1000๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ํ›„ ํ•ด์‹œ ๊ฐ’ ๊ตฌํ•˜๊ธฐ'๋ฅผ ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ด๋‹ค. ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ตฌํ˜„๋˜๋Š” ์ •๋ ฌ ๋ฐฉ๋ฒ• ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ตœ์•… : O(n^2) ํ‰๊ท  : O(nlogn) ์„ค๊ณ„๋ฅผ ํ†ตํ•ด์„œ ๊ฐœ์„  ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„ ๊ตฌํ˜„ ๊ณผ์ • pivot์„ ์„ ํƒํ•œ๋‹ค. ์œ„์ชฝ ํฌ์ธํ„ฐ๊ฐ€ ๋‚ฎ์€ ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค. ์•„๋ž˜์ชฝ ํฌ์ธํ„ฐ๊ฐ€ ๋†’์€ ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค. ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์š”์†Œ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. 2-4๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ต์ฐจํ•œ๋‹ค๋ฉด ํ˜„์žฌ ํ”ผ๋ฒ—๊ณผ ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์„ ๊ตํ™˜ํ•œ๋‹ค. ํ”ผ๋ฒ—๊ณผ ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ section, ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ section์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ section์— ๋Œ€ํ•ด 1-5๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. section์˜ ํฌ๊ธฐ๊ฐ€ 0 ๋˜๋Š” 1์ด๋ฉด ..

[c++] ๋ฐฑ์ค€ 18809๋ฒˆ : Gaaaaaaaaaarden
Algorithm ๋ฌธ์ œ/BOJ 2020. 3. 26. 17:12

https://www.acmicpc.net/problem/18809 ์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ #include #include #include #include using namespace std; struct seed { int y, x; }; int map[50][50]; vector landToSeed; vector Green; vector Red; int N, M, G, R; vector g; vector r; int dir_y[4] = {0,1,0,-1}; int dir_x[4] = {1,0,-1,0}; void printVector(vector& a) { // int์— land๋„ ๋„ฃ์„์ˆ˜ ์žˆ์„๊นŒ? vector::iterator iter; for (iter = a.begin(); iter != a.end(); i..

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ณผ์ œ 3. Tony Hoare :: QuickSort ๋…ผ๋ฌธ ํ•ด์„ํ•˜๊ธฐ
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 3. 26. 16:56

QuickSort ๋…ผ๋ฌธ brief Part One: Theory ์ด sorting์€ ๋‘ ๊ฐœ์˜ ๊ฐ„๋‹จํ•œ ๋ณด์กฐ๋ฌธ์ œ๋กœ ๋‚˜๋‰˜์–ด ํ•ด๊ฒฐ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ฐ ๋ฌธ์ œ๋Š” ์ด๋ฏธ ์•Œ๋ ค์ง„ ๋ฐฉ์‹์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค. Partition : key๊ฐ’์„ dividing line์— ๋”ฐ๋ผ ์œ„ ์•„๋ž˜๋กœ ๋‚˜๋ˆ„์–ด ์ด๋ถ„ํ•˜๋Š” ๊ฒƒ ์‹ค์ œ๋กœ ์ด dividing line์€ ํ”์น˜ ์•Š๊ณ , ์žˆ๋‹ค ํ• ์ง€๋ผ๋„ ์ฐพ๊ธฐ ํž˜๋“ ๋ฐ dividing line์ด ์กด์žฌํ•˜๊ณ  ๊ทธ๊ฒƒ์˜ ์œ„์น˜๋งŒ ์•ˆ๋‹ค๋ฉด item๋“ค์„ ์žฌ์ •๋ ฌํ•˜๊ธฐ๋Š” ์‰ฌ์›Œ์ง„๋‹ค. partition ๊ณผ์ • ์ •๋ ฌ๋˜์–ด์•ผํ•˜๋Š” ์•„์ดํ…œ์˜ ํ‚ค ์ค‘ ํŠน์ • ํ‚ค๋ฅผ ๊ณ ๋ฅธ๋‹ค. ์ด ํ‚ค๋Š” bound ๋ผ ๋ถˆ๋ฆฐ๋‹ค. dividing line ์•„๋ž˜์—๋Š” bound๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ key๊ฐ’์ด, ์œ„์ชฝ์—๋Š” ํฐ key ๊ฐ’์ด ์˜ค๊ฒŒ ํ•œ๋‹ค. dividing line์˜ ์œ„์น˜๋Š” ๋ฏธ๋ฆฌ ์•Œ ํ•„์š”..

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ณผ์ œ 2. 1000๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์ •๋ ฌ ์‹œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 3. 25. 20:01

/* 2020-03-19 ์šฐํฌ์€ ์‚ฝ์ž…์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ */ #define _CRT_SECURE_NO_WARNINGS #include #include #include #include int buffer[10000000]; // ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์•ผ stack overflow๊ฐ€ ๋‚˜์ง€ ์•Š์Œ void fileOpen() { FILE *fp = fopen("d://algorithm/unsorted10000000.txt", "r"); int i = 0; int tmp = 0; if (fp == NULL) { printf("ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"); } while (fscanf(fp, "%d ", &tmp)!=EOF) { buffer[i++] = tmp; } fclose(fp); } void insertSortin..

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ณผ์ œ 1. ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ณ„์‚ฐ :: ๊ฐ ์‹œ๊ฐ„ ๋ณต์žก๋„์— ํ•ด๋‹นํ•˜๋Š” ์†Œ์š”์‹œ๊ฐ„์„ ๊ตฌํ•ด๋ณด์ž
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 3. 25. 19:52

์ฒซ๋ฒˆ์งธ ๊ณผ์ œ์ด๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ f(n)์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์†Œ์š”์‹œ๊ฐ„์ด 1 nanosecond == 10^(-9) second ์ด๋ผ๊ณ  ํ•  ๋•Œ, ์™ผ์ชฝ์— ํ‘œ์‹œ๋œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์œ„์ชฝ์˜ ์‹œ๊ฐ„ ๋‚ด์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์™„๋ฃŒ๋˜๋ ค๋ฉด ์ตœ๋Œ€๋กœ ์–ด๋–ค N๊นŒ์ง€ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์„์ง€ ์•Œ์•„๋ณด์ž. ๋Œ€ํ‘œ๋กœ ํ•˜๋‚˜๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๋ฉด 1sec๋Š” 10^(9) nanosec ์ด๋ฏ€๋กœ n^2์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ n^2 = 10^(9) ๋กœ ๋‘๊ณ  ํ’€๋ฉด 1sec ๋งŒ์— ๊ณ„์‚ฐ๋  ์ˆ˜ ์žˆ๋Š” n ๊ฐ’์ด ๋‚˜์˜จ๋‹ค. n ๊ฐ’์€ ๋ชจ๋‘ ์ •์ˆ˜๋กœ ๊ฐ€์ •ํ•˜์˜€๋‹ค.

[c++] ๋ฐฑ์ค€ 18808๋ฒˆ : ์Šคํ‹ฐ์ปค ๋ถ™์ด๊ธฐ
Algorithm ๋ฌธ์ œ/BOJ 2020. 3. 21. 17:15

https://www.acmicpc.net/problem/18808 #include #include using namespace std; int n, m, k; int turn; int st_l, st_w; struct sticker { int x_len, y_len; }; void rotateSticker(vector& stic) { int width = stic[0].size(); int length = stic.size(); vector tmp(width, vector(length)); for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) { tmp[j][length-1-i] = stic[i][j]; } } stic = tmp; } ..

[c++]๋ฐฑ์ค€ 17836๋ฒˆ : ๊ณต์ฃผ๋‹˜์„ ๊ตฌํ•ด๋ผ!
Algorithm ๋ฌธ์ œ/BOJ 2020. 3. 21. 12:31

๋ฌธ์ œ : ๋ฐฑ์ค€ 17836๋ฒˆ _ ๊ณต์ฃผ๋‹˜์„ ๊ตฌํ•ด๋ผ! https://www.acmicpc.net/problem/17836 ์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งž์€ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ 1 ์ดˆ 256 MB 2465 566 431 21.923% ๋ฌธ์ œ ์šฉ์‚ฌ๋Š” ๋งˆ์™•์ด ์ˆจ๊ฒจ๋†“์€ ๊ณต์ฃผ๋‹˜์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด (N, M) ํฌ๊ธฐ์˜ ์„ฑ ์ž…๊ตฌ (1,1)์œผ๋กœ ๋“ค์–ด์™”๋‹ค. ๋งˆ์™•์€ ์šฉ์‚ฌ๊ฐ€ ๊ณต์ฃผ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋„๋ก ์„ฑ์˜ ์—ฌ๋Ÿฌ ๊ตฐ๋ฐ ๋งˆ๋ฒ• ๋ฒฝ์„ ์„ธ์›Œ๋†“์•˜๋‹ค. ์šฉ์‚ฌ๋Š” ํ˜„์žฌ์˜ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌด๊ธฐ๋กœ๋Š” ๋งˆ๋ฒ• ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋งˆ๋ฒ• ๋ฒฝ์„ ํ”ผํ•ด (N, M) ์œ„์น˜์— ์žˆ๋Š” ๊ณต์ฃผ๋‹˜์„ ๊ตฌ์ถœํ•ด์•ผ๋งŒ ํ•œ๋‹ค. ๋งˆ์™•์€ ์šฉ์‚ฌ๋ฅผ ๊ดด๋กญํžˆ๊ธฐ ์œ„ํ•ด ๊ณต์ฃผ์—๊ฒŒ ์ €์ฃผ๋ฅผ ๊ฑธ์—ˆ๋‹ค. ์ €์ฃผ์— ๊ฑธ๋ฆฐ ๊ณต์ฃผ๋Š” T์‹œ๊ฐ„ ์ด๋‚ด๋กœ ์šฉ์‚ฌ๋ฅผ ๋งŒ๋‚˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด ์˜์›ํžˆ ๋Œ๋กœ ๋ณ€ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ณต์ฃผ๋‹˜์„ ๊ตฌ์ถœํ•˜๊ณ  ํ”„๋Ÿฌํฌ์ฆˆ ํ•˜๊ณ  ..

[c++]๋ฐฑ์ค€ 1260๋ฒˆ : DFS์™€ BFS
Algorithm ๋ฌธ์ œ/BOJ 2020. 3. 20. 20:33

๋ฌธ์ œ : ๋ฐฑ์ค€ 1260๋ฒˆ _ DFS์™€ BFS ์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งž์€ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ 2 ์ดˆ 128 MB 87845 28912 16832 31.543% ๋ฌธ์ œ ๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ..