From 0c5d33615a07d98de7044bce70d03f4015b4fbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A7=84=EC=84=B1=EC=B0=AC?= Date: Tue, 18 Jan 2022 20:58:41 +0900 Subject: [PATCH 1/7] DS_Store delete --- .DS_Store | Bin 6148 -> 0 bytes 1. Recursion & Brute Force/.DS_Store | Bin 10244 -> 0 bytes .../APSS_boardcover_sungchan/.DS_Store | Bin 6148 -> 0 bytes .../APSS_boardcover_sungchan/boardcover.cpp | 109 ------------------ .../APSS_clocksync_sungchan/.DS_Store | Bin 6148 -> 0 bytes .../APSS_clocksync_sungchan/clocksync.cpp | 88 -------------- .../APSS_picnic_sungchan/.DS_Store | Bin 6148 -> 0 bytes .../APSS_picnic_sungchan/picnic.cpp | 59 ---------- 1. Recursion & Brute Force/README.md | 1 - README.md | 80 ------------- 10 files changed, 337 deletions(-) delete mode 100644 .DS_Store delete mode 100644 1. Recursion & Brute Force/.DS_Store delete mode 100644 1. Recursion & Brute Force/APSS_boardcover_sungchan/.DS_Store delete mode 100644 1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp delete mode 100644 1. Recursion & Brute Force/APSS_clocksync_sungchan/.DS_Store delete mode 100644 1. Recursion & Brute Force/APSS_clocksync_sungchan/clocksync.cpp delete mode 100644 1. Recursion & Brute Force/APSS_picnic_sungchan/.DS_Store delete mode 100644 1. Recursion & Brute Force/APSS_picnic_sungchan/picnic.cpp delete mode 100644 1. Recursion & Brute Force/README.md delete mode 100644 README.md diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index d61688bf009da1a71c98d794988fe7f82c89bdca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK&x_MQ6n?W?-PA?sK|wtY2%bu94t5biV(khZMA(QPRJu(Qc7sW#G>tu!Lhky% zc<|&|@K5mOZT|`{`n{Q1=x+5=L`3Gnn{Vd5c`unS^CLq6QHzK?C}ZOiieCu( zS=VICTG%Ocj)cZk(UeL`*LpjHW56-+uQ9-D_XhdYAxQ_cx_%WEu-$_8v7#%Q!7J$z zDYV9vJkHcO%2b4S-aH;aJ3q9bwqed_UQTSzPthbx%UQSkRcviHE?nI4cD!Bhb$G1i z;cPUk%Km8jl3UM|%A(WyD0-eI^I_}CLsiU1X^~8IKpH3L^6Eue#A@DGRS}muHZ%^e z={1L~>x+fI=eMQb-CMTh!uRjoZOdM-w_G;8t2b}oI~cyn@a#*+i+TB zsyISCe}g8b?LMa8fSv$5lv8|eYJNbk)v1Z56K23pVQ<&b;TUiXI0nu!!25%QG6oiF zgL>;gp|1eI2FzMe=idR2kro4swLy3w!h`}%sIXTIVZvdTwqIbeHfX|0=qDqOeX_7O z6rrCEyVTuD1O{E{7;p@nWnkMhTYUcS|Nj1eHpsOc1CD|JiUH9Wgo8eYWY5;M!SPuu tp@&cw?pGVsAt>x|tSfvJUxR9aPjUqqSgZ}g0&zbAng&-m2L37oKLJzBvC;ql diff --git a/1. Recursion & Brute Force/.DS_Store b/1. Recursion & Brute Force/.DS_Store deleted file mode 100644 index 3eb8c3e8a3f8da32e034438c9426c4a60963f4f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10244 zcmeI1&ui0Q7{{Nt>$a3BRG8?%L8b^E#%y%I#7kGZgTg=-nFk%UO-fgm<}LlPB4f~t zA}9m@0Ktn0UOag6EchSHvm%Hm@1FE|e{4;-P;be3Jxux7k_T#H|oqEUibkb$8$=CY0*lRyW#Rn z)W|U^G@F&@q^FY1F>Q#Af|5&Ix<$5EP`a)tu@!^qCZB!f6*3AvtG8_3uv!2P<9jT_2#5xYFRUv_QU%Fw23*SXgeJG6wB$R#JXw~Ym3 zS~pS)JJb5MVa(l_*5~KvcXkrm*-KY%uI4tKic8s#0Sl4ur*K+qAJi{!ZU0N@d!haT zaIqc6;~fC6zOKLS8UTHd<-HL{@eMKnw#=IAP?tTz7@q{J!ZO~jv#^3X0~N6SuN1h? z|FFYNRTKe5KoL*`6ahuxI0y`hAUd1>U&gnSKL7t8$DvdVML-cap#(@gYi2Wea{IYU z_j$6pb{&^TxUj+ diff --git a/1. Recursion & Brute Force/APSS_boardcover_sungchan/.DS_Store b/1. Recursion & Brute Force/APSS_boardcover_sungchan/.DS_Store deleted file mode 100644 index f6e1c96226aee2bb02d9c20859bd06e57b2d6d44..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKyH3ME5S)b+k&uu}%KHK&{=kYtLCp_<6S_!_kf3(QXT$6Rl;A)~fo7%M@$K#0 z$y0c}0BrTLy#YD^OS&W8Jj~7a-Dh@I86(np#ugilc*PT*XVs?@&b`Dy)+0HG{A-@I z$Np|NZid}Bu&#UmW~Yx-RtiV~DIf);fE4&e0q?!Emj1*1e@xO!3P^!}rGPEg_v;m3se0?|<-FH6`UCybSR3UG l(Ta)Din;Jse05OQ{F(cG;glG3=7UbukAUkUlLCLCzz4HcCLRC) diff --git a/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp b/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp deleted file mode 100644 index eb9a715..0000000 --- a/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp +++ /dev/null @@ -1,109 +0,0 @@ -// -// Created by 진성찬 on 2022/01/07. -// - -#include - -int board(char map[20][20], int H, int W) { - int white = 0; - for(int i = 0; i < H; i++) { - for(int j = 0; j < W; j++) { - if(map[i][j] == '.') white++; - } - } - if(white % 3 != 0) return 0; - if(white == 0) return 1; - int count = 0; - - int first_row = -1; - int first_col = -1; - for(int i = 0; i < H; i++) { - for(int j = 0; j < W; j++) { - if(map[i][j] == '.') { - first_row = i; - first_col = j; - break; - } - } - if(first_row != -1 && first_col != -1) break; - } - if(first_row == -1 && first_col == -1) return 0; - - if(first_row < H - 1 && first_col < W - 1) { // left upper corner - if(map[first_row + 1][first_col] == '.' && map[first_row][first_col + 1] == '.') { - map[first_row][first_col] = '#'; - map[first_row+1][first_col] = '#'; - map[first_row][first_col+1] = '#'; - count += board(map, H, W); - map[first_row][first_col] = '.'; - map[first_row+1][first_col] = '.'; - map[first_row][first_col+1] = '.'; - } - } - - if(first_row < H - 1 && first_col < W - 1) { // right upper corner - if(map[first_row + 1][first_col + 1] == '.' && map[first_row][first_col + 1] == '.') { - map[first_row][first_col] = '#'; - map[first_row+1][first_col+1] = '#'; - map[first_row][first_col+1] = '#'; - count += board(map, H, W); - map[first_row][first_col] = '.'; - map[first_row+1][first_col+1] = '.'; - map[first_row][first_col+1] = '.'; - } - } - - if(first_row < H - 1 && first_col < W - 1) { // left lower corner - if(map[first_row + 1][first_col] == '.' && map[first_row + 1][first_col + 1] == '.') { - map[first_row][first_col] = '#'; - map[first_row+1][first_col] = '#'; - map[first_row+1][first_col+1] = '#'; - count += board(map, H, W); - map[first_row][first_col] = '.'; - map[first_row+1][first_col] = '.'; - map[first_row+1][first_col+1] = '.'; - } - } - - if(first_row < H - 1 && first_col > 0) { // right lower corner - if(map[first_row + 1][first_col] == '.' && map[first_row + 1][first_col - 1] == '.') { - map[first_row][first_col] = '#'; - map[first_row+1][first_col] = '#'; - map[first_row+1][first_col-1] = '#'; - count += board(map, H, W); - map[first_row][first_col] = '.'; - map[first_row+1][first_col] = '.'; - map[first_row+1][first_col-1] = '.'; - } - } - - return count; -} - -int main() { - int case_number; - std::cin >> case_number; - int *H = new int[case_number]; - int *W = new int[case_number]; - char map[30][20][20]; - bool *exists = new bool[case_number]; - - for(int i = 0; i < case_number; i++) { - std::cin >> H[i] >> W[i]; - exists[i] = false; - for(int j = 0; j < H[i]; j++) { - for(int k = 0; k < W[i]; k++) { - std::cin >> map[i][j][k]; - if(map[i][j][k] == '.') exists[i] = true; - } - } - } - - for(int i = 0; i < case_number; i++) { - if(exists[i]) std::cout << board(map[i], H[i], W[i]) << std::endl; - else std::cout << 0 << std::endl; - } - - delete[] H, W, exists; - return 0; -} \ No newline at end of file diff --git a/1. Recursion & Brute Force/APSS_clocksync_sungchan/.DS_Store b/1. Recursion & Brute Force/APSS_clocksync_sungchan/.DS_Store deleted file mode 100644 index a11fdedc816df3a16613495bacbd9842852874c7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~u};H442FM0hf1hYM#g&rB;MduwF5IRfEqR;QVB7zW$RfX`mq@+wtP1Vlgtz6kjDq0n7x();FQ2+-gZ9!xAY&*|EEp569EzUR|IUax! -#include - -bool completed(int clock[16]) { - bool complete = true; - for(int i = 0; i < 16; i++) { - if(clock[i] % 12 != 0) { - complete = false; - break; - } - } - return complete; -} - -int switches[10][5] = { - {0, 1, 2, -1, -1}, - {3, 7, 9, 11, -1}, - {4, 10, 14, 15, -1}, - {0, 4, 5, 6, 7}, - {6, 7, 8, 10, 12}, - {0, 2, 14, 15, -1}, - {3, 14, 15, -1, -1}, - {4, 5, 7, 14, 15}, - {1, 2, 3, 4, 5}, - {3, 4, 5, 9, 13} -}; - -int sync(int clock[16], int start) { - int minimum; - if(completed(clock)) return 0; - if(start == 9) { - for(int i = 1; i < 4; i++) { - for(int j = 0; j < 5; j++) { - clock[switches[9][j]] += 3; - } - if(completed(clock)) { - for(int j = 0; j < 5; j++) { - clock[switches[9][j]] -= 3*i; - } - return i; - } - } - for(int j = 0; j < 5; j++) { - clock[switches[9][j]] -= 9; - } - return 31; - } else { - int count; - minimum = 31; - for(int i = 0; i < 4; i++) { - for(int j = 0; j < 5; j++) { - if(switches[start][j] != -1 && i != 0) clock[switches[start][j]] += 3; - } - count = i + sync(clock, start+1); - if(count < minimum) minimum = count; - } - for(int j = 0; j < 5; j++) { - if(switches[start][j] != -1) clock[switches[start][j]] -= 9; - } - } - - if(minimum > 30 && start == 0) return -1; - return minimum; -} - -int main() { - int case_number; - std::cin >> case_number; - int** clock = new int*[case_number]; - for(int i = 0; i < case_number; i++) { - clock[i] = new int[16]; - } - - for(int i = 0; i < case_number; i++) { - for(int j = 0; j < 16; j++) { - std::cin >> clock[i][j]; - } - } - - for(int i = 0; i < case_number; i++) { - std::cout << sync(clock[i], 0) << std::endl; - } - -} \ No newline at end of file diff --git a/1. Recursion & Brute Force/APSS_picnic_sungchan/.DS_Store b/1. Recursion & Brute Force/APSS_picnic_sungchan/.DS_Store deleted file mode 100644 index b4e0d27f20a60eec31b491c3ade7ff7bc8d8a0fd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKF-`+P474Fd1kIHx_XSA&U=@Xenh&5PO{9p>&|k&7@-vvREktr4rJ+INlXrH# zo?YD(=d+pl@@;!>HkjEQPPBIpQ{z5;VpkP$ARK4xY-4xuS3H*)xi`or^W{7LaevsX z_lGt1hmSM6>}X|40VyB_q<|EV0zW9g+6$ZCCTf%dQa}oPDZuYTgA;q8c94ABAr zX9$O39=!yxF#zm^VLuSx_R7;bLwWV4#nek#9O4B_e70S zKnk2HaGlG6_5T6>OaFgL(n<*VEFYa9F-&YUBhhIvphL^%dV iImUwJco9jN*Eq+1FB}ttj(E_4`Wc`uGAZ!a3VZ^c4 -int case_number, student_number, friend_number; - -int picnic(bool taken[10], int friends[10][10]){ - int minnum = -1; - for (int i = 0; i> case_number; - int *answer = new int[case_number]; - - for(int T = 0; T < case_number; T++){ - int friends[10][10] = {0}; - std::cin >> student_number >> friend_number; - bool taken[10] = {false}; - int *friend_list = new int[2 * friend_number]; - for(int i = 0; i< 2 * friend_number; i++) { - std::cin >> friend_list[i]; - } - - for(int i = 0; i< friend_number; i++){ - int a = friend_list[2*i]; - int b = friend_list[2*i+1]; - friends[a][b] = 1; - friends[b][a] = 1; - } - delete[] friend_list; - answer[T] = picnic(taken,friends); - } - - for (int i = 0; i < case_number; i++) { - std::cout << answer[i]; - if(i < case_number - 1) std::cout << std::endl; - } - - delete[] answer; -} \ No newline at end of file diff --git a/1. Recursion & Brute Force/README.md b/1. Recursion & Brute Force/README.md deleted file mode 100644 index 1773d9e..0000000 --- a/1. Recursion & Brute Force/README.md +++ /dev/null @@ -1 +0,0 @@ -# 재귀 & 완전탐색 diff --git a/README.md b/README.md deleted file mode 100644 index ee1b9ce..0000000 --- a/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# 알고리즘 스터디 -## 문제풀이 싸이트 -프로그래머스 https://programmers.co.kr/
-백준 https://www.acmicpc.net/
-백준 난이도별 https://solved.ac/problems/tags
- -## 규칙 -1. `master` 브랜치에 직접 push하지 말것 -2. 자기 이름으로 된 브랜치(ex. minkyu)를 파서 push할 것 -3. 문제의 풀이는 해당 문제의 출처, 문제이름, 작성자 이름으로 된 폴더를 만들어서 README.md 파일에 설명을 함께 수록할 것(선택) (ex. 종만북_Boggle_이민규) -4. 각 문제를 올릴 때마다 `master` 브랜치에 Pull Request 하고 comment/review 등을 통해 서로의 코드를 살펴보고 리뷰하기 - -## git 간략 사용 설명서 -Git이란?? -코드의 변경사항을 추적하고 관리를 용이하게 해주는 분산버전관리시스템입니다. -다들 과제에서 발표자료를 만들거나 할 때, - -> 전공발표.pptx
-> 전공발표 최종.pptx
-> 전공발표 진짜 최종.pptx
-> 전공발표 찐찐찐 최종.pptx - -처럼, 동일한 파일을 업데이트함에 따라 기존 내용도 혹시 몰라 보관하고 싶을 때 이렇게 중구난방으로 이름을 지어본 적 있으시죠??
-이때 과거의 코드를 숨겨서 컴퓨터가 알아서 관리하고 우리가 필요할 때 불러낼 수도 있으며, 팀원끼리 동일한 파일을 수정할 때 해당 파일의 병합을 손쉽게 도와주는 도구가 바로 git이라고 생각하시면 됩니다.
-보통 로컬에서 git으로 버전관리를 시작할 때는 `git init`이라는 명령어로 해당 폴더에서 버전관리를 시작하겠다고 컴퓨터에 알리는 명령어를 사용하는데, 지금은 이미 git으로 관리가 되고있는 원격 저장소를 로컬로 불러오기만 하면 돼요!
-`git --version` 명령어를 터미널이나 cmd에 입력하면 현재 깔려있는 git의 버전을 알 수 있는데, 아무 것도 안 나오는 분들은 git을 까는 것부터 시작하면 됩니다. 맥을 쓰시는 분들은 기본으로 git이 깔려있을테니 넘어가고, 윈도우 쓰시는 분들은 https://git-scm.com/downloads 여기서 git을 받아주세요. 저는 그냥 터미널에서 글자로 치는 게 편한데, 터미널이 익숙하지 않으신 분들은 그냥 GUI 버전을 다운받으셔도 됩니다! - -### 원격 저장소 로컬로 가져오기 -``` shell -$ cd "원격 저장소를 불러올 폴더명" -$ git clone https://github.com/minkyu97/algorithm_study.git -``` - -### 로컬에서 개인 branch 파기 -``` shell -$ git switch -c 브랜치명 -## 위 명령어가 안 먹힐 시 아래 명령어 사용 -$ git checkout -b 브랜치명 -``` - -### git add -처음 어떤 파일을 만들었을 때, 해당 파일은 git의 관리 하에 있지 않습니다. 따라서 앞으로 이 파일의 변경사항을 추적하라고 명령을 내려줘야 하는데, 이때 사용하는 게 `git add`입니다. 파일을 수정했을 때에도 동일한 명령어를 이용해 변경사항을 git에게 알려줘야 합니다.
-(참고로 어떤 파일이 관리 하에 있고 어떤 파일이 관리 하에 없는지를 보려면 `git status` 명령어를 이용하면 됩니다!!) -``` shell -## 특정 파일의 생성이나 변경사항, 삭제를 git에게 알려줄 때 -$ git add 파일명 -## 추가되거나 수정된 모든 파일을 추적할 때 -$ git add . -$ git add -A -``` -보통 `git add .`이나 `git add -A`를 주로 사용하시고, 특정 파일만 변경사항을 추적하고 싶을 때만 첫번째 명령어를 사용하시면 됩니다. 두번째, 세번째 명령어도 엄밀히는 무슨 차이가 있었는데 깜빡했네요... 아마 둘 중 어느 걸 사용해도 별 지장은 없을 겁니다. - -### git commit -앱스토어나 플레이스토어에서 앱을 다운 받을 때, 해당 앱의 버전이 1.0.7 등으로 표시되어있는 걸 보신 적 있으시죠? 해당 버전은 이전 버전과 비교해서 여러 차이를 가지고 있을 거예요. 그런 차이들을 모아서 아 이제 앱을 한 번 업데이트 시킬 때가 됐구나! 할 때 버전을 올리는 거죠.
-`git commit`이 바로 그런 일을 수행하는 명령어라고 볼 수 있어요. `git add` 명령어로 여러 파일의 수정사항을 저장했고, 이제 이러한 파일들의 변경사항을 하나의 버전으로 확정함으로써 이 버전에서 어떤 걸 변경했고, 만들어졌는지를 확인할 수 있는 기록을 만듦과 동시에 혹시 이후 버전에서 문제가 생겨서 과거의 버전으로 돌아가고 싶을 때, 해당 버전으로 돌아갈 수 있는 이정표를 남겨놓는 작업이죠.
-다만, 버전을 업데이트할 때는 이 버전에서 뭘 수정했는지 꼭 사람이 읽을 수 있는 기록으로 남겨줘야 돼요. 이걸 commit message 라고 부릅니다. -``` shell -## add 되어있는 모든 파일의 변경사항을 확정할 때 -$ git commit -## 버전의 변경사항을 기록함과 동시에 변경사항을 확정할 때 -$ git commit -m "커밋 메세지" -## 모든 파일의 변경사항 add와 commit을 동시에 하고싶을 때(새로 만든 파일은 포함이 안 돼요) -$ git commit -am "커밋 메세지" -``` -참고로 첫번째 명령어를 사용하면 터미널 텍스트 편집기인 vi가 자동으로 켜지고, 거기서 버전의 변경사항을 기록한 뒤 저장해야 하는데, vi가 익숙하지 않은 분들은 2번째 명령어를 사용하세요. 개인적으로 vi 사용법이 어렵지 않으니 이 참에 알아둬도 좋을 것 같다고 생각합니다!.. - -### git push -지금까지 한 모든 작업들은 로컬의 git에만 기록되어있습니다. 이제 이 기록들을 원격저장소에 업로드하는 작업이 필요하죠. 이건 엄청 간단해요! 그냥 다음 명령어를 쓰시면 됩니다. -``` shell -$ git push origin 브랜치명 -``` -예를 들어 제 로컬 minkyu 브랜치에서 원격 minkyu 브랜치로 push하고 싶을 때는 `git push origin minkyu`를 쓰시면 됩니다. - -### gitignore -`git add .` 등으로 여러 파일을 동시에 추가하고싶은데, 딱 하나의 파일만 제외하고 싶을때, 그리고 그 파일은 앞으로도 add할 일이 없을 때는 매번 그 파일만 제외하고 다른 파일들을 일일이 add하는 건 여간 번거로운 일이 아니겠죠? 그럴 땐 `.gitignore`라는 파일을 만들어 놓으면 거기에 기록되어있는 파일은 알아서 git이 무시하고 add를 하지 않으니, 안심하고 `git add .`을 사용하시면 됩니다.
-터미널에서 만드려면 다음 명령어를 사용해서 `.gitignore`파일을 만든 뒤에, 줄바꿈으로 구분해가며 무시하고 싶은 파일명들을 기록해보세요. -``` shell -$ echo ".gitignore" >> .gitignore -``` -윈도우에서는 좀 다를 수 있는데 제가 윈도우를 안 써서 정확히는 모르겠네요 ㅠㅠ 윈도우 쓰시는 분들은 한 번 구글링 해보세요! From 9cd988a69ece43d424ad65118c488e1fa21090d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A7=84=EC=84=B1=EC=B0=AC?= Date: Tue, 18 Jan 2022 21:00:57 +0900 Subject: [PATCH 2/7] first commit --- .../APSS_boardcover_sungchan/boardcover.cpp | 109 +++++++++++++++++ .../APSS_clocksync_sungchan/clocksync.cpp | 88 ++++++++++++++ .../APSS_picnic_sungchan/picnic.cpp | 59 ++++++++++ .../BJ_14501_sungchan/resign.cpp | 26 ++++ .../BJ_2448_sungchan/star.cpp | 60 ++++++++++ .../BJ_3080_sungchan/.idea/misc.xml | 4 + .../BJ_3080_sungchan/.idea/modules.xml | 8 ++ .../BJ_3080_sungchan/.idea/vcs.xml | 6 + ...353\260\261\354\244\200_3080_sungchan.iml" | 2 + .../BJ_3080_sungchan/beautifulname.cpp | 92 +++++++++++++++ 1. Recursion & Brute Force/README.md | 1 + .../APSS_fanmeeting_sungchan/fanmeeting.cpp | 80 +++++++++++++ 2. DC & DP/APSS_fence_sungchan/fence.cpp | 111 ++++++++++++++++++ .../APSS_quadtree_sungchan/quadtree.cpp | 41 +++++++ .../quantization.cpp | 61 ++++++++++ .../APSS_wildcard_sungchan/wildcard.cpp | 75 ++++++++++++ 2. DC & DP/BJ_10830_sungchan/matmul.cpp | 57 +++++++++ 2. DC & DP/BJ_1102_sungchan/powerplant.cpp | 77 ++++++++++++ 2. DC & DP/BJ_1324_sungchan/jenjang.cpp | 48 ++++++++ 2. DC & DP/BJ_14567_sungchan/prerequisite.cpp | 31 +++++ 2. DC & DP/BJ_2339_sungchan/stoneboard.cpp | 80 +++++++++++++ README.md | 80 +++++++++++++ 22 files changed, 1196 insertions(+) create mode 100644 1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp create mode 100644 1. Recursion & Brute Force/APSS_clocksync_sungchan/clocksync.cpp create mode 100644 1. Recursion & Brute Force/APSS_picnic_sungchan/picnic.cpp create mode 100644 1. Recursion & Brute Force/BJ_14501_sungchan/resign.cpp create mode 100644 1. Recursion & Brute Force/BJ_2448_sungchan/star.cpp create mode 100644 1. Recursion & Brute Force/BJ_3080_sungchan/.idea/misc.xml create mode 100644 1. Recursion & Brute Force/BJ_3080_sungchan/.idea/modules.xml create mode 100644 1. Recursion & Brute Force/BJ_3080_sungchan/.idea/vcs.xml create mode 100644 "1. Recursion & Brute Force/BJ_3080_sungchan/.idea/\353\260\261\354\244\200_3080_sungchan.iml" create mode 100644 1. Recursion & Brute Force/BJ_3080_sungchan/beautifulname.cpp create mode 100644 1. Recursion & Brute Force/README.md create mode 100644 2. DC & DP/APSS_fanmeeting_sungchan/fanmeeting.cpp create mode 100644 2. DC & DP/APSS_fence_sungchan/fence.cpp create mode 100644 2. DC & DP/APSS_quadtree_sungchan/quadtree.cpp create mode 100644 2. DC & DP/APSS_quantization_sungchan/quantization.cpp create mode 100644 2. DC & DP/APSS_wildcard_sungchan/wildcard.cpp create mode 100644 2. DC & DP/BJ_10830_sungchan/matmul.cpp create mode 100644 2. DC & DP/BJ_1102_sungchan/powerplant.cpp create mode 100644 2. DC & DP/BJ_1324_sungchan/jenjang.cpp create mode 100644 2. DC & DP/BJ_14567_sungchan/prerequisite.cpp create mode 100644 2. DC & DP/BJ_2339_sungchan/stoneboard.cpp create mode 100644 README.md diff --git a/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp b/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp new file mode 100644 index 0000000..eb9a715 --- /dev/null +++ b/1. Recursion & Brute Force/APSS_boardcover_sungchan/boardcover.cpp @@ -0,0 +1,109 @@ +// +// Created by 진성찬 on 2022/01/07. +// + +#include + +int board(char map[20][20], int H, int W) { + int white = 0; + for(int i = 0; i < H; i++) { + for(int j = 0; j < W; j++) { + if(map[i][j] == '.') white++; + } + } + if(white % 3 != 0) return 0; + if(white == 0) return 1; + int count = 0; + + int first_row = -1; + int first_col = -1; + for(int i = 0; i < H; i++) { + for(int j = 0; j < W; j++) { + if(map[i][j] == '.') { + first_row = i; + first_col = j; + break; + } + } + if(first_row != -1 && first_col != -1) break; + } + if(first_row == -1 && first_col == -1) return 0; + + if(first_row < H - 1 && first_col < W - 1) { // left upper corner + if(map[first_row + 1][first_col] == '.' && map[first_row][first_col + 1] == '.') { + map[first_row][first_col] = '#'; + map[first_row+1][first_col] = '#'; + map[first_row][first_col+1] = '#'; + count += board(map, H, W); + map[first_row][first_col] = '.'; + map[first_row+1][first_col] = '.'; + map[first_row][first_col+1] = '.'; + } + } + + if(first_row < H - 1 && first_col < W - 1) { // right upper corner + if(map[first_row + 1][first_col + 1] == '.' && map[first_row][first_col + 1] == '.') { + map[first_row][first_col] = '#'; + map[first_row+1][first_col+1] = '#'; + map[first_row][first_col+1] = '#'; + count += board(map, H, W); + map[first_row][first_col] = '.'; + map[first_row+1][first_col+1] = '.'; + map[first_row][first_col+1] = '.'; + } + } + + if(first_row < H - 1 && first_col < W - 1) { // left lower corner + if(map[first_row + 1][first_col] == '.' && map[first_row + 1][first_col + 1] == '.') { + map[first_row][first_col] = '#'; + map[first_row+1][first_col] = '#'; + map[first_row+1][first_col+1] = '#'; + count += board(map, H, W); + map[first_row][first_col] = '.'; + map[first_row+1][first_col] = '.'; + map[first_row+1][first_col+1] = '.'; + } + } + + if(first_row < H - 1 && first_col > 0) { // right lower corner + if(map[first_row + 1][first_col] == '.' && map[first_row + 1][first_col - 1] == '.') { + map[first_row][first_col] = '#'; + map[first_row+1][first_col] = '#'; + map[first_row+1][first_col-1] = '#'; + count += board(map, H, W); + map[first_row][first_col] = '.'; + map[first_row+1][first_col] = '.'; + map[first_row+1][first_col-1] = '.'; + } + } + + return count; +} + +int main() { + int case_number; + std::cin >> case_number; + int *H = new int[case_number]; + int *W = new int[case_number]; + char map[30][20][20]; + bool *exists = new bool[case_number]; + + for(int i = 0; i < case_number; i++) { + std::cin >> H[i] >> W[i]; + exists[i] = false; + for(int j = 0; j < H[i]; j++) { + for(int k = 0; k < W[i]; k++) { + std::cin >> map[i][j][k]; + if(map[i][j][k] == '.') exists[i] = true; + } + } + } + + for(int i = 0; i < case_number; i++) { + if(exists[i]) std::cout << board(map[i], H[i], W[i]) << std::endl; + else std::cout << 0 << std::endl; + } + + delete[] H, W, exists; + return 0; +} \ No newline at end of file diff --git a/1. Recursion & Brute Force/APSS_clocksync_sungchan/clocksync.cpp b/1. Recursion & Brute Force/APSS_clocksync_sungchan/clocksync.cpp new file mode 100644 index 0000000..fdc2ef1 --- /dev/null +++ b/1. Recursion & Brute Force/APSS_clocksync_sungchan/clocksync.cpp @@ -0,0 +1,88 @@ +// +// Created by 진성찬 on 2022/01/07. +// + +#include +#include + +bool completed(int clock[16]) { + bool complete = true; + for(int i = 0; i < 16; i++) { + if(clock[i] % 12 != 0) { + complete = false; + break; + } + } + return complete; +} + +int switches[10][5] = { + {0, 1, 2, -1, -1}, + {3, 7, 9, 11, -1}, + {4, 10, 14, 15, -1}, + {0, 4, 5, 6, 7}, + {6, 7, 8, 10, 12}, + {0, 2, 14, 15, -1}, + {3, 14, 15, -1, -1}, + {4, 5, 7, 14, 15}, + {1, 2, 3, 4, 5}, + {3, 4, 5, 9, 13} +}; + +int sync(int clock[16], int start) { + int minimum; + if(completed(clock)) return 0; + if(start == 9) { + for(int i = 1; i < 4; i++) { + for(int j = 0; j < 5; j++) { + clock[switches[9][j]] += 3; + } + if(completed(clock)) { + for(int j = 0; j < 5; j++) { + clock[switches[9][j]] -= 3*i; + } + return i; + } + } + for(int j = 0; j < 5; j++) { + clock[switches[9][j]] -= 9; + } + return 31; + } else { + int count; + minimum = 31; + for(int i = 0; i < 4; i++) { + for(int j = 0; j < 5; j++) { + if(switches[start][j] != -1 && i != 0) clock[switches[start][j]] += 3; + } + count = i + sync(clock, start+1); + if(count < minimum) minimum = count; + } + for(int j = 0; j < 5; j++) { + if(switches[start][j] != -1) clock[switches[start][j]] -= 9; + } + } + + if(minimum > 30 && start == 0) return -1; + return minimum; +} + +int main() { + int case_number; + std::cin >> case_number; + int** clock = new int*[case_number]; + for(int i = 0; i < case_number; i++) { + clock[i] = new int[16]; + } + + for(int i = 0; i < case_number; i++) { + for(int j = 0; j < 16; j++) { + std::cin >> clock[i][j]; + } + } + + for(int i = 0; i < case_number; i++) { + std::cout << sync(clock[i], 0) << std::endl; + } + +} \ No newline at end of file diff --git a/1. Recursion & Brute Force/APSS_picnic_sungchan/picnic.cpp b/1. Recursion & Brute Force/APSS_picnic_sungchan/picnic.cpp new file mode 100644 index 0000000..85720a4 --- /dev/null +++ b/1. Recursion & Brute Force/APSS_picnic_sungchan/picnic.cpp @@ -0,0 +1,59 @@ +// +// Created by 진성찬 on 2022/01/07. +// +#include +int case_number, student_number, friend_number; + +int picnic(bool taken[10], int friends[10][10]){ + int minnum = -1; + for (int i = 0; i> case_number; + int *answer = new int[case_number]; + + for(int T = 0; T < case_number; T++){ + int friends[10][10] = {0}; + std::cin >> student_number >> friend_number; + bool taken[10] = {false}; + int *friend_list = new int[2 * friend_number]; + for(int i = 0; i< 2 * friend_number; i++) { + std::cin >> friend_list[i]; + } + + for(int i = 0; i< friend_number; i++){ + int a = friend_list[2*i]; + int b = friend_list[2*i+1]; + friends[a][b] = 1; + friends[b][a] = 1; + } + delete[] friend_list; + answer[T] = picnic(taken,friends); + } + + for (int i = 0; i < case_number; i++) { + std::cout << answer[i]; + if(i < case_number - 1) std::cout << std::endl; + } + + delete[] answer; +} \ No newline at end of file diff --git a/1. Recursion & Brute Force/BJ_14501_sungchan/resign.cpp b/1. Recursion & Brute Force/BJ_14501_sungchan/resign.cpp new file mode 100644 index 0000000..27bd217 --- /dev/null +++ b/1. Recursion & Brute Force/BJ_14501_sungchan/resign.cpp @@ -0,0 +1,26 @@ +// +// Created by 진성찬 on 2022/01/08. +// + +#include +using namespace std; +int N, *T, *P; + +int revenue(int start) { + int on, off; + if(start >= N) return 0; + if(start + T[start] > N) on = 0; + else on = P[start] + revenue(start+T[start]); + off = revenue(start+1); + return max(on, off); +} + +int main() { + cin >> N; + T = new int[N]; + P = new int[N]; + for(int i = 0; i < N; i++) { + cin >> T[i] >> P[i]; + } + cout << revenue(0); +} \ No newline at end of file diff --git a/1. Recursion & Brute Force/BJ_2448_sungchan/star.cpp b/1. Recursion & Brute Force/BJ_2448_sungchan/star.cpp new file mode 100644 index 0000000..76256a2 --- /dev/null +++ b/1. Recursion & Brute Force/BJ_2448_sungchan/star.cpp @@ -0,0 +1,60 @@ +// +// Created by 진성찬 on 2022/01/08. +// + + +#include +#include +#include +using namespace std; +int N; +string stars = " * \n" + " * * \n" + "*****"; + +string star(int N); + +vector split(string s, string divid) { + vector v; + int start = 0; + int d = s.find(divid); + while(d != -1) { + v.push_back(s.substr(start, d-start)); + start = d + 1; + d = s.find(divid, start); + } + v.push_back(s.substr(start, d - start)); + return v; +} + +string combine(int N) { + string one = star(N); + vector lines = split(one, "\n"); + + string two = ""; + for(int i = 0; i < N; i++) { + two += lines.at(i); + two += " "; + two += lines.at(i); + if(i != N-1) two += "\n"; + } + + string new_lines = ""; + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) new_lines += ' '; + new_lines += lines.at(i); + for(int j = 0; j < N; j++) new_lines += ' '; + new_lines += "\n"; + } + return new_lines + two; +} + +string star(int N) { + if(N == 3) return stars; + else return combine(N/2); +} + +int main() { + cin >> N; + cout << star(N); +} diff --git a/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/misc.xml b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/modules.xml b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/modules.xml new file mode 100644 index 0000000..e87152d --- /dev/null +++ b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/vcs.xml b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/\353\260\261\354\244\200_3080_sungchan.iml" "b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/\353\260\261\354\244\200_3080_sungchan.iml" new file mode 100644 index 0000000..f08604b --- /dev/null +++ "b/1. Recursion & Brute Force/BJ_3080_sungchan/.idea/\353\260\261\354\244\200_3080_sungchan.iml" @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/1. Recursion & Brute Force/BJ_3080_sungchan/beautifulname.cpp b/1. Recursion & Brute Force/BJ_3080_sungchan/beautifulname.cpp new file mode 100644 index 0000000..a95c2c8 --- /dev/null +++ b/1. Recursion & Brute Force/BJ_3080_sungchan/beautifulname.cpp @@ -0,0 +1,92 @@ +#include +#include +#define MOD 1000000007 +int N; +using namespace std; +string* names; +int* start_num; +int* end_num; +long long factorial[28]; + +bool comp(string s1, string s2) { + return s1 < s2; +} + +void mutual_finder(string* names) { + start_num = new int[N-1]; + end_num = new int[N-1]; + for(int i = 0; i < N - 1; i++) { // finding mutual string between names[i] and names[i+1] + string mutual = ""; + int len1 = names[i].size(); + int len2 = names[i+1].size(); + for(int j = 0; j < len1; j++) { + if(j >= len2) break; + else if(names[i][j] == names[i+1][j]) mutual += names[i][j]; + else break; + } + if(mutual == "") { + start_num[i] = 0; + end_num[i] = N-1; + } else { + int mutual_size = mutual.size(); + bool start_done = false; + for(int j = 0; j <= i; j++) { + if(names[j].size() < mutual_size) continue; + for(int k = 0; k < mutual_size; k++) { + if(mutual[k] != names[j][k]) break; + if(k == mutual_size - 1) { + start_done = true; + start_num[i] = j; + } + } + if(start_done) break; + } + bool end_done = false; + for(int j = N-1; j >= i+1; j--) { + if(names[j].size() < mutual_size) continue; + for(int k = 0; k < mutual_size; k++) { + if(mutual[k] != names[j][k]) break; + if(k == mutual_size - 1) { + end_done = true; + end_num[i] = j; + } + } + if(end_done) break; + } + } + } + factorial[0] = 1; + for(int i = 1; i <= 27; i++) factorial[i] = (i * factorial[i-1]) % MOD; +} + +int counter(int start, int end) { + if(start == end) return 1; + else if(start + 1 == end) return 2; + + int index_number = 0; + int* index = new int[end-start]; + for(int i = start; i < end; i++) { + if(start_num[i] == start && end_num[i] == end) { + index[index_number++] = i; + } + } + index[index_number] = end; + + long long count = (factorial[index_number + 1] * counter(start, index[0])) % MOD; + for(int i = 0; i < index_number; i++) { + count = (count * counter(index[i] + 1, index[i+1])) % MOD; + } + + return count; +} + +int main() { + cin >> N; + names = new string[N]; + for(int i = 0; i < N; i++) { + cin >> names[i]; + } + sort(names, names+N, comp); + mutual_finder(names); + cout << counter(0, N-1); +} diff --git a/1. Recursion & Brute Force/README.md b/1. Recursion & Brute Force/README.md new file mode 100644 index 0000000..1773d9e --- /dev/null +++ b/1. Recursion & Brute Force/README.md @@ -0,0 +1 @@ +# 재귀 & 완전탐색 diff --git a/2. DC & DP/APSS_fanmeeting_sungchan/fanmeeting.cpp b/2. DC & DP/APSS_fanmeeting_sungchan/fanmeeting.cpp new file mode 100644 index 0000000..cfa578d --- /dev/null +++ b/2. DC & DP/APSS_fanmeeting_sungchan/fanmeeting.cpp @@ -0,0 +1,80 @@ +#include +#include +using namespace std; +int C; + +vector multiply(const vector& a, const vector& b) { + vector c(a.size() + b.size(), 0); + for(int i = 0; i < a.size(); i++) + for(int j = 0; j < b.size(); j++) + c[i+j] += a[i] * b[j]; + return c; +} + +void addTo(vector& a, const vector& b, int k) { + if(a.size() < b.size() + k) { + for(int i = a.size(); i < b.size() + k; i++) a.push_back(0); + } + for(int i = 0; i < b.size(); i++) a[i+k] += b[i]; +} + +void subFrom(vector& a, const vector& b) { + for(int i = 0; i < b.size(); i++) a[i] -= b[i]; +} + +vector karatsuba(vector a, vector b) { + int an = a.size(), bn = b.size(); + if(an < bn) return karatsuba(b, a); + if(an == 0 || bn == 0) return vector(); + if(an <= 100) return multiply(a, b); + int half = an / 2; + + vector a0(a.begin(), a.begin() + half); + vector a1(a.begin() + half, a.end()); + vector b0(b.begin(), b.begin() + min(b.size(), half)); + vector b1(b.begin() + min(b.size(), half), b.end()); + vector z2 = karatsuba(a1, b1); + vector z0 = karatsuba(a0, b0); + + addTo(a0, a1, 0); + addTo(b0, b1, 0); + vector z1 = karatsuba(a0, b0); + subFrom(z1, z0); + subFrom(z1, z2); + + vector ret; + addTo(ret, z0, 0); + addTo(ret, z1, half); + addTo(ret, z2, half + half); + + return ret; +} + +int counter(string members, string fans, bool trim) { + int member_size = members.size(); + int fan_size = fans.size(); + + vector male_member(member_size), male_fan(fan_size); + for(int i = 0; i < member_size; i++) male_member[i] = (members[i] == 'M'); + for(int i = 0; i < fan_size; i++) male_fan[i] = (fans[fan_size - i - 1] == 'M'); + vector karatsubas = karatsuba(male_member, male_fan); + int answer = 0; + for(int i = member_size - 1; i < fan_size; i++) { + if(karatsubas[i] == 0) answer++; + } + return answer; +} + +int main() { + cin >> C; + string* members = new string[C]; + string* fans = new string[C]; + for(int i = 0; i < C; i++) { + cin >> members[i]; + cin >> fans[i]; + } + + for(int i = 0; i < C; i++) { + cout << counter(members[i], fans[i], false) << endl; + } +} \ No newline at end of file diff --git a/2. DC & DP/APSS_fence_sungchan/fence.cpp b/2. DC & DP/APSS_fence_sungchan/fence.cpp new file mode 100644 index 0000000..7277df6 --- /dev/null +++ b/2. DC & DP/APSS_fence_sungchan/fence.cpp @@ -0,0 +1,111 @@ + +#include +#include +using namespace std; +int C, num, height; +vector> boards; + +int rectangle(vector board) { + int size = board.size(); + if(size == 1) return board.at(0); + if(size == 2) return max(max(board.at(0), board.at(1)), 2 * min(board.at(0), board.at(1))); + + vector left(board.begin(), board.begin() + size/2); + vector right(board.begin() + size/2, board.begin() + size); + int left_max = rectangle(left); + int right_max = rectangle(right); + + int height = min(board.at(size/2 - 1), board.at(size/2)); + int mid_max = 2 * height; + + int left_index = -1; + for(int i = size/2 - 2; i >= 0; i--) { + if(board.at(i) >= height) mid_max += height; + else { + left_index = i; + break; + } + } + + int right_index = -1; + for(int i = size/2 + 1; i < size; i++) { + if(board.at(i) >= height) mid_max += height; + else { + right_index = i; + break; + } + } + + if(left_index != -1) { + if(right_index != -1) { + int max_height = height; + while(true) { + if(left_index < 0) { + if(right_index == size) break; + max_height = min(max_height, board.at(right_index)); + if(max_height * (right_index + 1) > mid_max) mid_max = max_height * (right_index + 1); + right_index++; + } else if (right_index == size) { + max_height = min(max_height, board.at(left_index)); + if(max_height * (size - left_index) > mid_max) mid_max = max_height * (size - left_index); + left_index--; + } else { + int left_height = board.at(left_index); + int right_height = board.at(right_index); + if(left_height >= right_height && left_height <= max_height) { + max_height = left_height; + if((right_index - left_index) * max_height > mid_max) mid_max = (right_index - left_index) * max_height; + left_index--; + } else if(right_height > left_height && right_height <= max_height) { + max_height = right_height; + if((right_index - left_index) * max_height > mid_max) mid_max = (right_index - left_index) * max_height; + right_index++; + } else if(max_height >= left_height) { + if((right_index - left_index) * max_height > mid_max) mid_max = (right_index - left_index) * max_height; + right_index++; + } else if(max_height >= right_height) { + if((right_index - left_index) * max_height > mid_max) mid_max = (right_index - left_index) * max_height; + left_index--; + } else { + if((right_index - left_index + 1) * max_height > mid_max) mid_max = (right_index - left_index + 1) * max_height; + left_index--; + right_index++; + } + } + } + } else { // right_index == -1 + int new_height = board.at(left_index); + if(new_height * (size - left_index) > mid_max) mid_max = new_height * (size - left_index); + for(int i = left_index - 1; i >= 0; i--) { + new_height = min(new_height, board.at(i)); + if(new_height * (size - i) > mid_max) mid_max = new_height * (size - i); + } + } + } else if(right_index != -1) { // left_index == -1 + int new_height = board.at(right_index); + if(new_height * (right_index + 1) > mid_max) mid_max = new_height * (right_index + 1); + for(int i = right_index + 1; i < size; i++) { + new_height = min(new_height, board.at(i)); + if(new_height * (i + 1) > mid_max) mid_max = new_height * (i + 1); + } + } + + return max(max(left_max, right_max), mid_max); +} + +int main() { + cin >> C; + for(int i = 0; i < C; i++) { + cin >> num; + vector board; + boards.push_back(board); + for(int j = 0; j < num; j++) { + cin >> height; + boards.at(i).push_back(height); + } + } + + for(int i = 0; i < C; i++) { + cout << rectangle(boards.at(i)) << endl; + } +} \ No newline at end of file diff --git a/2. DC & DP/APSS_quadtree_sungchan/quadtree.cpp b/2. DC & DP/APSS_quadtree_sungchan/quadtree.cpp new file mode 100644 index 0000000..7793429 --- /dev/null +++ b/2. DC & DP/APSS_quadtree_sungchan/quadtree.cpp @@ -0,0 +1,41 @@ +// +// Created by 진성찬 on 2022/01/13. +// + +#include +using namespace std; +int C; + +string upside_down(string input) { + if(input.size() == 1) return input; + int* index = new int[5]; + index[0] = 1; + for(int i = 1; i < 4; i++) { + int x_num = 0; + int other_num = 0; + int idx = index[i-1]; + while(x_num * 3 + 1 != other_num) { + if(input.at(idx++) == 'x') x_num++; + else other_num++; + } + index[i] = idx; + } + index[4] = input.size(); + + string* substring = new string[4]; + for(int i = 0; i < 4; i++) { + substring[i] = upside_down(input.substr(index[i], index[i+1] - index[i])); + } + return "x" + substring[2] + substring[3] + substring[0] + substring[1]; +} + +int main() { + cin >> C; + string* images = new string[C]; + for(int i = 0; i < C; i++) { + cin >> images[i]; + } + for(int i = 0; i < C; i++) { + cout << upside_down(images[i]) << endl; + } +} \ No newline at end of file diff --git a/2. DC & DP/APSS_quantization_sungchan/quantization.cpp b/2. DC & DP/APSS_quantization_sungchan/quantization.cpp new file mode 100644 index 0000000..52b9b4b --- /dev/null +++ b/2. DC & DP/APSS_quantization_sungchan/quantization.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +using namespace std; +int C; +int seq[50][100]; +int length[50]; +int quant[50]; +int cache[100][100][10]; + +int getErrorSum(int* seq, int start, int end) { + int sum = 0; + for(int i = start; i <= end; i++) sum += seq[i]; + int mean = round((float) sum / (float) (end - start + 1)); + int errorsum = 0; + for(int i = start; i <= end; i++) errorsum += (mean - seq[i]) * (mean - seq[i]); + return errorsum; +} + + +int quantize(int* sequence, int quant, int length, int start, int end) { + if(length <= quant) return 0; + if(cache[start][end][quant-1] != -1) return cache[start][end][quant-1]; + int minimum = getErrorSum(sequence, start, end); + if(quant == 1) { + cache[start][end][0] = minimum; + return minimum; + } + for(int i = start; i <= end - quant + 1; i++) { + int error = getErrorSum(sequence, start, i) + quantize(sequence, quant-1, length-(i-start+1), i+1, end); + if(error < minimum) minimum = error; + } + cache[start][end][quant-1] = minimum; + return minimum; +} + +void initialize() { + for(int i = 0; i < 100; i++) + for(int j = 0; j < 100; j++) + for(int k = 0; k < 10; k++) + cache[i][j][k] = -1; +} + +int main() { + cin >> C; + for(int i = 0; i < C; i++) { + cin >> length[i]; + cin >> quant[i]; + for(int j = 0; j < length[i]; j++){ + cin >> seq[i][j]; + } + sort(seq[i], seq[i] + length[i]); + } + + for(int i = 0; i < C; i++) { + initialize(); + cout << quantize(seq[i], quant[i], length[i], 0, length[i]-1) << endl; + } + + return 0; +} \ No newline at end of file diff --git a/2. DC & DP/APSS_wildcard_sungchan/wildcard.cpp b/2. DC & DP/APSS_wildcard_sungchan/wildcard.cpp new file mode 100644 index 0000000..418a9ea --- /dev/null +++ b/2. DC & DP/APSS_wildcard_sungchan/wildcard.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; +int C; +string* W; +string** files; + +bool comp(string s1, string s2) { + return s1 < s2; +} + +bool matchMemoized(const string& W, const string& S, int w, int s, int cache[101][101]) { + int& ret = cache[w][s]; + if(ret != -1) return ret; + if(w < W.size() && s < S.size() && W[w] == '?' || W[w] == S[s]) return ret = matchMemoized(W, S, w+1, s+1, cache); + if(w == W.size()) return ret = (s == S.size()); + if(W[w] == '*') { + if(matchMemoized(W, S, w+1, s, cache) || (s < S.size() && matchMemoized(W, S, w, s+1, cache))) return ret = 1; + } + return ret = 0; +} + +bool match(const string& w, const string& s) { + int pos = 0; + while(pos < s.size() && pos < w.size() && (w[pos] == '?' || w[pos] == s[pos])) pos++; + + if(pos == w.size()) return pos == s.size(); + if(w[pos] == '*') { + for(int skip = 0; pos+skip <= s.size(); skip++) { + if(match(w.substr(pos+1), s.substr(pos+skip))) return true; + } + } + return false; +} + +void finder(string W, string* file_names, int file_number) { + int cache[101][101]; + for(int i = 0; i <= 100; i++) + for(int j = 0; j <= 100; j++) + cache[i][j] = -1; + string* answer = new string[file_number]; + int answer_num = 0; + + for(int i = 0; i < file_number; i++) { + if(match(W, file_names[i])) answer[answer_num++] = file_names[i]; + //if(matchMemoized(W, file_names[i], 0, 0, cache)) answer[answer_num++] = file_names[i]; + } + + sort(answer, answer+answer_num, comp); + for(int i = 0; i < answer_num; i++) { + cout << answer[i] << endl; + } +} + + +int main() { + cin >> C; + W = new string[C]; + files = new string*[C]; + int* files_number = new int[C]; + for(int i = 0; i < C; i++) { + cin >> W[i]; + int n; + cin >> n; + files[i] = new string[n]; + files_number[i] = n; + for(int j = 0; j < n; j++) { + cin >> files[i][j]; + } + } + + for(int i = 0; i < C; i++) { + finder(W[i], files[i], files_number[i]); + } +} \ No newline at end of file diff --git a/2. DC & DP/BJ_10830_sungchan/matmul.cpp b/2. DC & DP/BJ_10830_sungchan/matmul.cpp new file mode 100644 index 0000000..0ca2b83 --- /dev/null +++ b/2. DC & DP/BJ_10830_sungchan/matmul.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; +int N; +long long B; +int** I; + +int** product(int** A, int** D) { + int** C = new int*[N]; + for(int i = 0; i < N; i++) { + C[i] = new int[N]; + for(int j = 0; j < N; j++) { + C[i][j] = 0; + for(int k = 0; k < N; k++) { + C[i][j] += (A[i][k] * D[k][j]); + } + C[i][j] %= 1000; + } + } + return C; +} + +int main() { + ios_base::sync_with_stdio(0); + cin.tie(0), cout.tie(0); + + cin >> N >> B; + int** matrix = new int*[N]; + for(int i = 0; i < N; i++) { + matrix[i] = new int[N]; + for(int j = 0; j < N; j++) { + cin >> matrix[i][j]; + } + } + + I = new int*[N]; + for(int i = 0; i < N; i++) { + I[i] = new int[N]; + I[i][i] = 1; + } + + while(B) { + if(B % 2 == 1) { + I = product(I, matrix); + } + matrix = product(matrix, matrix); + B /= 2; + } + + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + cout << I[i][j] << " "; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/2. DC & DP/BJ_1102_sungchan/powerplant.cpp b/2. DC & DP/BJ_1102_sungchan/powerplant.cpp new file mode 100644 index 0000000..f263347 --- /dev/null +++ b/2. DC & DP/BJ_1102_sungchan/powerplant.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +using namespace std; +int N, P; +int cost[16][16]; +int on[16]; +int off[16]; +int ret[65536]; + +void memoize(int c, int minimum) { + int index = 0; + for(int i = 0; i < c; i++) { + index += (int) pow(2, on[i]); + } + ret[index] = minimum; +} + +int memoization(int c) { + int index = 0; + for(int i = 0; i < c; i++) { + index += (int) pow(2, on[i]); + } + return ret[index]; +} + +int answer(int C) { + if(C == 0) { + if(P == 0) return 0; + else return -1; + } + if(P <= C) return 0; + if(memoization(C) != -1) return memoization(C); + + int minimum = (P - C) * 36; + for(int j = 0; j < N - C; j++) { + int select = 36; + for(int i = 0; i < C; i++) { + if(cost[on[i]][off[j]] < select) { + select = cost[on[i]][off[j]]; + } + } + on[C] = off[j]; + for(int i = j + 1; i < N - C; i++) { + off[i-1] = off[i]; + } + int count = select + answer(C+1); + minimum = min(minimum, count); + for(int i = N - C - 1; i > j; i--) { + off[i] = off[i-1]; + } + off[j] = on[C]; + } + memoize(C, minimum); + return minimum; +} + +int main() { + for(int i = 0; i < 65536; i++) ret[i] = -1; + + cin >> N; + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) + cin >> cost[i][j]; + } + + char s; + int C = 0; + for(int i = 0; i < N; i++) { + cin >> s; + if(s == 'Y') on[C++] = i; + else off[i-C] = i; + } + cin >> P; + + cout << answer(C); +} \ No newline at end of file diff --git a/2. DC & DP/BJ_1324_sungchan/jenjang.cpp b/2. DC & DP/BJ_1324_sungchan/jenjang.cpp new file mode 100644 index 0000000..b885bff --- /dev/null +++ b/2. DC & DP/BJ_1324_sungchan/jenjang.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +int N; +int *day1, *day2; +int ret[1000][1000]; + +int counter(int start1, int start2) { + if(start1 >= N || start2 >= N) return 0; + if(ret[start1][start2] != -1) return ret[start1][start2]; + + int startnum = day1[start1]; + int index2 = -1; + for(int i = start2; i < N; i++) { + if(day2[i] == startnum) { + index2 = i; + break; + } + } + if(index2 == -1) return ret[start1][start2] = counter(start1+1, start2); + else { + int max = 1; + for(int i = start1 + 1; i < N; i++) { + if(day1[i] > startnum) { + int count = 1 + counter(i, index2+1); + if(count > max) max = count; + } + } + for(int i = start1 + 1; i < N; i++) { + if(counter(i, start2) > max) max = counter(i, start2); + } + + return ret[start1][start2] = max; + } +} + +int main() { + for(int i = 0; i < 1000; i++) + for(int j = 0; j < 1000; j++) + ret[i][j] = -1; + + cin >> N; + day1 = new int[N]; + day2 = new int[N]; + for(int i = 0; i < N; i++) cin >> day1[i]; + for(int i = 0; i < N; i++) cin >> day2[i]; + int answer = counter(0, 0); + cout << answer; +} \ No newline at end of file diff --git a/2. DC & DP/BJ_14567_sungchan/prerequisite.cpp b/2. DC & DP/BJ_14567_sungchan/prerequisite.cpp new file mode 100644 index 0000000..3e862e5 --- /dev/null +++ b/2. DC & DP/BJ_14567_sungchan/prerequisite.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int N, M; + +int* counter(bool** course) { + int* answer = new int[N]; + for(int i = 0; i < N; i++) answer[i] = 1; + for(int i = 0; i < N; i++) { + for(int j = i+1; j < N; j++) { + if(course[i][j]) answer[j] = max(answer[i] + 1, answer[j]); + } + } + return answer; +} + +int main() { + cin >> N >> M; + bool** course = new bool*[N]; + for(int i = 0; i < N; i++) course[i] = new bool[N]; + + for(int i = 0; i < M; i++) { + int A, B; + cin >> A >> B; + course[A-1][B-1] = true; + } + + int* answer = counter(course); + for(int i = 0; i < N; i++) { + cout << answer[i] << " "; + } +} \ No newline at end of file diff --git a/2. DC & DP/BJ_2339_sungchan/stoneboard.cpp b/2. DC & DP/BJ_2339_sungchan/stoneboard.cpp new file mode 100644 index 0000000..d51e05b --- /dev/null +++ b/2. DC & DP/BJ_2339_sungchan/stoneboard.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; +int N; +int** stone; + +int counter(int startrow, int startcol, int endrow, int endcol, bool rowwise) { + if(startrow > endrow || startcol > endcol) return 0; + + int gem = 0; + int impurity = 0; + int* gem_row = new int[15]; + int* gem_col = new int[15]; + int* impurity_row = new int[15]; + int* impurity_col = new int[15]; + for(int i = startrow; i <= endrow; i++) { + for(int j = startcol; j <= endcol; j++) { + if(stone[i][j] == 2) { + gem_row[gem] = i; + gem_col[gem] = j; + gem++; + } else if(stone[i][j] == 1) { + impurity_row[impurity] = i; + impurity_col[impurity] = j; + impurity++; + } + } + } + if(gem == 0) return 0; + else if(gem == 1) { + if(impurity == 0) return 1; + else return 0; + } else if(impurity == 0) return 0; + + if(rowwise) { // same row + int count = 0; + for(int i = 0; i < impurity; i++) { + bool possible = true; + for(int j = 0; j < gem; j++) { + if(gem_row[j] == impurity_row[i]) { + possible = false; + break; + } + } + if(possible) { + count += counter(startrow, startcol, impurity_row[i]-1, endcol, false) * counter(impurity_row[i]+1, startcol, endrow, endcol, false); + } + } + return count; + } else { // same col + int count = 0; + for(int j = 0; j < impurity; j++) { + bool possible = true; + for(int i = 0; i < gem; i++) { + if(gem_col[i] == impurity_col[j]) { + possible = false; + break; + } + } + if(possible) { + count += counter(startrow, startcol, endrow, impurity_col[j]-1, true) * counter(startrow, impurity_col[j]+1, endrow, endcol, true); + } + } + return count; + } +} + +int main() { + cin >> N; + stone = new int*[N]; + for(int i = 0; i < N; i++) { + stone[i] = new int[N]; + for(int j = 0; j < N; j++) { + cin >> stone[i][j]; + } + } + + int answer = counter(0,0,N-1,N-1, true) + counter(0,0,N-1,N-1, false); + if(answer == 0) cout << -1; + else cout << answer; +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee1b9ce --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +# 알고리즘 스터디 +## 문제풀이 싸이트 +프로그래머스 https://programmers.co.kr/
+백준 https://www.acmicpc.net/
+백준 난이도별 https://solved.ac/problems/tags
+ +## 규칙 +1. `master` 브랜치에 직접 push하지 말것 +2. 자기 이름으로 된 브랜치(ex. minkyu)를 파서 push할 것 +3. 문제의 풀이는 해당 문제의 출처, 문제이름, 작성자 이름으로 된 폴더를 만들어서 README.md 파일에 설명을 함께 수록할 것(선택) (ex. 종만북_Boggle_이민규) +4. 각 문제를 올릴 때마다 `master` 브랜치에 Pull Request 하고 comment/review 등을 통해 서로의 코드를 살펴보고 리뷰하기 + +## git 간략 사용 설명서 +Git이란?? +코드의 변경사항을 추적하고 관리를 용이하게 해주는 분산버전관리시스템입니다. +다들 과제에서 발표자료를 만들거나 할 때, + +> 전공발표.pptx
+> 전공발표 최종.pptx
+> 전공발표 진짜 최종.pptx
+> 전공발표 찐찐찐 최종.pptx + +처럼, 동일한 파일을 업데이트함에 따라 기존 내용도 혹시 몰라 보관하고 싶을 때 이렇게 중구난방으로 이름을 지어본 적 있으시죠??
+이때 과거의 코드를 숨겨서 컴퓨터가 알아서 관리하고 우리가 필요할 때 불러낼 수도 있으며, 팀원끼리 동일한 파일을 수정할 때 해당 파일의 병합을 손쉽게 도와주는 도구가 바로 git이라고 생각하시면 됩니다.
+보통 로컬에서 git으로 버전관리를 시작할 때는 `git init`이라는 명령어로 해당 폴더에서 버전관리를 시작하겠다고 컴퓨터에 알리는 명령어를 사용하는데, 지금은 이미 git으로 관리가 되고있는 원격 저장소를 로컬로 불러오기만 하면 돼요!
+`git --version` 명령어를 터미널이나 cmd에 입력하면 현재 깔려있는 git의 버전을 알 수 있는데, 아무 것도 안 나오는 분들은 git을 까는 것부터 시작하면 됩니다. 맥을 쓰시는 분들은 기본으로 git이 깔려있을테니 넘어가고, 윈도우 쓰시는 분들은 https://git-scm.com/downloads 여기서 git을 받아주세요. 저는 그냥 터미널에서 글자로 치는 게 편한데, 터미널이 익숙하지 않으신 분들은 그냥 GUI 버전을 다운받으셔도 됩니다! + +### 원격 저장소 로컬로 가져오기 +``` shell +$ cd "원격 저장소를 불러올 폴더명" +$ git clone https://github.com/minkyu97/algorithm_study.git +``` + +### 로컬에서 개인 branch 파기 +``` shell +$ git switch -c 브랜치명 +## 위 명령어가 안 먹힐 시 아래 명령어 사용 +$ git checkout -b 브랜치명 +``` + +### git add +처음 어떤 파일을 만들었을 때, 해당 파일은 git의 관리 하에 있지 않습니다. 따라서 앞으로 이 파일의 변경사항을 추적하라고 명령을 내려줘야 하는데, 이때 사용하는 게 `git add`입니다. 파일을 수정했을 때에도 동일한 명령어를 이용해 변경사항을 git에게 알려줘야 합니다.
+(참고로 어떤 파일이 관리 하에 있고 어떤 파일이 관리 하에 없는지를 보려면 `git status` 명령어를 이용하면 됩니다!!) +``` shell +## 특정 파일의 생성이나 변경사항, 삭제를 git에게 알려줄 때 +$ git add 파일명 +## 추가되거나 수정된 모든 파일을 추적할 때 +$ git add . +$ git add -A +``` +보통 `git add .`이나 `git add -A`를 주로 사용하시고, 특정 파일만 변경사항을 추적하고 싶을 때만 첫번째 명령어를 사용하시면 됩니다. 두번째, 세번째 명령어도 엄밀히는 무슨 차이가 있었는데 깜빡했네요... 아마 둘 중 어느 걸 사용해도 별 지장은 없을 겁니다. + +### git commit +앱스토어나 플레이스토어에서 앱을 다운 받을 때, 해당 앱의 버전이 1.0.7 등으로 표시되어있는 걸 보신 적 있으시죠? 해당 버전은 이전 버전과 비교해서 여러 차이를 가지고 있을 거예요. 그런 차이들을 모아서 아 이제 앱을 한 번 업데이트 시킬 때가 됐구나! 할 때 버전을 올리는 거죠.
+`git commit`이 바로 그런 일을 수행하는 명령어라고 볼 수 있어요. `git add` 명령어로 여러 파일의 수정사항을 저장했고, 이제 이러한 파일들의 변경사항을 하나의 버전으로 확정함으로써 이 버전에서 어떤 걸 변경했고, 만들어졌는지를 확인할 수 있는 기록을 만듦과 동시에 혹시 이후 버전에서 문제가 생겨서 과거의 버전으로 돌아가고 싶을 때, 해당 버전으로 돌아갈 수 있는 이정표를 남겨놓는 작업이죠.
+다만, 버전을 업데이트할 때는 이 버전에서 뭘 수정했는지 꼭 사람이 읽을 수 있는 기록으로 남겨줘야 돼요. 이걸 commit message 라고 부릅니다. +``` shell +## add 되어있는 모든 파일의 변경사항을 확정할 때 +$ git commit +## 버전의 변경사항을 기록함과 동시에 변경사항을 확정할 때 +$ git commit -m "커밋 메세지" +## 모든 파일의 변경사항 add와 commit을 동시에 하고싶을 때(새로 만든 파일은 포함이 안 돼요) +$ git commit -am "커밋 메세지" +``` +참고로 첫번째 명령어를 사용하면 터미널 텍스트 편집기인 vi가 자동으로 켜지고, 거기서 버전의 변경사항을 기록한 뒤 저장해야 하는데, vi가 익숙하지 않은 분들은 2번째 명령어를 사용하세요. 개인적으로 vi 사용법이 어렵지 않으니 이 참에 알아둬도 좋을 것 같다고 생각합니다!.. + +### git push +지금까지 한 모든 작업들은 로컬의 git에만 기록되어있습니다. 이제 이 기록들을 원격저장소에 업로드하는 작업이 필요하죠. 이건 엄청 간단해요! 그냥 다음 명령어를 쓰시면 됩니다. +``` shell +$ git push origin 브랜치명 +``` +예를 들어 제 로컬 minkyu 브랜치에서 원격 minkyu 브랜치로 push하고 싶을 때는 `git push origin minkyu`를 쓰시면 됩니다. + +### gitignore +`git add .` 등으로 여러 파일을 동시에 추가하고싶은데, 딱 하나의 파일만 제외하고 싶을때, 그리고 그 파일은 앞으로도 add할 일이 없을 때는 매번 그 파일만 제외하고 다른 파일들을 일일이 add하는 건 여간 번거로운 일이 아니겠죠? 그럴 땐 `.gitignore`라는 파일을 만들어 놓으면 거기에 기록되어있는 파일은 알아서 git이 무시하고 add를 하지 않으니, 안심하고 `git add .`을 사용하시면 됩니다.
+터미널에서 만드려면 다음 명령어를 사용해서 `.gitignore`파일을 만든 뒤에, 줄바꿈으로 구분해가며 무시하고 싶은 파일명들을 기록해보세요. +``` shell +$ echo ".gitignore" >> .gitignore +``` +윈도우에서는 좀 다를 수 있는데 제가 윈도우를 안 써서 정확히는 모르겠네요 ㅠㅠ 윈도우 쓰시는 분들은 한 번 구글링 해보세요! From 2394ec6bee2c9954f931b32084cf01420abbddac Mon Sep 17 00:00:00 2001 From: SungchanJin <97156069+SungchanJin@users.noreply.github.com> Date: Tue, 25 Jan 2022 20:13:41 +0900 Subject: [PATCH 3/7] Add files via upload --- 3. DP/BJ_11066_sungchan/merging.cpp | 59 ++++++++++++++++++++++++ 3. DP/BJ_1328_sungchan/building.cpp | 70 +++++++++++++++++++++++++++++ 3. DP/BJ_3933_sungchan/lagrange.cpp | 63 ++++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 3. DP/BJ_11066_sungchan/merging.cpp create mode 100644 3. DP/BJ_1328_sungchan/building.cpp create mode 100644 3. DP/BJ_3933_sungchan/lagrange.cpp diff --git a/3. DP/BJ_11066_sungchan/merging.cpp b/3. DP/BJ_11066_sungchan/merging.cpp new file mode 100644 index 0000000..6da9ab0 --- /dev/null +++ b/3. DP/BJ_11066_sungchan/merging.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; +int T, *K, **file, **cumulated; +int cache[501][501]; + +int cost(int index, int start, int end) { + if(start == end) return 0; + + int& ret = cache[start][end]; + if(ret != -1) return ret; + + if(start + 1 == end) { + return ret = file[index][start] + file[index][end]; + } + if(start + 2 == end) { + int sum = cumulated[index][start+2]; + if(start > 0) sum -= cumulated[index][start-1]; + return ret = 2 * sum - max(file[index][start], file[index][start+2]); + } + + int answer = cumulated[index][end]; + if(start > 0) answer -= cumulated[index][start-1]; + + int minimum = INT32_MAX; + for(int i = start; i < end; i++) { + int cand = cost(index, start, i) + cost(index, i+1, end); + if(cand < minimum) minimum = cand; + } + + return ret = answer + minimum; +} + +int main() { + cin >> T; + K = new int[T]; + file = new int*[T]; + cumulated = new int*[T]; + for(int i = 0; i < T; i++) { + cin >> K[i]; + file[i] = new int[K[i]]; + cumulated[i] = new int[K[i]]; + cin >> file[i][0]; + cumulated[i][0] = file[i][0]; + for(int j = 1; j < K[i]; j++) { + cin >> file[i][j]; + cumulated[i][j] = cumulated[i][j-1] + file[i][j]; + } + } + + + for(int i = 0; i < T; i++) { + for(int j = 0; j <= 500; j++) + for(int k = 0; k <= 500; k++) + cache[j][k] = -1; + + cout << cost(i, 0, K[i]-1) << endl; + } +} \ No newline at end of file diff --git a/3. DP/BJ_1328_sungchan/building.cpp b/3. DP/BJ_1328_sungchan/building.cpp new file mode 100644 index 0000000..e6ee685 --- /dev/null +++ b/3. DP/BJ_1328_sungchan/building.cpp @@ -0,0 +1,70 @@ +#include +#include +using namespace std; +typedef long long ll; +const ll MOD = 1000000007; +ll com[100][100]; +ll fact[100]; +ll cache[100][100]; + +void combination(int num) { + if(num > 1) combination(num - 1); + else { + com[1][0] = com[1][1] = 1; + return; + } + com[num][0] = 1; + for(int i = 1; i <= num; i++) { + com[num][i] = (com[num-1][i] + com[num-1][i-1]) % MOD; + } +} + +void factorial(int num) { + if(num > 0) factorial(num - 1); + else { + fact[0] = 1; + return; + } + fact[num] = (fact[num-1] * num) % MOD; +} + +ll counter(int a, int b) { + ll& ret = cache[a][b]; + if(ret != -1) return ret; + + if(b == 1) return ret = fact[a-1]; + if(a == b) return ret = 1; + + ll ans = 0; + for(int i = b-1; i < a; i++) { + ans += (com[a-1][i] * (counter(i, b-1) * fact[a-i-1] % MOD)) % MOD; + } + return ret = ans % MOD; +} + +ll count(int n, int l, int r) { + if(l + r > n + 1) return 0; + if(n == 1) return 1; + if(l == 1 && r == 1) return 0; + + combination(n-1); + factorial(n-1); + + for(int i = 1; i < n; i++) + for(int j = 1; j < n; j++) + cache[i][j] = -1; + + if(l == 1 || r == 1) return counter(n-1, max(l, r)-1); + + ll ans = 0; + for(int i = l - 1; i <= n - r; i++) { + ans += (com[n-1][i] * (counter(i, l-1) * counter(n-i-1, r-1) % MOD)) % MOD; + } + return ans % MOD; +} + +int main() { + int N, L, R; + cin >> N >> L >> R; + cout << count(N, L, R) << endl; +} \ No newline at end of file diff --git a/3. DP/BJ_3933_sungchan/lagrange.cpp b/3. DP/BJ_3933_sungchan/lagrange.cpp new file mode 100644 index 0000000..a88f9d5 --- /dev/null +++ b/3. DP/BJ_3933_sungchan/lagrange.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; +int square[183]; +bool cache[32768][2]; + +int count(int num, int part) { + if(part == 1) { + if(cache[num][0]) return 0; + if(cache[num][1]) return 1; + } + + int ans = 0; + + if(part == 1) { + for(int i = 1; i <= 182; i++) { + if(square[i] == num) return cache[num][1] = 1; + if(square[i] > num) { + cache[num][0] = 1; + return 0; + } + } + } else if(part == 2) { + for(int i = 1; i <= 182; i++) { + if(square[i] > num/2) break; + ans += count(num - square[i], 1); + } + } else if(part == 3) { + for(int i = 1; i <= 182; i++) { + if(square[i] > num/3) break; + for(int j = i; j <= 182; j++) { + if(square[j] > (num - square[i])/2) break; + ans += count(num - square[i] - square[j], 1); + } + } + } else if(part == 4) { + for(int i = 1; i <= 182; i++) { + if(square[i] > num/4) break; + for(int j = i; j <= 182; j++) { + if(square[j] > (num - square[i])/3) break; + for(int k = j; k <= 182; k++) { + if(square[k] > (num - square[i] - square[j])/2) break; + ans += count(num - square[i] - square[j] - square[k], 1); + } + } + } + } + return ans; +} + +int main() { + for(int i = 0; i <= 182; i++) square[i] = i * i; + int* N = new int[255]; + int number = 0; + for(int i = 0; i < 255; i++) { + cin >> N[i]; + if(N[i] == 0) break; + else number++; + } + + for(int i = 0; i < number; i++) { + cout << count(N[i], 1) + count(N[i], 2) + count(N[i], 3) + count(N[i], 4) << endl; + } +} \ No newline at end of file From 61b62ac4b732449ac18b9506c965dba000f4b6be Mon Sep 17 00:00:00 2001 From: SungchanJin <97156069+SungchanJin@users.noreply.github.com> Date: Mon, 7 Feb 2022 22:34:58 +0900 Subject: [PATCH 4/7] Add files via upload --- 4. Greedy/BJ_1041_sungchan/dice.cpp | 52 +++++++++++++ 4. Greedy/BJ_1045_sungchan/road.cpp | 105 ++++++++++++++++++++++++++ 4. Greedy/BJ_14698_sungchan/slime.cpp | 40 ++++++++++ 4. Greedy/BJ_1715_sungchan/card.cpp | 27 +++++++ 4 files changed, 224 insertions(+) create mode 100644 4. Greedy/BJ_1041_sungchan/dice.cpp create mode 100644 4. Greedy/BJ_1045_sungchan/road.cpp create mode 100644 4. Greedy/BJ_14698_sungchan/slime.cpp create mode 100644 4. Greedy/BJ_1715_sungchan/card.cpp diff --git a/4. Greedy/BJ_1041_sungchan/dice.cpp b/4. Greedy/BJ_1041_sungchan/dice.cpp new file mode 100644 index 0000000..6176a68 --- /dev/null +++ b/4. Greedy/BJ_1041_sungchan/dice.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +int dice[6]; +typedef long long ll; +ll N; + +int main() { + cin >> N; + + int min1 = 50; + for(int i = 0; i < 6; i++) { + cin >> dice[i]; + if(dice[i] < min1) min1 = dice[i]; + } + + if(N == 1) { + int max = 0; + int sum = 0; + for(int i = 0; i < 6; i++) { + if(dice[i] > max) max = dice[i]; + sum += dice[i]; + } + cout << sum - max; + return 0; + } + + int min2 = 100; + for(int i = 0; i < 5; i++) { + for(int j = i+1; j < 6; j++) { + if(i+j != 5) { + if(dice[i] + dice[j] < min2) min2 = dice[i] + dice[j]; + } + } + } + + int min3 = 150; + for(int i = 1; i < 4; i++) { + for(int j = i+1; j < 5; j++) { + if(i+j != 5) { + if(dice[0] + dice[i] + dice[j] < min3) min3 = dice[0] + dice[i] + dice[j]; + if(dice[5] + dice[i] + dice[j] < min3) min3 = dice[5] + dice[i] + dice[j]; + } + } + } + + + ll one = (5*N*N - 16*N + 12) * (min1); + ll two = (8*N - 12) * (min2); + ll three = 4 * (min3); + + cout << one + two + three; +} \ No newline at end of file diff --git a/4. Greedy/BJ_1045_sungchan/road.cpp b/4. Greedy/BJ_1045_sungchan/road.cpp new file mode 100644 index 0000000..1000e2a --- /dev/null +++ b/4. Greedy/BJ_1045_sungchan/road.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +using namespace std; +int N, M; + +int main() { + cin >> N >> M; + int cnt = 0; + char c; + bool **connected = new bool*[N]; + bool impossible = false; + for(int i = 0; i < N; i++) { + bool impossible1 = true; + connected[i] = new bool[N]; + for(int j = 0; j < N; j++) { + cin >> c; + if(c == 'Y') { + connected[i][j] = true; + cnt++; + impossible1 = false; + } + } + if(impossible1) impossible = true; + } + cnt /= 2; + + if(N == 1 && M == 0) { + cout << 0; + return 0; + } + if(cnt < M || impossible) { + cout << -1; + return 0; + } + + bool *connect = new bool[N]; + int *road = new int[N]; + int total = 0; + + priority_queue, greater<>> cost; + for(int i = 1; i < N; i++) { + if(connected[0][i]) { + connect[i] = true; + road[0]++; + road[i]++; + total++; + for(int j = 1; j < N; j++) { + if(connected[i][j] && !connect[j]) { + cost.push(N*min(i, j)+max(i, j)); + } + } + } + } + + while(!cost.empty() && total < N-1) { + int a = cost.top(); + cost.pop(); + int b = a / N; + int bb = a % N; + if(!connect[b] || !connect[bb]) { + road[b]++; + road[bb]++; + total++; + connected[b][bb] = false; + if(!connect[b]) { + for(int i = 1; i < N; i++) { + if(connected[b][i] && !connect[i]) { + cost.push(N*min(b,i)+max(b,i)); + } + } + } else { // !connect[bb] + for(int i = 1; i < N; i++) { + if(connected[bb][i] && !connect[i]) { + cost.push(N*min(bb,i)+max(bb,i)); + } + } + } + connect[b] = connect[bb] = true; + } + } + + + if(total < N-1) { + cout << -1; + return 0; + } + + for(int i = 1; i < N-1; i++) { + if(total == M) break; + for(int j = i+1; j < N; j++) { + if(total == M) break; + if(connected[i][j]) { + road[i]++; + road[j]++; + total++; + } + } + } + + for(int i = 0; i < N; i++) { + cout << road[i] << " "; + } +} \ No newline at end of file diff --git a/4. Greedy/BJ_14698_sungchan/slime.cpp b/4. Greedy/BJ_14698_sungchan/slime.cpp new file mode 100644 index 0000000..fee910d --- /dev/null +++ b/4. Greedy/BJ_14698_sungchan/slime.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; +int T, *N; +typedef unsigned long long ll; +ll MOD = 1e9+7; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(0); + + cin >> T; + N = new int[T]; + ll *product = new ll[T]; + for(int i = 0; i < T; i++) { + cin >> N[i]; + ll ener; + priority_queue, greater<>> energy; + for(int j = 0; j < N[i]; j++) { + cin >> ener; + energy.push(ener); + } + + product[i] = 1; + while(energy.size() > 1) { + ll num1 = energy.top(); + energy.pop(); + ll num2 = energy.top(); + energy.pop(); + energy.push(num1 * num2); + product[i] *= ((num1 % MOD) * (num2 % MOD)) % MOD; + product[i] %= MOD; + } + } + for(int i = 0; i < T; i++) { + cout << product[i] << "\n"; + } +} \ No newline at end of file diff --git a/4. Greedy/BJ_1715_sungchan/card.cpp b/4. Greedy/BJ_1715_sungchan/card.cpp new file mode 100644 index 0000000..e312179 --- /dev/null +++ b/4. Greedy/BJ_1715_sungchan/card.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +using namespace std; +int N; + +int main() { + cin >> N; + priority_queue, greater<>> numbers; + int number; + for(int i = 0; i < N; i++) { + cin >> number; + numbers.push(number); + } + + int ans = 0; + while(numbers.size() > 1) { + int num1 = numbers.top(); + numbers.pop(); + int num2 = numbers.top(); + numbers.pop(); + numbers.push(num1 + num2); + ans += (num1 + num2); + } + cout << ans; +} \ No newline at end of file From 8cb1d3b4703a66990d3a6730e70521c5ec0c0ebe Mon Sep 17 00:00:00 2001 From: SungchanJin <97156069+SungchanJin@users.noreply.github.com> Date: Tue, 8 Feb 2022 12:50:01 +0900 Subject: [PATCH 5/7] Add files via upload --- 4. Greedy/BJ_1092_sungchan/ship.cpp | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 4. Greedy/BJ_1092_sungchan/ship.cpp diff --git a/4. Greedy/BJ_1092_sungchan/ship.cpp b/4. Greedy/BJ_1092_sungchan/ship.cpp new file mode 100644 index 0000000..e0340a9 --- /dev/null +++ b/4. Greedy/BJ_1092_sungchan/ship.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +using namespace std; +int N, M; +int *crane, *box; + +int main() { + cin >> N; + crane = new int[N]; + for(int i = 0; i < N; i++) { + cin >> crane[i]; + } + sort(crane, crane+N); + + cin >> M; + box = new int[M]; + for(int i = 0; i < M; i++) { + cin >> box[i]; + } + sort(box, box+M); + + if(crane[N-1] < box[M-1]) { + cout << -1; + return 0; + } + + if(N == 1) { + cout << M; + return 0; + } + + int *loadable = new int[M]; + int start = N-2; + for(int i = M-1; i >= 0; i--) { + bool done = false; + for(int j = start; j >= 0; j--) { + if(box[i] > crane[j]) { + loadable[i] = N - j - 1; + start = j; + done = true; + break; + } + } + if(!done) { + for(int j = i; j >= 0; j--) { + loadable[j] = N; + } + break; + } + } + + int ans = 1; + for(int i = 2; i <= M; i++) { + int tmp = i/loadable[M-i] + (i % loadable[M-i] != 0); + if(tmp > ans) ans = tmp; + } + cout << ans; +} \ No newline at end of file From 0924e2cb239606662226360106830296675384c4 Mon Sep 17 00:00:00 2001 From: SungchanJin <97156069+SungchanJin@users.noreply.github.com> Date: Tue, 15 Feb 2022 20:49:24 +0900 Subject: [PATCH 6/7] Add files via upload --- .../BJ_1477_sungchan/restarea.cpp" | 47 ++++++++++ .../BJ_1561_sungchan/amusement.cpp" | 56 ++++++++++++ .../BJ_1939_sungchan/weightlimit.cpp" | 71 ++++++++++++++++ .../BJ_2470_sungchan/solution.cpp" | 85 +++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 "5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1477_sungchan/restarea.cpp" create mode 100644 "5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1561_sungchan/amusement.cpp" create mode 100644 "5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1939_sungchan/weightlimit.cpp" create mode 100644 "5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_2470_sungchan/solution.cpp" diff --git "a/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1477_sungchan/restarea.cpp" "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1477_sungchan/restarea.cpp" new file mode 100644 index 0000000..18e7244 --- /dev/null +++ "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1477_sungchan/restarea.cpp" @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace std; +int N, M, L; + +int main() { + cin >> N >> M >> L; + int* rest = new int[N]; + for(int i = 0; i < N; i++) { + cin >> rest[i]; + } + sort(rest, rest+N); + + int* gap = new int[N+1]; + gap[0] = rest[0]; + for(int i = 1; i < N; i++) { + gap[i] = rest[i] - rest[i-1]; + } + gap[N] = L - rest[N-1]; + sort(gap, gap+(N+1), greater<>()); + int* index = new int[N+1]; + for(int i = 0; i <= N; i++) index[i] = 1; + + for(int i = 0; i < M; i++) { + bool done = false; + int max = ceil((double) gap[0] / (double) index[0]); + for(int i = 1; i <= N; i++) { + if(ceil((double) gap[i] / (double) index[i]) > max) { + index[i]++; + done = true; + break; + } + } + if(!done) index[0]++; + } + + int ans = ceil((double) gap[0] / (double) index[0]); + for(int i = 1; i <= N; i++) { + int cand = ceil((double) gap[i] / (double) index[i]); + if(cand > ans) ans = cand; + } + + cout << ans; +} \ No newline at end of file diff --git "a/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1561_sungchan/amusement.cpp" "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1561_sungchan/amusement.cpp" new file mode 100644 index 0000000..5779c64 --- /dev/null +++ "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1561_sungchan/amusement.cpp" @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; +typedef long long ll; +int N, M; + +int main() { + cin >> N >> M; + int* time = new int[M]; + for(int i = 0; i < M; i++) { + cin >> time[i]; + } + + if(N <= M) { + cout << N; + return 0; + } + + ll low = 0; + ll high = 30 * (ll)(ceil((double) N / (double) M)); + ll mid = (low+high)/2; + + while(true) { + ll cnt = M; + for(int i = 0; i < M; i++) { + cnt += mid / time[i]; + if(cnt >= N) break; + } + if(cnt >= N) high = mid; + else if(cnt < N) low = mid; + mid = (low+high)/2; + if(low+1 == high && cnt < N) { + mid = high; + break; + } + } + + int cnt = M; + for(int i = 0; i < M; i++) { + cnt += (mid-1)/ time[i]; + } + int num = N-cnt; + int ans = -1; + for(int i = 0; i < M; i++) { + if(mid % time[i] == 0) { + num--; + } + if(num == 0) { + ans = i+1; + break; + } + } + + cout << ans; +} \ No newline at end of file diff --git "a/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1939_sungchan/weightlimit.cpp" "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1939_sungchan/weightlimit.cpp" new file mode 100644 index 0000000..12cc49a --- /dev/null +++ "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_1939_sungchan/weightlimit.cpp" @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +using namespace std; +int N, M; + +int main() { + cin >> N >> M; + vector> bridge(N+1); + + int A, B, C; + for(int i = 0; i < M; i++) { + cin >> A >> B >> C; + if(bridge[A].find(B) == bridge[A].end()) { + bridge[A][B] = C; + bridge[B][A] = C; + } else if(C > bridge[A][B]) { + bridge[A][B] = C; + bridge[B][A] = C; + } + } + + int src, dest; + cin >> src >> dest; + + bool* connected = new bool[N+1]; + int* limit = new int[N+1]; + int* neighbor = new int[N+1]; + connected[src] = true; + for(int i = 1; i <= N; i++) { + if(i == src) continue; + if(bridge[i].find(src) != bridge[i].end()) { + limit[i] = bridge[i].at(src); + neighbor[i] = src; + } else { + limit[i] = -1; + } + } + + while(!connected[dest]) { + int maximum = -1; + int max_index = -1; + for(int i = 1; i <= N; i++) { + if(!connected[i] && limit[i] != -1) { + if(limit[i] > maximum) { + maximum = limit[i]; + max_index = i; + } + } + } + connected[max_index] = true; + for(int i = 1; i <= N; i++) { + if(!connected[i] && bridge[max_index].find(i) != bridge[max_index].end()) { + if(bridge[max_index].at(i) > limit[i]) { + limit[i] = bridge[max_index].at(i); + neighbor[i] = max_index; + } + } + } + } + + int index = dest; + int minimum = INT32_MAX; + while(index != src) { + minimum = min(minimum, limit[index]); + index = neighbor[index]; + } + cout << minimum; +} \ No newline at end of file diff --git "a/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_2470_sungchan/solution.cpp" "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_2470_sungchan/solution.cpp" new file mode 100644 index 0000000..bce07c7 --- /dev/null +++ "b/5. \341\204\213\341\205\265\341\204\207\341\205\256\341\206\253\341\204\220\341\205\241\341\206\267\341\204\211\341\205\242\341\206\250/BJ_2470_sungchan/solution.cpp" @@ -0,0 +1,85 @@ +#include +#include +#include +using namespace std; +int N; + +int main() { + cin >> N; + int* acid = new int[N]; + for(int i = 0; i < N; i++) { + cin >> acid[i]; + } + sort(acid, acid+N); + + int acid_index = -1; + for(int i = 0; i < N; i++) { + if(acid[i] > 0) { + acid_index = i; + break; + } + } + if(acid_index == -1) { + cout << acid[N-2] << " " << acid[N-1]; + return 0; + } + if(acid_index == 0) { + cout << acid[0] << " " << acid[1]; + return 0; + } + + int minimum = INT32_MAX; + pair index; + int limit = N-1; + for(int i = 0; i < acid_index; i++) { + bool done = false; + for(int j = acid_index; j <= limit; j++) { + int cand = acid[i]+acid[j]; + if(cand > 0) { + if(cand < minimum) { + minimum = cand; + index.first = i; + index.second = j; + limit = j; + } + if(j > acid_index) { + int cand2 = -(acid[i]+acid[j-1]); + if(cand2 < minimum) { + minimum = cand2; + index.first = i; + index.second = j-1; + } + } + done = true; + break; + } else if(cand == 0) { + cout << acid[i] << " " << acid[j]; + return 0; + } + } + if(!done) { + int cand3 = -(acid[i] + acid[limit]); + if(cand3 < minimum) { + minimum = cand3; + index.first = i; + index.second = limit; + } + } + } + + if(acid_index > 1) { + if(acid[acid_index-2] + acid[acid_index-1] > -minimum) { + minimum = -(acid[acid_index-2] + acid[acid_index-1]); + index.first = acid_index-2; + index.second = acid_index-1; + } + } + if(N-acid_index > 1) { + if(acid[acid_index] + acid[acid_index+1] < minimum) { + index.first = acid_index; + index.second = acid_index+1; + } + } + + cout << acid[index.first] << " " << acid[index.second]; +} \ No newline at end of file From 0462198f8c6cfdcbdb342afd93ecfec548ad9d5a Mon Sep 17 00:00:00 2001 From: SungchanJin <97156069+SungchanJin@users.noreply.github.com> Date: Tue, 22 Feb 2022 20:15:26 +0900 Subject: [PATCH 7/7] Add files via upload --- 6. DFS & BFS/BJ_10891_sungchan/cactus.cpp | 78 ++++++++++++++++++++++ 6. DFS & BFS/BJ_2021_sungchan/transfer.cpp | 73 ++++++++++++++++++++ 6. DFS & BFS/BJ_3584_sungchan/dinosaur.cpp | 41 ++++++++++++ 6. DFS & BFS/BJ_4196_sungchan/domino.cpp | 71 ++++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 6. DFS & BFS/BJ_10891_sungchan/cactus.cpp create mode 100644 6. DFS & BFS/BJ_2021_sungchan/transfer.cpp create mode 100644 6. DFS & BFS/BJ_3584_sungchan/dinosaur.cpp create mode 100644 6. DFS & BFS/BJ_4196_sungchan/domino.cpp diff --git a/6. DFS & BFS/BJ_10891_sungchan/cactus.cpp b/6. DFS & BFS/BJ_10891_sungchan/cactus.cpp new file mode 100644 index 0000000..c58dddc --- /dev/null +++ b/6. DFS & BFS/BJ_10891_sungchan/cactus.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +const int MAX = 100001; +typedef pair P; + +int N, M, dcnt, dfsn[MAX]; +vector adj[MAX]; +stack

S; +vector> bcc; + +int DFS(int curr, int prev = -1) { + int result = dfsn[curr] = ++dcnt; + + for(int next: adj[curr]) { + if(next == prev) continue; + if(dfsn[curr] > dfsn[next]) S.push(P(curr, next)); + if(dfsn[next] > 0) result = min(result, dfsn[next]); + else { + int temp = DFS(next, curr); + result = min(result, temp); + if(temp >= dfsn[curr]) { + vector

currBCC; + while(!S.empty() && S.top() != P(curr, next)) { + currBCC.push_back(S.top()); + S.pop(); + } + currBCC.push_back(S.top()); + S.pop(); + bcc.push_back(currBCC); + } + } + } + + return result; +} + +int main() { + cin >> N >> M; + int x, y; + for(int i = 0; i < M; i++) { + cin >> x >> y; + adj[x].push_back(y); + adj[y].push_back(x); + } + + for(int i = 0; i < N; i++) { + if(dfsn[i] == 0) DFS(i); + } + + bool cactus = true; + if(bcc.size() == 1) { + if(M > N) cactus = false; + } + vector checked(N); + for(auto &c: bcc) { + if(c.size() == 1) continue; + set nodes; + for(auto &p: c) { + nodes.insert(p.first); + nodes.insert(p.second); + } + for(int node: nodes) { + if(checked[node]) cactus = false; + checked[node] = true; + } + int n = nodes.size(); + int m = c.size(); + if(m > n) cactus = false; + } + + if(cactus) cout << "Cactus"; + else cout << "Not cactus"; +} \ No newline at end of file diff --git a/6. DFS & BFS/BJ_2021_sungchan/transfer.cpp b/6. DFS & BFS/BJ_2021_sungchan/transfer.cpp new file mode 100644 index 0000000..166b730 --- /dev/null +++ b/6. DFS & BFS/BJ_2021_sungchan/transfer.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +using namespace std; +int N, L; +int station, src, dest; + +int main() { + cin >> N >> L; + vector> line(L+1); + for(int i = 1; i <= L; i++) { + cin >> station; + while(station != -1) { + line[i].push_back(station); + cin >> station; + } + } + + vector> link(N+1); + for(int i = 1; i <= L; i++) { + for(int j: line[i]) { + link[j].push_back(i); + } + } + cin >> src >> dest; + if(src == dest) { + cout << 0; + return 0; + } + + if(link[src].empty()) { + cout << -1; + return 0; + } + + vector visited_line(L+1, false); // 방문한 호선 + vector visited_station(N+1, false); // 방문한 역 + set tovisit; // 방문할 호선 + + for(int i: link[src]) { + tovisit.insert(i); // src와 연결된 호선 추가 + } + + visited_station[src] = true; // src 방문 체크 + + int ans = 0; + while(!tovisit.empty()) { // 방문할 호선이 있을 때 반복 + for(int i: tovisit) { + visited_line[i] = true; // 호선 방문 체크 + } + set temp(tovisit); + for(int i : tovisit) { // ans번 환승하여 방문할 호선들 + for(int j: line[i]) { // 각 호선에 연결된 역들 + if(visited_station[j]) continue; + visited_station[j] = true; + if(j == dest) { + cout << ans; + return 0; + } + for(int k: link[j]) { + if(!visited_line[k]) { + temp.insert(k); + } + } + } + temp.erase(i); + } + tovisit = temp; + ans++; + } + + cout << -1; // 불가능한 경우 +} \ No newline at end of file diff --git a/6. DFS & BFS/BJ_3584_sungchan/dinosaur.cpp b/6. DFS & BFS/BJ_3584_sungchan/dinosaur.cpp new file mode 100644 index 0000000..5d66aba --- /dev/null +++ b/6. DFS & BFS/BJ_3584_sungchan/dinosaur.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; +int T, N; + +int main() { + cin >> T; + while(T--) { + int ans = 0; + cin >> N; + vector parent(N+1, 0); + int A, B; + for(int i = 0; i < N-1; i++) { + cin >> A >> B; + parent[B] = A; + } + cin >> A >> B; + if(A == B || parent[A] == 0) ans = A; + else if(parent[B] == 0) ans = B; + else { + vector parents; + parents.push_back(A); + while(parent[A] != 0) { + A = parent[A]; + parents.push_back(A); + } + bool done = false; + while(!done) { + for(int i : parents) { + if(B == i) { + ans = B; + done = true; + break; + } + } + B = parent[B]; + } + } + cout << ans << endl; + } +} \ No newline at end of file diff --git a/6. DFS & BFS/BJ_4196_sungchan/domino.cpp b/6. DFS & BFS/BJ_4196_sungchan/domino.cpp new file mode 100644 index 0000000..e137340 --- /dev/null +++ b/6. DFS & BFS/BJ_4196_sungchan/domino.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; +const int MAX = 100001; +int T, N, M; +int cnt, dfsn[MAX], SN, sn[MAX]; +vector adj[MAX]; +bool finished[MAX]; +stack S; + +int DFS(int curr) { + dfsn[curr] = ++cnt; + S.push(curr); + + int result = dfsn[curr]; + for(int next : adj[curr]) { + if(dfsn[next] == 0) result = min(result, DFS(next)); + else if(!finished[next]) result = min(result, dfsn[next]); + } + + if(result == dfsn[curr]) { + while(true) { + int t = S.top(); + S.pop(); + finished[t] = true; + sn[t] = SN; + if(t == curr) break; + } + SN++; + } + return result; +} + +int main() { + cin >> T; + while(T--) { + cin >> N >> M; + cnt = SN = 0; + for(int i = 1; i <= N; i++) { + dfsn[i] = sn[i] = 0; + adj[i].clear(); + finished[i] = false; + } + + int x, y; + for(int i = 0; i < M; i++) { + cin >> x >> y; + adj[x].push_back(y); + } + for(int i = 1; i <= N; i++) { + if(dfsn[i] == 0) DFS(i); + } + + vector indegree(SN, 0); + for(int i = 1; i <= N; i++) { + for(int j: adj[i]) { + if(sn[i] != sn[j]) { + indegree[sn[j]]++; + } + } + } + + int ans = 0; + for(int i = 0; i < SN; i++) { + if(indegree[i] == 0) ans++; + } + cout << ans << endl; + } +} \ No newline at end of file