├── .github └── workflows │ └── stale.yml ├── .gitignore ├── LICENSE ├── README.md ├── 강의 자료 ├── 01-코딩 테스트를 위한 준비 │ ├── 01_코딩 테스트를 위한 준비_01.pdf │ ├── 01_코딩 테스트를 위한 준비_02.pdf │ └── 01_코딩 테스트를 위한 준비_03.pdf ├── 02-알고리즘 │ ├── 01~02-완전 탐색 │ │ ├── 02-알고리즘-01-완전탐색.pdf │ │ ├── 02-알고리즘-02-완전탐색-응용편.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1182-부분수열의 합 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 14888-연산자 끼워넣기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15649-N과 M(1) │ │ │ ├── solution-advanced.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15650-N과 M(2) │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15651-N과 M(3) │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15652-N과 M(4) │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15663-N과 M(9) │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1759-암호 만들기 │ │ │ ├── solution copy.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 9663-N Queen │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 03~04-정렬 │ │ ├── 02-알고리즘-03-정렬.pdf │ │ ├── 02-알고리즘-04-정렬-응용편.pdf │ │ ├── README.md │ │ ├── 문제별 코드 │ │ │ ├── 1015-수열 정렬 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ │ ├── 10825-국영수 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ │ ├── 11652-카드 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ │ ├── 1181-단어 정렬 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ │ ├── 15970-화살표 그리기 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ │ └── 20291-파일 정리 │ │ │ │ ├── base_code.java │ │ │ │ ├── solution.cpp │ │ │ │ ├── solution.java │ │ │ │ └── solution.py │ │ └── 화살표 그리기 별첨.pdf │ ├── 05~06-이분 탐색 │ │ ├── 02-알고리즘-05-이분 탐색.pdf │ │ ├── 02-알고리즘-06-매개 변수 탐색.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 10816-숫자 카드 2 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1300-K 번째 수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 13702-이상한 술집 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1637-날카로운 눈 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1654-랜선 자르기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 17266-어두운 굴다리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1764-듣보잡 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1920-수 찾기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2110-공유기 설치 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2343-기타 레슨 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2470-두 용액 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2512-예산 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2805-나무 자르기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 3273-두 수의 합 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 6236-용돈 관리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 7795-먹을 것인가 먹힐 것인가 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 07~08-두 포인터 │ │ ├── 02-알고리즘-07-두 포인터.pdf │ │ ├── 02-알고리즘-08-두 포인터-응용편.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 11728-배열 합치기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1253-좋다 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 13144-List of Unique Numbers │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15565-귀여운 라이언 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 16472-고냥이 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1806-부분 합 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2003-수들의 합 2 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2230-수 고르기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2470-두 용액 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2473-세 용액 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2559-수열 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 3273-두 수의 합 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 09~11-그래프 탐색 │ │ ├── 02-알고리즘-09-그래프와 탐색.pdf │ │ ├── 02-알고리즘-10-그래프와 탐색-응용.pdf │ │ ├── 02-알고리즘-11-그래프와 탐색-응용-2.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1012-유기농 배추 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11403-경로 찾기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11724-연결 요소의 개수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11725-트리의 부모 찾기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1260-DFS와 BFS │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.py │ │ │ ├── solution_list.java │ │ │ └── solution_matrix.java │ │ │ ├── 1389-케빈 베이컨의 6단계 법칙 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 14502-연구소 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1697-숨바꼭질 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 18404-현명한 나이트 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2178-미로 탐색 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2251-물통 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2606-바이러스 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2644-촌수 계산 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2667-단지번호 붙이기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 3055-탈출 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 3184-양 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 4963-섬의 개수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 5567-결혼식 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 7562-나이트의 이동 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 7569-토마토 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 12-트리 │ │ ├── 02-알고리즘-12-트리.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1068-트리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11725-트리의 부모 찾기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1240-노드 사이의 거리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 14267-회사 문화 1 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15681-트리와 쿼리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15900-나무 탈출 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1991-트리 순회 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 20364-부동산 다툼 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 3584-가장 가까운 공통 조상 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 4803-트리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 5639-이진 검색 트리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 9489-사촌 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 13-위상정렬 │ │ ├── 02-알고리즘-13-위상정렬.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1005-ACM Craft │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 14676-영우는 사기꾼 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1516-게임 개발 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2056-작업 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2252-줄 세우기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2623-음악 프로그램 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2637-장난감 조립 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 9470-Strahler 순서 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 14-최단거리 │ │ ├── 02-알고리즘-14-다익스트라.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1753-최단경로 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 1916-최소비용 구하기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ ├── 15~17-동적 프로그래밍 │ │ ├── 02-알고리즘-15-동적프로그래밍.pdf │ │ ├── 02-알고리즘-16-동적프로그래밍.pdf │ │ ├── 02-알고리즘-17-동적프로그래밍.pdf │ │ ├── README.md │ │ └── 문제별 코드 │ │ │ ├── 1003-피보나치 함수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 10870-피보나치 수 5 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11052-카드 구매하기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11057-오르막 수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11066-파일 합치기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1149-RGB 거리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 11726-2xN 타일링 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1309-동물원 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1495-기타리스트 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1562-계단 수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15681-트리와 쿼리 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15988-1, 2, 3 더하기 3 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15990-1, 2, 3 더하기 5 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 15991-1, 2, 3 더하기 6 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 1949-우수 마을 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2011-암호코드 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2096-내려가기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2156-포도주 시식 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2193-이친수 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2579-계단 오르기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 2688-줄어들지 않아 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 5557-1학년 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ ├── 9095-1, 2, 3 더하기 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ │ │ └── 9465-스티커 │ │ │ ├── base_code.java │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ └── solution.py │ └── autoindex.py ├── 03-모의 코딩테스트 │ ├── 03-코딩테스트-1.pdf │ ├── 03-코딩테스트-2.pdf │ └── 03-코딩테스트-3.pdf ├── README.md └── 단원 리스트.md └── 류호석배 알고리즘 코딩 테스트 ├── README.md ├── 제1회 ├── 1번-홀수 홀릭 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 2번-인내의 도미노 장인 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 3번-문자열 지옥에 빠진 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 4번-꿈틀꿈틀 호석 애벌레 │ ├── solution-2.java │ ├── solution.cpp │ ├── solution.java │ ├── solution.py │ ├── subsolution(brute force).cpp │ └── subsolution(brute force).java ├── 5번-골목 대장 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py └── README.md ├── 제2회 ├── 1번-폰 호석만 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 2번-계보 복원가 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 3번-짠돌이 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 4번-호석이 두 마리 치킨 │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── 5번-광부 호석 │ ├── solution.cpp │ ├── solution.java │ └── solution.py └── README.md └── 제3회 ├── 1번-빌런 호석 ├── solution.cpp ├── solution.java └── solution.py ├── 2번-정보 상인 호석 ├── solution.cpp ├── solution.java └── solution.py ├── 3번-트리 디자이너 호석 ├── solution.cpp ├── solution.java └── solution.py ├── 4번-공정 컨설턴트 호석 ├── solution.cpp ├── solution.java └── solution.py ├── 5번-호석사우루스 ├── solution.cpp └── solution.java └── README.md /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | name: Mark stale issues and pull requests 2 | 3 | on: 4 | schedule: 5 | - cron: "30 1 * * *" 6 | 7 | jobs: 8 | stale: 9 | 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/stale@v3 14 | with: 15 | repo-token: ${{ secrets.GITHUB_TOKEN }} 16 | stale-issue-message: 'Stale issue message' 17 | stale-pr-message: 'Stale pull request message' 18 | stale-issue-label: 'no-issue-activity' 19 | stale-pr-label: 'no-pr-activity' 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Hoseok Ryu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_01.pdf -------------------------------------------------------------------------------- /강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_02.pdf -------------------------------------------------------------------------------- /강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/01-코딩 테스트를 위한 준비/01_코딩 테스트를 위한 준비_03.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/02-알고리즘-01-완전탐색.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/01~02-완전 탐색/02-알고리즘-01-완전탐색.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/02-알고리즘-02-완전탐색-응용편.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/01~02-완전 탐색/02-알고리즘-02-완전탐색-응용편.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1182-부분수열의 합/base_code.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1182-부분수열의 합/base_code.java -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1182-부분수열의 합/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, S, nums[25], ans; 6 | void input() { 7 | cin >> N >> S; 8 | for (int i=1;i<=N;i++) 9 | cin >> nums[i]; 10 | } 11 | 12 | void rec_func(int k, int value) { 13 | if (k == N + 1) { 14 | if (value == S) ans++; 15 | return; 16 | } 17 | rec_func(k + 1, value + nums[k]); 18 | rec_func(k + 1, value); 19 | } 20 | int main() { 21 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 22 | input(); 23 | rec_func(1, 0); 24 | if (S == 0) ans--; 25 | cout << ans; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1182-부분수열의 합/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, S = map(int, sys.stdin.readline().split(' ')) 3 | nums = list(map(int, sys.stdin.readline().split(' '))) 4 | ans = 0 5 | 6 | def rec_func(k, value): 7 | if k == n: 8 | global ans 9 | if value == S: 10 | ans += 1 11 | 12 | else: 13 | rec_func(k + 1, value + nums[k]) 14 | rec_func(k + 1, value) 15 | 16 | rec_func(0, 0) 17 | if S == 0: 18 | ans -= 1 19 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/14888-연산자 끼워넣기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | 6 | int N, nums[15], operators[5], ans_max, ans_min; 7 | void input() { 8 | cin >> N; 9 | for (int i=1;i<=N;i++) cin >> nums[i]; 10 | for (int i=1;i<=4;i++) cin >> operators[i]; 11 | ans_max = -0x80000000; 12 | ans_min = 0x7fffffff; 13 | } 14 | 15 | // 피연산자 2개와 연산자가 주어졌을 때 계산해주는 함수 16 | int calculator(int operand1, int op, int operand2){ 17 | // value, order[i], num[i+1] 18 | if (op == 1) // + 19 | return operand1 + operand2; 20 | else if (op == 2) // - 21 | return operand1 - operand2; 22 | else if (op == 3) // * 23 | return operand1 * operand2; 24 | else // / 25 | return operand1 / operand2; 26 | } 27 | 28 | void rec_func(int k,int value) { 29 | if (k == N) { 30 | ans_max = max(ans_max, value); 31 | ans_min = min(ans_min, value); 32 | return; 33 | } 34 | for (int cand = 1; cand <= 4; cand++) { 35 | if (operators[cand] >= 1){ 36 | operators[cand]--; 37 | rec_func(k + 1, calculator(value, cand, nums[k + 1])); 38 | operators[cand]++; 39 | } 40 | } 41 | } 42 | int main() { 43 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 44 | input(); 45 | rec_func(1, nums[1]); 46 | cout << ans_max << '\n' << ans_min; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/14888-연산자 끼워넣기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n = int(sys.stdin.readline()) 3 | nums = list(map(int, sys.stdin.readline().split(' '))) 4 | operators = list(map(int, sys.stdin.readline().split(' '))) 5 | min = 1e9 6 | max = -1e9 7 | 8 | def calculator(operand1, operator, operand2): 9 | if operator == 0: 10 | return operand1 + operand2 11 | if operator == 1: 12 | return operand1 - operand2 13 | if operator == 2: 14 | return operand1 * operand2 15 | if operator == 3: 16 | if operand1 < 0: 17 | return - ((-operand1) // operand2) 18 | else: 19 | return operand1 // operand2 20 | 21 | def rec_func(k, value): 22 | if k == n - 1: 23 | global min, max 24 | min = min if min < value else value 25 | max = max if max > value else value 26 | else: 27 | global operators 28 | for cand in range(4): 29 | if operators[cand] >= 1: 30 | operators[cand] -= 1 31 | rec_func(k + 1, calculator(value, cand, nums[k + 1])) 32 | operators[cand] += 1 33 | 34 | rec_func(0, nums[0]) 35 | print(max) 36 | print(min) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15649-N과 M(1)/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, M, selected[10], used[10]; 6 | void input() { 7 | cin >> N >> M; 8 | } 9 | 10 | void rec_func(int k) { 11 | if (k == M + 1) { 12 | for (int i = 1; i <= M; i++) 13 | cout << selected[i] << " "; 14 | cout << "\n"; 15 | return; 16 | } 17 | for (int cand = 1; cand <= N; cand++) { 18 | if (used[cand]) continue; 19 | used[cand] = 1; 20 | selected[k] = cand; 21 | rec_func(k + 1); 22 | used[cand] = 0; 23 | selected[k] = 0; 24 | } 25 | } 26 | int main() { 27 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 28 | input(); 29 | rec_func(1); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15649-N과 M(1)/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().split(' ')) 3 | 4 | selected = [0 for _ in range(m)] 5 | used = [0 for _ in range(n + 1)] 6 | def rec_func(k): 7 | if k == m: 8 | for x in selected: 9 | sys.stdout.write(str(x) + ' ') 10 | sys.stdout.write('\n') 11 | else: 12 | for cand in range(1, n + 1): 13 | if used[cand]: 14 | continue 15 | selected[k] = cand 16 | used[cand] = 1 17 | rec_func(k + 1) 18 | selected[k] = 0 19 | used[cand] = 0 20 | 21 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15650-N과 M(2)/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, M, selected[10]; 6 | void input() { 7 | cin >> N >> M; 8 | } 9 | 10 | void rec_func(int k) { 11 | if (k == M + 1) { 12 | for (int i = 1; i <= M; i++) 13 | cout << selected[i] << " "; 14 | cout << "\n"; 15 | return; 16 | } 17 | for (int cand = selected[k - 1] + 1; cand <= N; cand++) { 18 | selected[k] = cand; 19 | rec_func(k + 1); 20 | selected[k] = 0; 21 | } 22 | } 23 | int main() { 24 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 25 | input(); 26 | rec_func(1); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15650-N과 M(2)/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().split(' ')) 3 | 4 | selected = [0 for _ in range(m)] 5 | used = [0 for _ in range(n + 1)] 6 | def rec_func(k): 7 | if k == m: 8 | for x in selected: 9 | sys.stdout.write(str(x) + ' ') 10 | sys.stdout.write('\n') 11 | else: 12 | start = 1 if k == 0 else selected[k - 1] + 1 13 | for cand in range(start, n + 1): 14 | selected[k] = cand 15 | rec_func(k + 1) 16 | selected[k] = 0 17 | 18 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15651-N과 M(3)/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, M, selected[10]; 6 | void input() { 7 | cin >> N >> M; 8 | } 9 | 10 | void rec_func(int k) { 11 | if (k == M + 1) { 12 | for (int i = 1; i <= M; i++) 13 | cout << selected[i] << " "; 14 | cout << "\n"; 15 | return; 16 | } 17 | for (int cand = 1; cand <= N; cand++) { 18 | selected[k] = cand; 19 | rec_func(k + 1); 20 | selected[k] = 0; 21 | } 22 | } 23 | int main() { 24 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 25 | input(); 26 | rec_func(1); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15651-N과 M(3)/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().split(' ')) 3 | 4 | selected = [0 for _ in range(m)] 5 | used = [0 for _ in range(n + 1)] 6 | def rec_func(k): 7 | if k == m: 8 | for x in selected: 9 | sys.stdout.write(str(x) + ' ') 10 | sys.stdout.write('\n') 11 | else: 12 | for cand in range(1, n + 1): 13 | selected[k] = cand 14 | rec_func(k + 1) 15 | selected[k] = 0 16 | 17 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15652-N과 M(4)/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, M, selected[10]; 6 | void input() { 7 | cin >> N >> M; 8 | } 9 | 10 | void rec_func(int k) { 11 | if (k == M + 1) { 12 | for (int i = 1; i <= M; i++) 13 | cout << selected[i] << " "; 14 | cout << "\n"; 15 | return; 16 | } 17 | int start = selected[k-1]; 18 | if (start == 0) start = 1; 19 | for (int cand = start; cand <= N; cand++) { 20 | selected[k] = cand; 21 | rec_func(k + 1); 22 | selected[k] = 0; 23 | } 24 | } 25 | int main() { 26 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 27 | input(); 28 | rec_func(1); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15652-N과 M(4)/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().split(' ')) 3 | 4 | selected = [0 for _ in range(m)] 5 | used = [0 for _ in range(n + 1)] 6 | def rec_func(k): 7 | if k == m: 8 | for x in selected: 9 | sys.stdout.write(str(x) + ' ') 10 | sys.stdout.write('\n') 11 | else: 12 | start = 1 if k == 0 else selected[k - 1] 13 | for cand in range(start, n + 1): 14 | selected[k] = cand 15 | rec_func(k + 1) 16 | selected[k] = 0 17 | 18 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15663-N과 M(9)/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | 6 | int N, M, selected[10], nums[10], used[10]; 7 | void input() { 8 | cin >> N >> M; 9 | for (int i = 1; i <= N; i++) cin >> nums[i]; 10 | sort(nums + 1, nums + 1 + N); 11 | } 12 | 13 | void rec_func(int k) { 14 | if (k == M + 1) { 15 | for (int i = 1; i <= M; i++) 16 | cout << selected[i] << " "; 17 | cout << "\n"; 18 | return; 19 | } 20 | 21 | int last_cand = 0; 22 | for (int cand = 1; cand <= N; cand++) { 23 | if (used[cand] == 1) continue; 24 | if (nums[cand] == last_cand) continue; 25 | 26 | last_cand = nums[cand]; 27 | selected[k] = nums[cand], used[cand] = 1; 28 | rec_func(k + 1); 29 | selected[k] = 0, used[cand] = 0; 30 | } 31 | } 32 | int main() { 33 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 34 | input(); 35 | rec_func(1); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/15663-N과 M(9)/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().split(' ')) 3 | nums = sorted(list(map(int, sys.stdin.readline().split(' ')))) 4 | 5 | selected = [0 for _ in range(m)] 6 | used = [0 for _ in range(n + 1)] 7 | def rec_func(k): 8 | if k == m: 9 | for x in selected: 10 | sys.stdout.write(str(x) + ' ') 11 | sys.stdout.write('\n') 12 | else: 13 | last_cand = 0 14 | for cand in range(n): 15 | if used[cand] == 1 or nums[cand] == last_cand: 16 | continue 17 | last_cand = nums[cand] 18 | 19 | selected[k] = nums[cand] 20 | used[cand] = 1 21 | 22 | rec_func(k + 1) 23 | 24 | selected[k] = 0 25 | used[cand] = 0 26 | 27 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1759-암호 만들기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | 6 | int N, M, selected[16]; 7 | char chars[16]; 8 | void input() { 9 | cin >> M >> N; 10 | for (int i = 1; i <= N; i++) cin >> chars[i]; 11 | } 12 | 13 | bool isVowel(char x) { 14 | return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'; 15 | } 16 | 17 | void rec_func(int k) { 18 | if (k == M + 1) { 19 | int vowel = 0, consonant = 0; 20 | for (int i = 1; i <= M; i++) { 21 | if (isVowel(chars[selected[i]])) vowel++; 22 | else consonant++; 23 | } 24 | if (vowel >= 1 && consonant >= 2) { 25 | for (int i = 1; i <= M; i++) cout << chars[selected[i]]; 26 | cout << "\n"; 27 | } 28 | return; 29 | } 30 | for (int cand = selected[k - 1] + 1; cand <= N; cand++) { 31 | selected[k] = cand; 32 | rec_func(k + 1); 33 | selected[k] = 0; 34 | } 35 | } 36 | int main() { 37 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 38 | input(); 39 | sort(chars + 1, chars + 1 + N); 40 | rec_func(1); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/1759-암호 만들기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | input = sys.stdin.readline 3 | m, n = map(int, input().split(' ')) 4 | chars = sorted(input().strip().split(' ')) 5 | used = [0] * n 6 | selected = [0] * m 7 | 8 | def is_vowel(x: str): 9 | return x in "aeiou" 10 | 11 | def rec_func(k): 12 | if k == m: 13 | vowel, consonant = 0, 0 14 | for x in selected: 15 | if is_vowel(chars[x]): 16 | vowel += 1 17 | else: 18 | consonant += 1 19 | 20 | if (vowel >= 1) and (consonant >= 2): 21 | for x in selected: 22 | print(chars[x], end='') 23 | print() 24 | 25 | else: 26 | st = -1 if k == 0 else selected[k - 1] 27 | for i in range(st + 1, n): 28 | selected[k] = i 29 | rec_func(k + 1) 30 | selected[k] = 0 31 | 32 | rec_func(0) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/9663-N Queen/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | 5 | int N, col[15], ans; 6 | void input() { 7 | cin >> N; 8 | } 9 | 10 | bool attackable(int r1, int c1, int r2, int c2) { 11 | if (c1 == c2) return true; 12 | if (r1 - c1 == r2 - c2) return true; 13 | if (r1 + c1 == r2 + c2) return true; 14 | return false; 15 | } 16 | 17 | void rec_func(int row) { 18 | if (row == N + 1) { 19 | ans++; 20 | } 21 | else { 22 | for (int c = 1; c <= N; c++) { 23 | bool possible = true; 24 | // valid check (row, c) 25 | for (int i = 1; i <= row - 1; i++) { 26 | // (i, col[i]) 27 | if (attackable(row, c, i, col[i])) { 28 | possible = false; 29 | break; 30 | } 31 | } 32 | if (possible) { 33 | col[row] = c; 34 | rec_func(row + 1); 35 | col[row] = 0; 36 | } 37 | } 38 | } 39 | } 40 | int main() { 41 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 42 | input(); 43 | rec_func(1); 44 | cout << ans; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/01~02-완전 탐색/문제별 코드/9663-N Queen/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n = int(sys.stdin.readline()) 3 | col = [0 for _ in range(n)] 4 | ans = 0 5 | 6 | def attackable(r1, c1, r2, c2): 7 | if c1 == c2: 8 | return True 9 | if r1 - c1 == r2 - c2: 10 | return True 11 | if r1 + c1 == r2 + c2: 12 | return True 13 | return False 14 | 15 | def rec_func(row): 16 | if row == n: 17 | global ans 18 | ans += 1 19 | else: 20 | for cand in range(n): 21 | possible = True 22 | for i in range(row): 23 | if attackable(row, cand, i, col[i]): 24 | possible = False 25 | break 26 | 27 | if possible: 28 | col[row] = cand 29 | rec_func(row + 1) 30 | col[row] = 0 31 | 32 | rec_func(0) 33 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/02-알고리즘-03-정렬.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/03~04-정렬/02-알고리즘-03-정렬.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/02-알고리즘-04-정렬-응용편.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/03~04-정렬/02-알고리즘-04-정렬-응용편.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/README.md: -------------------------------------------------------------------------------- 1 | |문제 이름|문제 링크|답안 코드 링크| 2 | |---|---|---| 3 | |국영수|[링크](http://boj.kr/10825)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/10825-국영수)| 4 | |수열 정렬|[링크](http://boj.kr/1015)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/1015-수열%20정렬)| 5 | |카드|[링크](http://boj.kr/11652)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/11652-카드)| 6 | |화살표 그리기|[링크](http://boj.kr/15970)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/15970-화살표%20그리기)| 7 | |**연습문제**| 8 | |단어 정렬|[링크](http://boj.kr/1181)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/1181-단어%20정렬)| 9 | |파일 정리|[링크](http://boj.kr/20291)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/03~04-정렬/문제별%20코드/20291-파일%20정리)| -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/1015-수열 정렬/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 55 8 | 9 | int N; 10 | 11 | struct Elem { 12 | int num, idx; 13 | bool operator<(const Elem& rhs) const { 14 | if (num != rhs.num) return num < rhs.num; 15 | return idx < rhs.idx; 16 | } 17 | }B[NM]; 18 | int P[NM]; 19 | 20 | void input() { 21 | cin >> N; 22 | for (int i = 0; i < N; i++) { 23 | int x; cin >> x; 24 | B[i] = { x, i }; 25 | } 26 | } 27 | 28 | void pro() { 29 | sort(B, B + N); 30 | for (int i = 0; i < N; i++) { 31 | P[B[i].idx] = i; 32 | } 33 | for (int i = 0; i < N; i++) { 34 | cout << P[i] << " "; 35 | } 36 | } 37 | 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/1015-수열 정렬/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(sys.stdin.readline()) 4 | a = list(map(int, sys.stdin.readline().split())) 5 | B = [(x, i) for i, x in enumerate(a)] 6 | B.sort() 7 | P = [0 for _ in range(n)] 8 | for i in range(n): 9 | P[B[i][1]] = i 10 | for i in range(n): 11 | print(P[i], ' ') -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/10825-국영수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N; 10 | 11 | struct Elem { 12 | string name; 13 | int korean, english, math; 14 | bool operator<(const Elem& rhs) const { 15 | if (korean != rhs.korean) return rhs.korean < korean; 16 | if (english != rhs.english) return english < rhs.english; 17 | if (math != rhs.math) return rhs.math < math; 18 | return name < rhs.name; 19 | } 20 | }a[NM]; 21 | 22 | 23 | void input() { 24 | cin >> N; 25 | for (int i = 1; i <= N; i++) { 26 | cin >> a[i].name >> a[i].korean >> a[i].english >> a[i].math; 27 | } 28 | } 29 | 30 | void pro() { 31 | sort(a + 1, a + 1 + N); 32 | for (int i = 1; i <= N; i++) { 33 | cout << a[i].name << '\n'; 34 | } 35 | } 36 | 37 | 38 | int main() { 39 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 40 | input(); 41 | pro(); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/10825-국영수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | class Elem: 4 | def __init__(self, name, korean, english, math): 5 | self.name = name 6 | self.korean = korean 7 | self.english = english 8 | self.math = math 9 | 10 | n = int(sys.stdin.readline()) 11 | a = [] 12 | 13 | for i in range(n): 14 | inp = sys.stdin.readline().split() 15 | inp[1:] = map(int, inp[1:]) 16 | a.append(Elem(*inp)) 17 | 18 | a.sort(key = lambda x: (-x.korean, x.english, -x.math, x.name)) 19 | 20 | names = [elem.name for elem in a] 21 | for name in names: 22 | print(name) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/11652-카드/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | 6 | #define NM 100005 7 | 8 | int N; 9 | ll a[NM]; 10 | 11 | void input() { 12 | cin >> N; 13 | for (int i = 1; i <= N; i++) { 14 | cin >> a[i]; 15 | } 16 | } 17 | 18 | void pro() { 19 | sort(a+1, a+1+N); 20 | ll mode = a[1]; 21 | int modeCnt = 1, curCnt = 1; 22 | for (int i = 2; i <= N; i++) { 23 | if (a[i] == a[i - 1]) curCnt++; 24 | else curCnt = 1; 25 | if (modeCnt < curCnt) { 26 | modeCnt = curCnt; 27 | mode = a[i]; 28 | } 29 | } 30 | cout << mode; 31 | } 32 | 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 36 | input(); 37 | pro(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/11652-카드/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n = int(sys.stdin.readline()) 3 | a = [] 4 | 5 | for i in range(n): 6 | a.append(int(sys.stdin.readline())) 7 | 8 | a.sort() 9 | 10 | mode = a[0] 11 | modeCnt = 1 12 | curCnt = 1 13 | 14 | for i in range(1, n): 15 | if a[i] == a[i-1]: 16 | curCnt += 1 17 | else: 18 | curCnt = 1 19 | if modeCnt < curCnt: 20 | modeCnt = curCnt; 21 | mode = a[i] 22 | 23 | print(mode) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/1181-단어 정렬/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 20005 8 | 9 | int N; 10 | string a[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) { 15 | cin >> a[i]; 16 | } 17 | } 18 | 19 | void pro() { 20 | sort(a + 1, a + 1 + N, [](const string& A, const string& B) { 21 | if (A.length() != B.length()) return A.length() < B.length(); 22 | return A < B; 23 | }); 24 | for (int i = 1; i <= N; i++) { 25 | if (a[i] == a[i - 1]) continue; 26 | cout << a[i] << '\n'; 27 | } 28 | } 29 | 30 | 31 | int main() { 32 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 33 | input(); 34 | pro(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/1181-단어 정렬/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(sys.stdin.readline()) 4 | a = [] 5 | 6 | for i in range(n): 7 | a.append(sys.stdin.readline().strip()) 8 | 9 | a.sort(key = lambda x: (len(x), x)) 10 | 11 | for i in range(n): 12 | if i == 0 or a[i] != a[i-1]: 13 | print(a[i]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/15970-화살표 그리기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 5005 8 | 9 | int N; 10 | vector a[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) { 15 | int coord, color; 16 | cin >> coord >> color; 17 | a[color].push_back(coord); 18 | } 19 | } 20 | 21 | int toLeft(int color, int x) { 22 | if (x == 0) return 0x7fffffff; 23 | return a[color][x] - a[color][x - 1]; 24 | } 25 | 26 | int toRight(int color, int x) { 27 | if (x + 1 == a[color].size()) return 0x7fffffff; 28 | return a[color][x + 1] - a[color][x]; 29 | } 30 | 31 | void pro() { 32 | int ans = 0; 33 | for (int color = 1; color <= N; color++) { 34 | sort(a[color].begin(), a[color].end()); 35 | for (int i = 0; i < a[color].size(); i++) { 36 | int left = toLeft(color, i); 37 | int right = toRight(color, i); 38 | ans += min(left, right); 39 | } 40 | } 41 | cout << ans; 42 | } 43 | 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 47 | input(); 48 | pro(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/15970-화살표 그리기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n = int(sys.stdin.readline()) 3 | 4 | a = [[] for _ in range(n + 1)] 5 | 6 | for i in range(n): 7 | coord, color = map(int, sys.stdin.readline().split()) 8 | a[color].append(coord) 9 | 10 | def toLeft(color, i): 11 | if i == 0: 12 | return 1000000 13 | return a[color][i] - a[color][i - 1] 14 | 15 | def toRight(color, i): 16 | if i + 1 == len(a[color]): 17 | return 1000000 18 | return a[color][i + 1] - a[color][i] 19 | 20 | 21 | ans = 0 22 | for color in range(1, n + 1): 23 | a[color].sort() 24 | for i in range(len(a[color])): 25 | ans += min(toLeft(color, i), toRight(color, i)) 26 | 27 | print(ans) 28 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/20291-파일 정리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | int N; 8 | string a[50005]; 9 | void input() { 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) { 12 | cin >> a[i]; 13 | a[i] = a[i].substr(a[i].find(".") + 1); 14 | } 15 | } 16 | 17 | void pro() { 18 | sort(a + 1, a + 1 + N); 19 | for (int i = 1; i <= N;) { 20 | int j = i + 1, cnt = 1; 21 | for (; j <= N; j++) { 22 | if (a[i] == a[j]) cnt++; 23 | else break; 24 | } 25 | cout << a[i] << " " << cnt << "\n"; 26 | i = j; 27 | } 28 | } 29 | 30 | 31 | int main() { 32 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 33 | input(); 34 | pro(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/문제별 코드/20291-파일 정리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n = int(sys.stdin.readline()) 3 | 4 | ext = [] 5 | for _ in range(n): 6 | ext.append(sys.stdin.readline().strip().split('.')[1]) 7 | 8 | ext.sort() 9 | 10 | i = 0 11 | while i < n: 12 | cnt = 1 13 | for j in range(i + 1, n): 14 | if ext[j] == ext[i]: 15 | cnt += 1 16 | i += 1 17 | else: 18 | break 19 | print(ext[i], cnt) 20 | i += 1 -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/03~04-정렬/화살표 그리기 별첨.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/03~04-정렬/화살표 그리기 별첨.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/02-알고리즘-05-이분 탐색.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/05~06-이분 탐색/02-알고리즘-05-이분 탐색.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/02-알고리즘-06-매개 변수 탐색.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/05~06-이분 탐색/02-알고리즘-06-매개 변수 탐색.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/10816-숫자 카드 2/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | int lower_bound(int A[], int L, int R, int X) { 18 | int ans = R + 1; 19 | while (L <= R) { 20 | int mid = (L + R) / 2; 21 | if (A[mid] >= X) { 22 | ans = mid; 23 | R = mid - 1; 24 | } 25 | else { 26 | L = mid + 1; 27 | } 28 | } 29 | return ans; 30 | } 31 | 32 | int upper_bound(int A[], int L, int R, int X) { 33 | int ans = R + 1; 34 | while (L <= R) { 35 | int mid = (L + R) / 2; 36 | if (A[mid] > X) { 37 | ans = mid; 38 | R = mid - 1; 39 | } 40 | else { 41 | L = mid + 1; 42 | } 43 | } 44 | return ans; 45 | } 46 | 47 | #include 48 | void pro() { 49 | sort(A + 1, A + 1 + N); 50 | 51 | int M; 52 | cin >> M; 53 | for (int i = 1; i <= M; i++) { 54 | int X; 55 | cin >> X; 56 | cout << upper_bound(A, 1, N, X) - lower_bound(A, 1, N, X) << ' '; 57 | } 58 | } 59 | 60 | 61 | int main() { 62 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 63 | input(); 64 | pro(); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/10816-숫자 카드 2/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = sorted(list(map(int, si().split()))) 5 | 6 | def lower_bound(a, l, r, x): 7 | res = r + 1 8 | while l <= r: 9 | mid = (l + r) // 2 10 | if a[mid] >= x: 11 | res = mid 12 | r = mid - 1 13 | else: l = mid + 1 14 | return res 15 | 16 | def upper_bound(a, l, r, x): 17 | res = r + 1 18 | while l <= r: 19 | mid = (l + r) // 2 20 | if a[mid] > x: 21 | res = mid 22 | r = mid - 1 23 | else: l = mid + 1 24 | return res 25 | 26 | m = int(si()) 27 | b = list(map(int, si().split())) 28 | for x in b: 29 | print(upper_bound(a, 0, n - 1, x) - lower_bound(a, 0, n - 1, x), end=' ') -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1300-K 번째 수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | int N, K; 8 | 9 | void input() { 10 | cin >> N >> K; 11 | } 12 | 13 | bool determination(ll candidate){ 14 | ll sum = 0; 15 | for (int i = 1; i <= N; i++){ 16 | sum += min((ll)N, candidate / i); 17 | } 18 | return sum >= K; 19 | } 20 | 21 | void pro() { 22 | ll L = 1, R = (ll)N * N, ans = 0; 23 | while (L <= R){ 24 | ll mid = (L + R) / 2; 25 | if (determination(mid)){ 26 | ans = mid; 27 | R = mid - 1; 28 | }else L = mid + 1; 29 | } 30 | cout << ans; 31 | } 32 | 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 36 | input(); 37 | pro(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1300-K 번째 수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(sys.stdin.readline()) 4 | k = int(sys.stdin.readline()) 5 | 6 | def determination(candidate): 7 | sum = 0 8 | for i in range(1, n + 1): 9 | sum += min(n, candidate // i) 10 | return sum >= k 11 | 12 | l, r, ans = 1, n * n, 0 13 | while l <= r: 14 | mid = (l + r) // 2 15 | if determination(mid): 16 | ans = mid 17 | r = mid - 1 18 | else: 19 | l = mid + 1 20 | 21 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/13702-이상한 술집/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 10005 8 | 9 | int N, K; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> K; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(ll amount) { 18 | if (amount == 0) { 19 | return false; 20 | } 21 | ll sum = 0; 22 | for (int i = 1; i <= N; i++) { 23 | sum += A[i] / amount; 24 | } 25 | return sum >= K; 26 | } 27 | 28 | void pro() { 29 | ll L = 0, R = 1ll << 31, ans = 0; 30 | while (L <= R) { 31 | int mid = (L + R) / 2; 32 | if (determination(mid)) { 33 | ans = mid; 34 | L = mid + 1; 35 | } 36 | else R = mid - 1; 37 | } 38 | cout << ans; 39 | } 40 | 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 44 | input(); 45 | pro(); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/13702-이상한 술집/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, k = list(map(int, si().split())) 4 | a = [int(si()) for _ in range(n)] 5 | 6 | def determination(amount): 7 | if amount == 0: 8 | return False 9 | sum = 0 10 | for x in a: 11 | sum += x // amount 12 | return sum >= k 13 | 14 | l, r, ans = 0, 1 << 31, 0 15 | while l <= r: 16 | mid = (l + r) // 2 17 | if determination(mid): 18 | ans = mid 19 | l = mid + 1 20 | else: 21 | r = mid - 1 22 | 23 | print(ans) 24 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1637-날카로운 눈/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 20005 8 | 9 | int N, K; 10 | int info[NM][3]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) for (int j = 0; j < 3; j++) cin >> info[i][j]; 15 | } 16 | 17 | int counting(int A, int C, int B, int X) { 18 | if (X < A) return 0; 19 | if (C < X) return (C - A) / B + 1; 20 | return (X - A) / B + 1; 21 | } 22 | 23 | bool determination(ll candidate) { 24 | ll sum = 0; 25 | for (int i = 1; i <= N; i++) { 26 | sum += counting(info[i][0], info[i][1], info[i][2], candidate); 27 | } 28 | return sum % 2 == 1; 29 | } 30 | 31 | void pro() { 32 | ll L = 1, R = 1ll << 31, ans = 0, ansCnt = 0; 33 | while (L <= R) { 34 | ll mid = (L + R) / 2; 35 | if (determination(mid)) { 36 | ans = mid; 37 | R = mid - 1; 38 | } 39 | else L = mid + 1; 40 | } 41 | if (ans == 0) { 42 | cout << "NOTHING"; 43 | } 44 | else { 45 | for (int i = 1; i <= N; i++) { 46 | if (info[i][0] <= ans && ans <= info[i][1] && (ans - info[i][0]) % info[i][2] == 0) { 47 | ansCnt++; 48 | } 49 | } 50 | cout << ans << " " << ansCnt; 51 | } 52 | } 53 | 54 | 55 | int main() { 56 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 57 | input(); 58 | pro(); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1637-날카로운 눈/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | info = [] 5 | for i in range(n): 6 | info.append(list(map(int, si().split()))) 7 | 8 | def count(A, C, B, X): 9 | if X < A: return 0 10 | if C < X: return (C - A) // B + 1 11 | return (X - A) // B + 1 12 | 13 | def determination(candidate): 14 | sum = 0 15 | for i in info: 16 | sum += count(i[0], i[1], i[2], candidate) 17 | return sum % 2 == 1 18 | 19 | l, r, ans, ansCnt = 1, 1 << 31, 0, 0 20 | while l <= r: 21 | mid = (l + r) // 2 22 | if determination(mid): 23 | ans = mid 24 | r = mid - 1 25 | else: 26 | l = mid + 1 27 | 28 | if ans == 0: 29 | print("NOTHING") 30 | else: 31 | for i in info: 32 | if i[0] <= ans and ans <= i[1] and (ans - i[0]) % i[2] == 0: 33 | ansCnt += 1 34 | print(ans, ansCnt) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1654-랜선 자르기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 10005 8 | 9 | int N, K; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> K >> N; 14 | for (int i = 1; i <= K; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(ll len) { 18 | ll sum = 0; 19 | for (int i = 1; i <= K; i++) { 20 | sum += A[i] / len; 21 | } 22 | return sum >= N; 23 | } 24 | 25 | void pro() { 26 | ll L = 1, R = 1ll << 31, ans = 0, ansCnt = 0; 27 | while (L <= R) { 28 | ll mid = (L + R) / 2; 29 | if (determination(mid)) { 30 | ans = mid; 31 | L = mid + 1; 32 | } 33 | else R = mid - 1; 34 | } 35 | cout << ans; 36 | } 37 | 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1654-랜선 자르기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | k, n = list(map(int, si().split())) 4 | a = [] 5 | for i in range(k): 6 | a.append(int(si())) 7 | 8 | def determination(len): 9 | sum = 0 10 | for x in a: 11 | sum += x // len 12 | return sum >= n 13 | 14 | l, r, ans = 1, 1 << 31, 0 15 | while l <= r: 16 | mid = (l + r) // 2 17 | if determination(mid): 18 | ans = mid 19 | l = mid + 1 20 | else: 21 | r = mid - 1 22 | 23 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/17266-어두운 굴다리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= M; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(int height) { 18 | int last = 0; // 밝혀진 마지막 위치 19 | for (int i = 1; i <= M; i++) { 20 | if (A[i] - last <= height) { 21 | last = A[i] + height; 22 | } else { 23 | return false; 24 | } 25 | } 26 | return last >= N; 27 | } 28 | 29 | void pro() { 30 | int L = 0, R = N, ans = N; 31 | sort(A + 1, A + M + 1); 32 | while (L <= R) { 33 | int mid = (L + R) / 2; 34 | if (determination(mid)) { 35 | ans = mid; 36 | R = mid - 1; 37 | } 38 | else L = mid + 1; 39 | } 40 | cout << ans; 41 | } 42 | 43 | 44 | int main() { 45 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 46 | input(); 47 | pro(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/17266-어두운 굴다리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | m = int (si()) 5 | a = list(map(int, si().split())) 6 | 7 | def determination(height): 8 | last = 0 9 | for x in a: 10 | if x - last <= height: 11 | last = x + height 12 | else: 13 | return False 14 | return last >= n 15 | 16 | l, r, ans = 0, n, n 17 | a.sort() 18 | while l <= r: 19 | mid = (l + r) // 2 20 | if determination(mid): 21 | ans = mid 22 | r = mid - 1 23 | else: 24 | l = mid + 1 25 | 26 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1764-듣보잡/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N, M; 10 | string A[NM], B[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | for (int i = 1; i <= M; i++) cin >> B[i]; 16 | } 17 | 18 | int bin_search(string A[], int L, int R, string X) { 19 | while (L <= R) { 20 | int mid = (L + R) / 2; 21 | if (A[mid] == X) 22 | return true; 23 | 24 | if (A[mid] < X) 25 | L = mid + 1; 26 | else 27 | R = mid - 1; 28 | } 29 | return false; 30 | } 31 | 32 | #include 33 | void pro() { 34 | sort(A + 1, A + 1 + N); 35 | sort(B + 1, B + 1 + M); 36 | vector ans; 37 | for (int i = 1; i <= M; i++) { 38 | if (bin_search(A, 1, N, B[i])) ans.push_back(B[i]); 39 | } 40 | cout << ans.size() << '\n'; 41 | for (string x : ans) cout << x << '\n'; 42 | } 43 | 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 47 | input(); 48 | pro(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1764-듣보잡/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, m = list(map(int, si().split())) 4 | a = sorted([si() for _ in range(n)]) 5 | b = sorted([si() for _ in range(m)]) 6 | 7 | def bin_search(a, l, r, x): 8 | while l <= r: 9 | mid = (l + r) // 2 10 | if a[mid] == x: 11 | return True 12 | if a[mid] < x: l = mid + 1 13 | else: r = mid - 1 14 | return False 15 | 16 | ans = [x for x in b if bin_search(a, 0, n-1, x)] 17 | print(len(ans)) 18 | for x in ans: 19 | print(x, end="") -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1920-수 찾기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | int bin_search(int A[],int L, int R, int X){ 18 | while (L <= R){ 19 | int mid = (L+R)/2; 20 | if (A[mid] == X) 21 | return true; 22 | 23 | if (A[mid] < X) 24 | L = mid + 1; 25 | else 26 | R = mid - 1; 27 | } 28 | return false; 29 | } 30 | 31 | void pro() { 32 | sort(A+1, A+1+N); 33 | int M; 34 | cin >> M; 35 | for (int i=1;i<=M;i++){ 36 | int X; 37 | cin >> X; 38 | if (bin_search(A, 1, N, X)) cout << 1 << "\n"; 39 | else cout << 0 << "\n"; 40 | } 41 | } 42 | 43 | 44 | int main() { 45 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 46 | input(); 47 | pro(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/1920-수 찾기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | 6 | def bin_search(a, l, r, x): 7 | while l <= r: 8 | mid = (l + r) // 2 9 | if a[mid] == x: 10 | return True 11 | if a[mid] < x: l = mid + 1 12 | else: r = mid - 1 13 | return False 14 | 15 | a.sort() 16 | m = int(si()) 17 | for x in map(int, si().split()): 18 | if bin_search(a, 0, n-1, x): print(1) 19 | else: print(0) 20 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2110-공유기 설치/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 200005 8 | 9 | int N, C; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> C; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(int D) { 18 | int cnt = 1, last = A[1]; 19 | for (int i=2;i<=N;i++){ 20 | if (A[i] - last < D) continue; 21 | last = A[i]; 22 | cnt++; 23 | } 24 | return cnt >= C; 25 | } 26 | 27 | void pro() { 28 | sort(A+1,A+1+N); 29 | int L = 1, R = 1000000000, ans = 0; 30 | while (L <= R) { 31 | ll mid = (L + R) / 2; 32 | if (determination(mid)) { 33 | ans = mid; 34 | L = mid + 1; 35 | } 36 | else R = mid - 1; 37 | } 38 | cout << ans; 39 | } 40 | 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 44 | input(); 45 | pro(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2110-공유기 설치/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, c = list(map(int, si().split())) 4 | a = [] 5 | for i in range(n): 6 | a.append(int(si())) 7 | 8 | def determination(D): 9 | cnt, last = 1, a[0] 10 | for i in range(1, n): 11 | if a[i] - last < D: continue 12 | last = a[i] 13 | cnt += 1 14 | return cnt >= c 15 | 16 | a.sort() 17 | l, r, ans = 1, 1000000000, 0 18 | while l <= r: 19 | mid = (l + r) // 2 20 | if determination(mid): 21 | ans = mid 22 | l = mid + 1 23 | else: 24 | r = mid - 1 25 | 26 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2343-기타 레슨/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(int len) { 18 | int cnt = 1, sum = 0; 19 | for (int i = 1; i <= N; i++) { 20 | if (sum + A[i] > len) { 21 | cnt++; 22 | sum = A[i]; 23 | } else { 24 | sum += A[i]; 25 | } 26 | } 27 | return cnt <= M; 28 | } 29 | 30 | void pro() { 31 | int L = A[1], R = 1000000000, ans = 0; 32 | for (int i=1;i<=N;i++) L = max(L, A[i]); 33 | while (L <= R) { 34 | ll mid = (L + R) / 2; 35 | if (determination(mid)) { 36 | ans = mid; 37 | R = mid - 1; 38 | } 39 | else L = mid + 1; 40 | } 41 | cout << ans; 42 | } 43 | 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 47 | input(); 48 | pro(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2343-기타 레슨/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, m = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | 6 | def determination(len): 7 | cnt, sum = 1, 0 8 | for x in a: 9 | if sum + x > len: 10 | cnt += 1 11 | sum = x 12 | else: 13 | sum += x 14 | return cnt <= m 15 | 16 | 17 | l, r, ans = max(a), 1000000000, 0 18 | while l <= r: 19 | mid = (l + r) // 2 20 | if determination(mid): 21 | ans = mid 22 | r = mid - 1 23 | else: 24 | l = mid + 1 25 | 26 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2470-두 용액/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = sorted(list(map(int, si().split()))) 5 | 6 | def lower_bound(a, l, r, x): 7 | res = r + 1 8 | while l <= r: 9 | mid = (l + r) // 2 10 | if a[mid] >= x: 11 | res = mid 12 | r = mid - 1 13 | else: l = mid + 1 14 | return res 15 | 16 | best_sum = 1 << 31 17 | v1, v2 = 0, 0 18 | for l in range(n - 1): 19 | candidate = lower_bound(a, l + 1, n - 1, -a[l]) 20 | if l < candidate - 1 and abs(a[l] + a[candidate - 1]) < best_sum: 21 | best_sum = abs(a[l] + a[candidate - 1]) 22 | v1, v2 = a[l], a[candidate - 1] 23 | 24 | if candidate < n and abs(a[l] + a[candidate]) < best_sum: 25 | best_sum = abs(a[l] + a[candidate]) 26 | v1, v2 = a[l], a[candidate] 27 | 28 | print(v1, v2) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2512-예산/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 10005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | cin >> M; 16 | } 17 | 18 | bool determination(int limit) { 19 | int sum = 0; 20 | for (int i = 1; i <= N; i++) { 21 | sum += min(A[i], limit); 22 | } 23 | return sum <= M; 24 | } 25 | 26 | void pro() { 27 | int L = 0, R = 0, ans = 0; 28 | for (int i=1;i<=N;i++) R = max(R, A[i]); 29 | while (L <= R) { 30 | int mid = (L + R) / 2; 31 | if (determination(mid)) { 32 | ans = mid; 33 | L = mid + 1; 34 | } 35 | else R = mid - 1; 36 | } 37 | cout << ans; 38 | } 39 | 40 | 41 | int main() { 42 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 43 | input(); 44 | pro(); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2512-예산/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | m = int(si()) 6 | 7 | def determination(limit): 8 | sum = 0 9 | for x in a: 10 | sum += min(x, limit) 11 | return sum <= m 12 | 13 | l, r, ans = 0, max(a), 0 14 | while l <= r: 15 | mid = (l + r) // 2 16 | if determination(mid): 17 | ans = mid 18 | l = mid + 1 19 | else: 20 | r = mid - 1 21 | 22 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2805-나무 자르기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 1000005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(int H) { 18 | ll sum = 0; 19 | for (int i = 1; i <= N; i++) { 20 | if (A[i] > H) sum += A[i] - H; 21 | } 22 | return sum >= M; 23 | } 24 | 25 | void pro() { 26 | ll L = 0, R = 2000000000, ans = 0; 27 | while (L <= R) { 28 | ll mid = (L + R) / 2; 29 | if (determination(mid)) { 30 | ans = mid; 31 | L = mid + 1; 32 | } 33 | else R = mid - 1; 34 | } 35 | cout << ans; 36 | } 37 | 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/2805-나무 자르기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, m = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | 6 | def determination(H): 7 | sum = 0 8 | for x in a: 9 | if x > H: 10 | sum += x - H 11 | return sum >= m 12 | 13 | l, r, ans = 0, 2000000000, 0 14 | while l <= r: 15 | mid = (l + r) // 2 16 | if determination(mid): 17 | ans = mid 18 | l = mid + 1 19 | else: 20 | r = mid - 1 21 | 22 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/3273-두 수의 합/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, Sum; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | cin >> Sum; 16 | } 17 | 18 | int bin_search(int A[], int L, int R, int X) { 19 | while (L <= R) { 20 | int mid = (L + R) / 2; 21 | if (A[mid] == X) 22 | return true; 23 | 24 | if (A[mid] < X) 25 | L = mid + 1; 26 | else 27 | R = mid - 1; 28 | } 29 | return false; 30 | } 31 | 32 | void pro() { 33 | sort(A + 1, A + 1 + N); 34 | int ans = 0; 35 | for (int i = 1; i <= N - 1; i++) { 36 | if (bin_search(A, i + 1, N, Sum - A[i])) ans++; 37 | } 38 | cout << ans; 39 | } 40 | 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 44 | input(); 45 | pro(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/3273-두 수의 합/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | sum = int(si()) 6 | 7 | def bin_search(a, l, r, x): 8 | while l <= r: 9 | mid = (l + r) // 2 10 | if a[mid] == x: 11 | return True 12 | if a[mid] < x: l = mid + 1 13 | else: r = mid - 1 14 | return False 15 | 16 | a.sort() 17 | ans = 0 18 | for i in range(n - 1): 19 | if bin_search(a, i + 1, n - 1, sum - a[i]): ans += 1 20 | 21 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/6236-용돈 관리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool determination(int withdrawl) { 18 | int cnt = 1, sum = 0; 19 | for (int i = 1; i <= N; i++) { 20 | if (sum + A[i] > withdrawl) { 21 | cnt++; 22 | sum = A[i]; 23 | } 24 | else { 25 | sum += A[i]; 26 | } 27 | } 28 | return cnt <= M; 29 | } 30 | 31 | void pro() { 32 | int L = A[1], R = 1000000000, ans = 0; 33 | for (int i = 1; i <= N; i++) L = max(L, A[i]); 34 | while (L <= R) { 35 | int mid = (L + R) / 2; 36 | if (determination(mid)) { 37 | ans = mid; 38 | R = mid - 1; 39 | } 40 | else L = mid + 1; 41 | } 42 | cout << ans; 43 | } 44 | 45 | 46 | int main() { 47 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 48 | input(); 49 | pro(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/6236-용돈 관리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, m = list(map(int, si().split())) 4 | a = [int(si()) for _ in range(n)] 5 | 6 | def determination(withdrawl): 7 | cnt, sum = 1, 0 8 | for x in a: 9 | if sum + x > withdrawl: 10 | cnt += 1 11 | sum = x 12 | else: 13 | sum += x 14 | return cnt <= m 15 | 16 | l, r, ans = max(a), 1000000000, 0 17 | while l <= r: 18 | mid = (l + r) // 2 19 | if determination(mid): 20 | ans = mid 21 | r = mid - 1 22 | else: 23 | l = mid + 1 24 | 25 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/7795-먹을 것인가 먹힐 것인가/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 20005 8 | 9 | int N, M; 10 | int A[NM], B[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | for (int i = 1; i <= M; i++) cin >> B[i]; 16 | } 17 | 18 | int lower_bound(int A[], int L, int R, int X) { 19 | int res = L - 1; 20 | while (L <= R) { 21 | int mid = (L + R) / 2; 22 | if (A[mid] < X) { 23 | res = mid; 24 | L = mid + 1; 25 | } 26 | else { 27 | R = mid - 1; 28 | } 29 | } 30 | return res; 31 | } 32 | 33 | #include 34 | void pro() { 35 | sort(B + 1, B + 1 + M); 36 | 37 | int ans = 0; 38 | for (int i = 1; i <= N; i++) { 39 | // A[i] 를 선택했을 때, B 에서는 A[i]보다 작은 게 몇 개나 있는 지 count하기 40 | ans += lower_bound(B, 1, M, A[i]); 41 | } 42 | cout << ans << '\n'; 43 | } 44 | 45 | 46 | int main() { 47 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 48 | int TT; 49 | cin >> TT; 50 | while (TT--) { 51 | input(); 52 | pro(); 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/05~06-이분 탐색/문제별 코드/7795-먹을 것인가 먹힐 것인가/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | def lower_bound(a, l, r, x): 5 | res = l - 1 6 | while l <= r: 7 | mid = (l + r) // 2 8 | if a[mid] < x: 9 | res = mid 10 | l = mid + 1 11 | else: r = mid - 1 12 | return res 13 | 14 | def solve(): 15 | b.sort() 16 | ans = 0 17 | for x in a: 18 | ans += lower_bound(b, 0, m - 1, x) + 1 19 | print(ans) 20 | 21 | TT = int(si()) 22 | for _ in range(TT): 23 | n, m = list(map(int, si().split())) 24 | a = list(map(int, si().split())) 25 | b = list(map(int, si().split())) 26 | solve() -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/02-알고리즘-07-두 포인터.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/07~08-두 포인터/02-알고리즘-07-두 포인터.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/02-알고리즘-08-두 포인터-응용편.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/07~08-두 포인터/02-알고리즘-08-두 포인터-응용편.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/11728-배열 합치기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 1000005 8 | 9 | int N, M; 10 | int A[NM], B[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | for (int i = 1; i <= M; i++) cin >> B[i]; 16 | } 17 | 18 | void pro() { 19 | int L = 1, R = 1; 20 | while (L <= N && R <= M){ 21 | if (A[L] <= B[R]) cout << A[L++] << " "; 22 | else cout << B[R++] << " "; 23 | } 24 | while (L <= N) cout << A[L++] << " "; 25 | while (R <= M) cout << B[R++] << " "; 26 | } 27 | 28 | 29 | int main() { 30 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 31 | input(); 32 | pro(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/11728-배열 합치기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, m = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | b = list(map(int, si().split())) 6 | L, R = 0, 0 7 | while L < n and R < m: 8 | if a[L] <= b[R]: 9 | print(a[L], end=' ') 10 | L += 1 11 | else: 12 | print(b[R], end=' ') 13 | R += 1 14 | while L < n: 15 | print(a[L], end=' ') 16 | L += 1 17 | while R < m: 18 | print(b[R], end=' ') 19 | R += 1 20 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/1253-좋다/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 2005 8 | 9 | int N; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | bool func(int target_idx) { 18 | int L = 1, R = N; 19 | int target = A[target_idx]; 20 | while (L < R) { 21 | if (L == target_idx) L++; 22 | else if (R == target_idx) R--; 23 | else { 24 | if (A[L] + A[R] > target) R--; 25 | else if (A[L] + A[R] == target) return true; 26 | else L++; 27 | } 28 | } 29 | return false; 30 | } 31 | 32 | void pro() { 33 | sort(A + 1, A + 1 + N); 34 | int ans = 0; 35 | for (int i = 1; i <= N; i++) { 36 | if (func(i)) ans++; 37 | } 38 | cout << ans; 39 | } 40 | 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 44 | input(); 45 | pro(); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/1253-좋다/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | a.sort() 6 | 7 | def func(target_idx): 8 | L, R = 0, n - 1 9 | target = a[target_idx] 10 | while L < R: 11 | if L == target_idx: L += 1 12 | elif R == target_idx: R -= 1 13 | else: 14 | if a[L] + a[R] > target: R -= 1 15 | elif a[L] + a[R] < target: L += 1 16 | else: return True 17 | return False; 18 | 19 | print(len([x for x in range(n) if func(x)])) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/13144-List of Unique Numbers/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N; 10 | int A[NM], cnt[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | long ans = 0; 19 | for (int L = 1, R = 0; L <= N; L++) { // L 마다 R 을 최대한 옮겨 줄 계획이다. 20 | // R 을 옮길 수 있는 만큼 옮긴다. 21 | while (R + 1 <= N && cnt[A[R + 1]] == 0) { 22 | R++; 23 | cnt[A[R]]++; 24 | } 25 | 26 | // 정답을 갱신한다 27 | ans += R - L + 1; 28 | 29 | // L 을 옮겨주면서 A[L] 의 개수를 감소시킨다. 30 | cnt[A[L]]--; 31 | } 32 | 33 | cout << ans; 34 | } 35 | 36 | 37 | int main() { 38 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 39 | input(); 40 | pro(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/13144-List of Unique Numbers/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | cnt = [0] * 100005 6 | 7 | ans = 0 8 | R = -1 9 | for L in range(n): 10 | while R + 1 < n and cnt[a[R + 1]] == 0: 11 | R += 1 12 | cnt[a[R]] += 1 13 | ans += R - L + 1 14 | cnt[a[L]] -= 1 15 | 16 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/15565-귀여운 라이언/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 1000005 8 | 9 | int N, K, A[NM]; 10 | 11 | void input() { 12 | cin >> N >> K; 13 | for (int i = 1; i <= N; i++) cin >> A[i]; 14 | } 15 | 16 | void pro() { 17 | int right = 0, ans = -1, sum = 0; 18 | for (int left = 1; left <= N; left++) { 19 | while (right < N && sum < K) { 20 | right++; 21 | if (A[right] == 1) sum++; 22 | } 23 | 24 | if (sum == K) { 25 | if (ans == -1) ans = right - left + 1; 26 | ans = min(ans, right - left + 1); 27 | } 28 | 29 | if (A[left] == 1) sum--; 30 | } 31 | 32 | cout << ans; 33 | } 34 | 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/15565-귀여운 라이언/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, k = map(int, si().split()) 4 | a = si().split() 5 | 6 | R, ans, sum = -1, -1 ,0 7 | for L in range(n): 8 | while R + 1 < n and sum < k: 9 | R += 1 10 | if a[R] == '1': 11 | sum += 1 12 | if sum == k: 13 | if ans == -1: 14 | ans = R - L + 1 15 | ans = min(ans, R - L + 1) 16 | if a[L] == '1': 17 | sum -= 1 18 | 19 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/16472-고냥이/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, cnt[30], kind; 10 | string A; 11 | 12 | void input() { 13 | cin >> N; 14 | cin >> A; 15 | } 16 | void add(char x) { // x 라는 알파벳 추가 17 | cnt[x - 'a']++; 18 | if (cnt[x - 'a'] == 1) // 새롭게 나타난 알파벳이라는 뜻 19 | kind++; 20 | } 21 | 22 | void erase(char x) { // x 라는 알파벳 제거 23 | cnt[x - 'a']--; 24 | if (cnt[x - 'a'] == 0) // 인식해야 하는 알파벳에서 빠지는 순간 25 | kind--; 26 | } 27 | 28 | void pro() { 29 | int len = A.length(), ans = 0; 30 | for (int R = 0, L = 0; R < len; R++) { 31 | // R 번째 문자를 오른쪽에 추가 32 | add(A[R]); 33 | 34 | // 불가능하면, 가능할 때까지 L을 이동 35 | while (kind > N) { 36 | erase(A[L++]); 37 | } 38 | 39 | // 정답 갱신 40 | ans = max(ans, R - L + 1); 41 | } 42 | cout << ans; 43 | } 44 | 45 | 46 | int main() { 47 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 48 | input(); 49 | pro(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/16472-고냥이/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = si().strip() 5 | cnt = [0] * 30 6 | kind = 0 7 | 8 | def add(x): 9 | global kind 10 | t = ord(x) - ord('a') 11 | cnt[t] += 1 12 | if cnt[t] == 1: kind += 1 13 | 14 | def erase(x): 15 | global kind 16 | t = ord(x)-ord('a') 17 | cnt[t] -= 1 18 | if cnt[t] == 0: kind -= 1 19 | 20 | L = 0 21 | ans = 0 22 | for R in range(len(a)): 23 | add(a[R]) 24 | while kind > n: 25 | erase(a[L]) 26 | L += 1 27 | ans = max(ans, R - L + 1) 28 | 29 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/1806-부분 합/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N, S; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> S; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | int R = 0, sum = 0, ans = N + 1; 19 | for (int L = 1; L <= N; L++) { 20 | // L - 1 을 구간에서 제외하기 21 | sum -= A[L - 1]; 22 | 23 | // R 을 옮길 수 있을 때 까지 옮기기 24 | while (R + 1 <= N && sum < S) 25 | sum += A[++R]; 26 | 27 | // [L ... R] 의 합, 즉 sum이 조건을 만족하면 정답 갱신하기 28 | if (sum >= S) 29 | ans = min(ans, R - L + 1); 30 | } 31 | 32 | // ans 값을 보고 불가능 판단하기 33 | if (ans == N + 1) 34 | ans = 0; 35 | cout << ans; 36 | } 37 | 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/1806-부분 합/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, S = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | R, sum, ans = -1, 0, n + 1 6 | for L in range(n): 7 | while R + 1 < n and sum < S: 8 | R += 1 9 | sum += a[R] 10 | 11 | if sum >= S: 12 | ans = min(ans, R - L + 1) 13 | 14 | sum -= a[L] 15 | 16 | if ans == n + 1: ans = 0 17 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2003-수들의 합 2/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N, S; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> S; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | int R = 0, sum = 0, ans = 0; 19 | for (int L = 1; L <= N; L++) { 20 | // L - 1 을 구간에서 제외하기 21 | sum -= A[L - 1]; 22 | 23 | // R 을 옮길 수 있을 때 까지 옮기기 24 | while (R + 1 <= N && sum < S) 25 | sum += A[++R]; 26 | 27 | // [L ... R] 의 합, 즉 sum이 조건을 만족하면 정답 갱신하기 28 | if (sum == S) 29 | ans++; 30 | } 31 | 32 | cout << ans; 33 | } 34 | 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2003-수들의 합 2/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, S = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | R, sum, ans = -1, 0, 0 6 | for L in range(n): 7 | while R + 1 < n and sum < S: 8 | R += 1 9 | sum += a[R] 10 | 11 | if sum == S: 12 | ans += 1 13 | 14 | sum -= a[L] 15 | 16 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2230-수 고르기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, M; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | sort(A + 1, A + 1 + N); 19 | 20 | int R = 1, ans = 0x7fffffff; 21 | for (int L = 1; L <= N; L++) { 22 | while (R + 1 <= N && A[R] - A[L] < M) ++R; 23 | if (A[R] - A[L] >= M) ans = min(ans , A[R] - A[L]); 24 | } 25 | 26 | cout << ans; 27 | } 28 | 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 32 | input(); 33 | pro(); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2230-수 고르기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, M = list(map(int, si().split())) 4 | a = sorted([int(si()) for _ in range(n)]) 5 | R, ans = 0, 1 << 31 6 | for L in range(n): 7 | while R + 1 < n and a[R] - a[L] < M: 8 | R += 1 9 | 10 | if a[R] - a[L] >= M: 11 | ans = min(ans, a[R] - a[L]) 12 | 13 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2470-두 용액/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | sort(A + 1, A + 1 + N); 19 | 20 | int best_sum = 0x7fffffff; 21 | int v1 = 0, v2 = 0, L = 1, R = N; 22 | 23 | while (L < R){ // L == R 인 상황이면 용액이 한 개 뿐인 것이므로, L < R 일 때까지만 반복한다. 24 | if (best_sum > abs(A[L] + A[R])) { 25 | best_sum = abs(A[L] + A[R]); 26 | v1 = A[L]; 27 | v2 = A[R]; 28 | } 29 | if (A[L] + A[R] > 0) R--; 30 | else L++; 31 | } 32 | cout << v1 << " " << v2; 33 | } 34 | 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2470-두 용액/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = sorted(list(map(int, si().split()))) 5 | 6 | L, R = 0, n - 1 7 | v1, v2, best_sum = 0, 0, 1 << 31 8 | while L < R: 9 | if best_sum > abs(a[L] + a[R]): 10 | best_sum = abs(a[L] + a[R]) 11 | v1, v2 = a[L], a[R] 12 | if a[L] + a[R] > 0: R -= 1 13 | else: L += 1 14 | 15 | print(v1, v2) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2473-세 용액/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | sort(A + 1, A + 1 + N); 19 | 20 | int best_sum = 0x7fffffff; 21 | int v1 = 0, v2 = 0, L = 1, R = N; 22 | 23 | while (L < R){ // L == R 인 상황이면 용액이 한 개 뿐인 것이므로, L < R 일 때까지만 반복한다. 24 | if (best_sum > abs(A[L] + A[R])) { 25 | best_sum = abs(A[L] + A[R]); 26 | v1 = A[L]; 27 | v2 = A[R]; 28 | } 29 | if (A[L] + A[R] > 0) R--; 30 | else L++; 31 | } 32 | cout << v1 << " " << v2; 33 | } 34 | 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2473-세 용액/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = sorted(list(map(int, si().split()))) 5 | 6 | L, R = 0, n - 1 7 | v1, v2, best_sum = 0, 0, 1 << 31 8 | while L < R: 9 | if best_sum > abs(a[L] + a[R]): 10 | best_sum = abs(a[L] + a[R]) 11 | v1, v2 = a[L], a[R] 12 | if a[L] + a[R] > 0: R -= 1 13 | else: L += 1 14 | 15 | print(v1, v2) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2559-수열/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N, K; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N >> K; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | int R = 0, sum = 0, ans = -100 * N; 19 | for (int L = 1; L + K - 1 <= N; L++) { 20 | // L - 1 을 구간에서 제외하기 21 | sum -= A[L - 1]; 22 | 23 | // R 을 옮길 수 있을 때 까지 옮기기 24 | while (R + 1 <= L + K - 1) 25 | sum += A[++R]; 26 | 27 | // [L ... R] 의 합, 즉 sum이 조건을 만족하면 정답 갱신하기 28 | ans = max(ans, sum); 29 | } 30 | 31 | cout << ans; 32 | } 33 | 34 | 35 | int main() { 36 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 37 | input(); 38 | pro(); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/2559-수열/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n, K = list(map(int, si().split())) 4 | a = list(map(int, si().split())) 5 | R, sum, ans = -1, 0, -100 * n 6 | for L in range(n - K + 1): 7 | while R + 1 <= L + K - 1: 8 | R += 1 9 | sum += a[R] 10 | 11 | ans = max(ans, sum) 12 | 13 | sum -= a[L] 14 | 15 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/3273-두 수의 합/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 500005 8 | 9 | int N, S; 10 | int A[NM]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | cin >> S; 16 | } 17 | 18 | void pro() { 19 | sort(A + 1, A + 1 + N); 20 | int ans = 0; 21 | for (int L = 1, R = N; L < R;){ 22 | if (A[L] + A[R] == S) ans++; 23 | if (A[L] + A[R] >= S) R--; 24 | else L++; 25 | } 26 | cout << ans; 27 | } 28 | 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 32 | input(); 33 | pro(); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/07~08-두 포인터/문제별 코드/3273-두 수의 합/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | n = int(si()) 4 | a = list(map(int, si().split())) 5 | S = int(si()) 6 | a.sort() 7 | L, R, ans = 0, n - 1, 0 8 | while L < R: 9 | if a[L] + a[R] == S: 10 | ans += 1 11 | if a[L] + a[R] >= S: 12 | R -= 1 13 | else: 14 | L += 1 15 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-09-그래프와 탐색.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-09-그래프와 탐색.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-10-그래프와 탐색-응용.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-10-그래프와 탐색-응용.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-11-그래프와 탐색-응용-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/09~11-그래프 탐색/02-알고리즘-11-그래프와 탐색-응용-2.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1012-유기농 배추/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | sys.setrecursionlimit(100000) 5 | 6 | dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] 7 | def dfs(x, y): 8 | visit[x][y] = True 9 | for dx, dy in dir: 10 | nx, ny = x + dx, y + dy 11 | if nx < 0 or nx >= n or ny < 0 or ny >= m: continue 12 | if visit[nx][ny]: continue 13 | if a[nx][ny] == 0: continue 14 | dfs(nx, ny) 15 | 16 | T = int(si()) 17 | while T: 18 | T -= 1 19 | m, n, K = map(int, si().split()) 20 | a = [[0] * m for _ in range(n)] 21 | for i in range(K): 22 | y, x = map(int, si().split()) 23 | a[x][y] = 1 24 | 25 | visit = [[False] * m for _ in range(n)] 26 | 27 | ans = 0 28 | for i in range(n): 29 | for j in range(m): 30 | if a[i][j] == 0 or visit[i][j]: 31 | continue 32 | ans += 1 33 | dfs(i, j) 34 | 35 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11403-경로 찾기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | 6 | #include 7 | int adj[105][105]; 8 | int n, m; 9 | void input() { 10 | cin >> n; 11 | for (int i = 1;i <= n;i++){ 12 | for (int j = 1;j <= n;j++){ 13 | cin >> adj[i][j]; 14 | } 15 | } 16 | } 17 | 18 | bool visit[105]; 19 | #include 20 | void BFS(int S) { 21 | for (int i = 1; i <= n; i++) visit[i] = false; 22 | queue Q; 23 | Q.push(S); 24 | while (!Q.empty()) { 25 | int x = Q.front(); Q.pop(); 26 | for (int y = 1; y <= n; y++){ 27 | if (adj[x][y] == 0 || visit[y]) continue; 28 | visit[y] = true; 29 | Q.push(y); 30 | } 31 | } 32 | for (int i = 1; i <= n; i++){ 33 | cout << (visit[i] ? 1 : 0) << " "; 34 | } 35 | cout << "\n"; 36 | } 37 | int main() { 38 | ios::sync_with_stdio(false); 39 | cin.tie(0); 40 | input(); 41 | for (int i = 1; i <= n; i++) 42 | BFS(i); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11403-경로 찾기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | adj = [list(map(int, si().split())) for _ in range(n)] 6 | 7 | def bfs(x): 8 | visit = [0] * (n) 9 | queue = deque() 10 | queue.append(x) 11 | while queue: 12 | x = queue.popleft() 13 | for y in range(n): 14 | if not adj[x][y] or visit[y]: 15 | continue 16 | visit[y] = 1 17 | queue.append(y) 18 | for i in range(n): 19 | print(visit[i], end=' ') 20 | print() 21 | 22 | for i in range(n): 23 | bfs(i) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11724-연결 요소의 개수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | 6 | #include 7 | vector adj[1005]; 8 | int n, m; 9 | void input() { 10 | cin >> n >> m; 11 | for (int i = 1; i <= m; i++) { 12 | int u, v; 13 | cin >> u >> v; 14 | adj[u].push_back(v); 15 | adj[v].push_back(u); 16 | } 17 | } 18 | 19 | bool visit[1005]; 20 | 21 | void DFS(int x) { 22 | visit[x] = true; 23 | for (int y : adj[x]) { 24 | if (visit[y]) continue; 25 | DFS(y); 26 | } 27 | } 28 | 29 | int main() { 30 | ios::sync_with_stdio(false); 31 | cin.tie(0); 32 | input(); 33 | int ans = 0; 34 | for (int i = 1; i <= n; i++) { 35 | if (visit[i]) continue; 36 | ans++; 37 | DFS(i); 38 | } 39 | cout << ans; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11724-연결 요소의 개수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = list(map(int, si().split())) 5 | adj = [[] for _ in range(n + 1)] 6 | 7 | for _ in range(m): 8 | x, y = list(map(int, si().split())) 9 | adj[x].append(y) 10 | adj[y].append(x) 11 | 12 | visit = [False] * (n + 1) 13 | def dfs(x): 14 | visit[x] = True 15 | for y in adj[x]: 16 | if visit[y]: 17 | continue 18 | dfs(y) 19 | 20 | ans = 0 21 | for i in range(1, n + 1): 22 | if visit[i]: continue 23 | ans += 1 24 | dfs(i) 25 | 26 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11725-트리의 부모 찾기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 100005 6 | 7 | #include 8 | vector adj[NM]; 9 | int n, m, parent[NM]; 10 | void input() { 11 | cin >> n; 12 | for (int i = 1; i < n; i++) { 13 | int u, v; 14 | cin >> u >> v; 15 | adj[u].push_back(v); 16 | adj[v].push_back(u); 17 | } 18 | } 19 | 20 | bool visit[NM]; 21 | #include 22 | void BFS(int S) { 23 | for (int i = 1; i <= n; i++) visit[i] = false; 24 | queue Q; 25 | Q.push(S), visit[S] = true; 26 | while (!Q.empty()) { 27 | int x = Q.front(); Q.pop(); 28 | for (int y : adj[x]) { 29 | if (visit[y]) continue; 30 | parent[y] = x; 31 | visit[y] = true; 32 | Q.push(y); 33 | } 34 | } 35 | } 36 | int main() { 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | input(); 40 | BFS(1); 41 | int ans = 0; 42 | for (int i = 2; i <= n; i++) 43 | cout << parent[i] << '\n'; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/11725-트리의 부모 찾기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | adj = [[] for _ in range(n + 1)] 6 | parent = [0] * (n + 1) 7 | 8 | for _ in range(n - 1): 9 | x, y = list(map(int, si().split())) 10 | adj[x].append(y) 11 | adj[y].append(x) 12 | 13 | visit = [0] * (n + 1) 14 | def bfs(x): 15 | queue = deque() 16 | queue.append(x) 17 | visit[x] = 1 18 | while queue: 19 | x = queue.popleft() 20 | for y in adj[x]: 21 | if visit[y]: 22 | continue 23 | visit[y] = 1 24 | parent[y] = x 25 | queue.append(y) 26 | 27 | bfs(1) 28 | for i in range(2, n + 1): 29 | print(parent[i]) 30 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1260-DFS와 BFS/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | 6 | #include 7 | vector adj[1005]; 8 | int n, m, V; 9 | void input() { 10 | cin >> n >> m >> V; 11 | for (int i = 1;i <= m; i++){ 12 | int u, v; 13 | cin >> u >> v; 14 | adj[u].push_back(v); 15 | adj[v].push_back(u); 16 | } 17 | for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end()); 18 | } 19 | 20 | int visit[1005], dist[1005]; 21 | 22 | void DFS(int x) { 23 | cout << x << " "; 24 | visit[x] = true; 25 | for (int y : adj[x]) { 26 | if (visit[y]) continue; 27 | DFS(y); 28 | } 29 | } 30 | 31 | #include 32 | void BFS(int S) { 33 | for (int i = 1; i <= n; i++) visit[i] = dist[i] = 0; 34 | queue Q; 35 | Q.push(S), visit[S] = 1, dist[S] = 0; 36 | while (!Q.empty()) { 37 | int x = Q.front(); Q.pop(); 38 | cout << x << " "; 39 | for (int y : adj[x]) { 40 | if (visit[y]) continue; 41 | visit[y] = 1; 42 | dist[y] = dist[x] + 1; 43 | Q.push(y); 44 | } 45 | } 46 | } 47 | int main() { 48 | ios::sync_with_stdio(false); 49 | cin.tie(0); 50 | input(); 51 | DFS(V); 52 | cout << "\n"; 53 | BFS(V); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1260-DFS와 BFS/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m, V = list(map(int, si().split())) 5 | adj = [[] for _ in range(n + 1)] 6 | 7 | for _ in range(m): 8 | x, y = list(map(int, si().split())) 9 | adj[x].append(y) 10 | adj[y].append(x) 11 | 12 | for i in range(1, n + 1): 13 | adj[i].sort() 14 | 15 | visit = [0] * (n + 1) 16 | def dfs(x): 17 | visit[x] = 1 18 | print(x, end=' ') 19 | for y in adj[x]: 20 | if visit[y]: 21 | continue 22 | dfs(y) 23 | 24 | def bfs(x): 25 | visit = [0] * (n + 1) 26 | queue = deque() 27 | queue.append(x) 28 | visit[x] = 1 29 | while queue: 30 | x = queue.popleft() 31 | print(x, end=' ') 32 | for y in adj[x]: 33 | if visit[y]: 34 | continue 35 | visit[y] = 1 36 | queue.append(y) 37 | 38 | dfs(V) 39 | print() 40 | bfs(V) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1389-케빈 베이컨의 6단계 법칙/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 105 6 | 7 | #include 8 | vector adj[NM]; 9 | int n, m, dist[NM]; 10 | void input() { 11 | cin >> n; 12 | cin >> m; 13 | for (int i = 1; i <= m; i++) { 14 | int u, v; 15 | cin >> u >> v; 16 | adj[u].push_back(v); 17 | adj[v].push_back(u); 18 | } 19 | } 20 | 21 | #include 22 | int BFS(int S) { 23 | int ret = 0; 24 | for (int i = 1; i <= n; i++) dist[i] = -1; 25 | queue Q; 26 | Q.push(S), dist[S] = 0; 27 | while (!Q.empty()) { 28 | int x = Q.front(); Q.pop(); 29 | ret += dist[x]; 30 | for (int y : adj[x]) { 31 | if (dist[y] != -1) continue; 32 | dist[y] = dist[x] + 1; 33 | Q.push(y); 34 | } 35 | } 36 | return ret; 37 | } 38 | 39 | void pro(){ 40 | int minV = BFS(1), minIdx = 1; 41 | for (int i = 2; i <= n; i++) { 42 | int v = BFS(i); 43 | if (minV > v) minV = v, minIdx = i; 44 | } 45 | cout << minIdx; 46 | } 47 | 48 | int main() { 49 | ios::sync_with_stdio(false); 50 | cin.tie(0); 51 | input(); 52 | pro(); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1389-케빈 베이컨의 6단계 법칙/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = list(map(int, si().split())) 5 | 6 | adj = [[] for _ in range(n + 1)] 7 | 8 | for _ in range(m): 9 | x, y = list(map(int, si().split())) 10 | adj[x].append(y) 11 | adj[y].append(x) 12 | 13 | def bfs(x): 14 | dist = [-1] * (n + 1) 15 | ret = 0 16 | queue = deque() 17 | queue.append(x) 18 | dist[x] = 0 19 | while queue: 20 | x = queue.popleft() 21 | ret += dist[x] 22 | for y in adj[x]: 23 | if dist[y] != -1: 24 | continue 25 | dist[y] = dist[x] + 1 26 | queue.append(y) 27 | return ret 28 | 29 | minV, minIdx = bfs(1), 1 30 | for i in range(2, n + 1): 31 | v = bfs(i) 32 | if minV > v: 33 | minV, minIdx = v, i 34 | 35 | print(minIdx) 36 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1697-숨바꼭질/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | int N, K; 10 | 11 | void input() { 12 | cin >> N >> K; 13 | } 14 | 15 | #include 16 | int dist[NM]; 17 | queue Q; 18 | void pro() { 19 | Q.push(N); 20 | for (int i = 0; i <= 100000; i++) dist[i] = -1; 21 | dist[N] = 0; 22 | while (!Q.empty()){ 23 | int x = Q.front(); 24 | Q.pop(); 25 | auto func = [](int x, int d){ 26 | if (x < 0 || x > 100000 || dist[x] != -1) return; 27 | dist[x] = d; 28 | Q.push(x); 29 | }; 30 | func(x - 1, dist[x] + 1); 31 | func(x + 1, dist[x] + 1); 32 | func(x * 2, dist[x] + 1); 33 | } 34 | cout << dist[K]; 35 | } 36 | 37 | 38 | int main() { 39 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 40 | input(); 41 | pro(); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/1697-숨바꼭질/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | N, K = list(map(int, si().split())) 5 | X = 100000 6 | 7 | dist = [-1] * (X + 1) 8 | 9 | def bfs(x): 10 | queue = deque() 11 | queue.append(x) 12 | dist[x] = 0 13 | def move(y, d): 14 | if y < 0 or y > X or dist[y] != -1: 15 | return 16 | dist[y] = d 17 | queue.append(y) 18 | 19 | while queue: 20 | x = queue.popleft() 21 | move(x - 1, dist[x] + 1) 22 | move(x + 1, dist[x] + 1) 23 | move(x * 2, dist[x] + 1) 24 | 25 | bfs(N) 26 | print(dist[K]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/18404-현명한 나이트/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = list(map(int, si().split())) 5 | sx, sy = list(map(int, si().split())) 6 | 7 | dist = [[-1] * (n + 1) for _ in range(n + 1)] 8 | dir = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)] 9 | 10 | q = deque() 11 | q.append((sx, sy)) 12 | dist[sx][sy] = 0 13 | while q: 14 | x, y = q.popleft() 15 | for dx, dy in dir: 16 | nx, ny = x + dx, y + dy 17 | if nx < 1 or ny < 1 or nx > n or ny > n: 18 | continue 19 | if dist[nx][ny] != -1: 20 | continue 21 | q.append((nx, ny)) 22 | dist[nx][ny] = dist[x][y] + 1 23 | 24 | for _ in range(m): 25 | ex, ey = list(map(int, si().split())) 26 | print(dist[ex][ey], end=' ') 27 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2178-미로 탐색/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 105 8 | 9 | int n, m; 10 | char a[NM][NM]; 11 | 12 | void input() { 13 | cin >> n >> m; 14 | for (int i = 1; i <= n; i++) { 15 | for (int j = 1; j <= m; j++) { 16 | cin >> a[i][j]; 17 | } 18 | } 19 | } 20 | 21 | #include 22 | int dist[NM][NM]; 23 | queue Q; 24 | int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} }; 25 | void pro() { 26 | Q.push(1); Q.push(1); 27 | dist[1][1] = 1; 28 | while (!Q.empty()) { 29 | int x = Q.front(); Q.pop(); 30 | int y = Q.front(); Q.pop(); 31 | for (int k = 0; k < 4; k++) { 32 | int nx = x + dir[k][0]; 33 | int ny = y + dir[k][1]; 34 | if (nx < 1 || ny < 1 || nx > n || ny > m) continue; 35 | if (a[nx][ny] == '0') continue; 36 | if (dist[nx][ny] != 0) continue; 37 | dist[nx][ny] = dist[x][y] + 1; 38 | Q.push(nx); Q.push(ny); 39 | } 40 | } 41 | cout << dist[n][m]; 42 | } 43 | 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 47 | input(); 48 | pro(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2178-미로 탐색/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = list(map(int, si().split())) 5 | a = [si().strip() for _ in range(n)] 6 | 7 | dist = [[0] * m for _ in range(n)] 8 | dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] 9 | 10 | def bfs(): 11 | queue = deque() 12 | queue.append(0) 13 | queue.append(0) 14 | dist[0][0] = 1 15 | 16 | while queue: 17 | x = queue.popleft() 18 | y = queue.popleft() 19 | for dx, dy in dir: 20 | nx, ny = x + dx, y + dy 21 | if nx < 0 or nx >= n or ny < 0 or ny >= m: continue 22 | if dist[nx][ny] != 0: continue 23 | if a[nx][ny] == '0': continue 24 | dist[nx][ny] = dist[x][y] + 1 25 | queue.append(nx) 26 | queue.append(ny) 27 | 28 | bfs() 29 | print(dist[n - 1][m - 1]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2251-물통/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | limit = list(map(int, si().split())) 5 | 6 | visit = [[[False] * 205 for _ in range(205)] for _ in range(205)] 7 | possible = [0] * 205 8 | 9 | def bfs(): 10 | def move(cur, f, t): 11 | res = [cur[0], cur[1], cur[2]] 12 | if cur[f] + cur[t] <= limit[t]: 13 | res[t] = res[f] + res[t] 14 | res[f] = 0 15 | else: 16 | res[f] -= limit[t] - res[t] 17 | res[t] = limit[t] 18 | return res 19 | 20 | queue = deque() 21 | queue.append([0, 0, limit[2]]) 22 | visit[0][0][limit[2]] = True 23 | 24 | while queue: 25 | cur = queue.popleft() 26 | if cur[0] == 0: possible[cur[2]] = True 27 | for f in range(3): 28 | for t in range(3): 29 | if f == t: continue 30 | nxt = move(cur, f, t) 31 | if visit[nxt[0]][nxt[1]][nxt[2]]: continue 32 | visit[nxt[0]][nxt[1]][nxt[2]] = True 33 | queue.append(nxt) 34 | 35 | 36 | bfs() 37 | for i in range(205): 38 | if possible[i]: 39 | print(i, end=' ') -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2606-바이러스/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | 6 | #include 7 | vector adj[105]; 8 | int n, m; 9 | void input() { 10 | cin >> n >> m; 11 | for (int i = 1; i <= m; i++) { 12 | int u, v; 13 | cin >> u >> v; 14 | adj[u].push_back(v); 15 | adj[v].push_back(u); 16 | } 17 | } 18 | 19 | bool visit[105]; 20 | #include 21 | void BFS(int S) { 22 | for (int i = 1; i <= n; i++) visit[i] = false; 23 | queue Q; 24 | Q.push(S), visit[S] = true; 25 | while (!Q.empty()) { 26 | int x = Q.front(); Q.pop(); 27 | for (int y : adj[x]) { 28 | if (visit[y]) continue; 29 | visit[y] = true; 30 | Q.push(y); 31 | } 32 | } 33 | } 34 | int main() { 35 | ios::sync_with_stdio(false); 36 | cin.tie(0); 37 | input(); 38 | BFS(1); 39 | int ans = 0; 40 | for (int i = 2; i <= n; i++) 41 | if (visit[i]) 42 | ans++; 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2606-바이러스/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | m = int(si()) 6 | adj = [[] for _ in range(n + 1)] 7 | 8 | for _ in range(m): 9 | x, y = list(map(int, si().split())) 10 | adj[x].append(y) 11 | adj[y].append(x) 12 | 13 | visit = [0] * (n + 1) 14 | def bfs(x): 15 | queue = deque() 16 | queue.append(x) 17 | visit[x] = 1 18 | while queue: 19 | x = queue.popleft() 20 | for y in adj[x]: 21 | if visit[y]: 22 | continue 23 | visit[y] = 1 24 | queue.append(y) 25 | 26 | bfs(1) 27 | print(sum(visit) - 1) 28 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2644-촌수 계산/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 105 6 | 7 | #include 8 | vector adj[NM]; 9 | int n, m, st, ed, dist[NM]; 10 | void input() { 11 | cin >> n; 12 | cin >> st >> ed; 13 | cin >> m; 14 | for (int i = 1; i <= m; i++) { 15 | int u, v; 16 | cin >> u >> v; 17 | adj[u].push_back(v); 18 | adj[v].push_back(u); 19 | } 20 | } 21 | 22 | #include 23 | void BFS(int S) { 24 | for (int i = 1; i <= n; i++) dist[i] = -1; 25 | queue Q; 26 | Q.push(S), dist[S] = 0; 27 | while (!Q.empty()) { 28 | int x = Q.front(); Q.pop(); 29 | for (int y : adj[x]) { 30 | if (dist[y] != -1) continue; 31 | dist[y] = dist[x] + 1; 32 | Q.push(y); 33 | } 34 | } 35 | } 36 | int main() { 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | input(); 40 | BFS(st); 41 | cout << dist[ed]; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2644-촌수 계산/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | st, ed = list(map(int, si().split())) 6 | m = int(si()) 7 | 8 | adj = [[] for _ in range(n + 1)] 9 | dist = [-1] * (n + 1) 10 | 11 | for _ in range(m): 12 | x, y = list(map(int, si().split())) 13 | adj[x].append(y) 14 | adj[y].append(x) 15 | 16 | def bfs(x): 17 | queue = deque() 18 | queue.append(x) 19 | dist[x] = 0 20 | while queue: 21 | x = queue.popleft() 22 | for y in adj[x]: 23 | if dist[y] != -1: 24 | continue 25 | dist[y] = dist[x] + 1 26 | queue.append(y) 27 | 28 | bfs(st) 29 | print(dist[ed]) 30 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/2667-단지번호 붙이기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | a = [si().strip() for _ in range(n)] 6 | 7 | visit = [[False] * n for _ in range(n)] 8 | dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] 9 | 10 | def dfs(x, y): 11 | global group_cnt 12 | group_cnt += 1 13 | visit[x][y] = True 14 | for dx, dy in dir: 15 | nx, ny = x + dx, y + dy 16 | if nx < 0 or nx >= n or ny < 0 or ny >= n: continue 17 | if visit[nx][ny]: continue 18 | if a[nx][ny] == '0': continue 19 | dfs(nx, ny) 20 | 21 | groups = [] 22 | group_cnt = 0 23 | for i in range(n): 24 | for j in range(n): 25 | if a[i][j] == '0' or visit[i][j]: 26 | continue 27 | group_cnt = 0 28 | dfs(i, j) 29 | groups.append(group_cnt) 30 | 31 | groups.sort() 32 | print(len(groups)) 33 | for g in groups: 34 | print(g) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/3184-양/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = map(int, si().split()) 5 | a = [si().strip() for _ in range(n)] 6 | sys.setrecursionlimit(100000) 7 | 8 | visit = [[False] * m for _ in range(n)] 9 | dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] 10 | totalSheep, totalWolf, sheep, wolf = 0, 0, 0, 0 11 | 12 | def dfs(x, y): 13 | global sheep, wolf 14 | if a[x][y] == 'o': 15 | sheep += 1 16 | if a[x][y] == 'v': 17 | wolf += 1 18 | visit[x][y] = True 19 | for dx, dy in dir: 20 | nx, ny = x + dx, y + dy 21 | if nx < 0 or nx >= n or ny < 0 or ny >= m: continue 22 | if visit[nx][ny]: continue 23 | if a[nx][ny] == '#': continue 24 | dfs(nx, ny) 25 | 26 | for i in range(n): 27 | for j in range(m): 28 | if a[i][j] != '#' and not visit[i][j]: 29 | sheep, wolf = 0, 0 30 | dfs(i, j) 31 | if sheep > wolf: totalSheep += sheep 32 | else: totalWolf += wolf 33 | print(totalSheep, totalWolf) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/4963-섬의 개수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 55 8 | 9 | int N, M, K; 10 | int a[NM][NM]; 11 | 12 | void input() { 13 | cin >> M >> N; 14 | for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) cin >> a[i][j]; 15 | } 16 | 17 | int dir[8][2] = { {1,0},{0,1},{-1,0},{0,-1}, {1, 1},{1,-1},{-1,1},{-1,-1} }; 18 | bool visit[NM][NM]; 19 | 20 | // x, y 를 갈 수 있다는 걸 알고 방문한 상태 21 | void dfs(int x, int y) { 22 | visit[x][y] = true; 23 | for (int k = 0; k < 8; k++) { 24 | int nx = x + dir[k][0]; 25 | int ny = y + dir[k][1]; 26 | if (nx < 1 || ny < 1 || nx > N || ny > M) continue; // 지도를 벗어나는 곳으로 가는가? 27 | if (a[nx][ny] == 0) continue; // 갈 수 있는 칸인지 확인해야 한다. 28 | if (visit[nx][ny]) continue; // 이미 방문한 적이 있는 곳인가? 29 | dfs(nx, ny); 30 | } 31 | } 32 | void pro() { 33 | int ans = 0; 34 | for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) visit[i][j] = false; 35 | for (int i = 1; i <= N; i++) 36 | for (int j = 1; j <= M; j++) 37 | if (a[i][j] == 1 && !visit[i][j]) { 38 | ans++; 39 | dfs(i, j); 40 | } 41 | cout << ans << "\n"; 42 | } 43 | 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 47 | while (true) { 48 | input(); 49 | if (N == 0 && M == 0) break; 50 | pro(); 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/4963-섬의 개수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | sys.setrecursionlimit(100000) 5 | 6 | dir = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]] 7 | def dfs(x, y): 8 | visit[x][y] = True 9 | for dx, dy in dir: 10 | nx, ny = x + dx, y + dy 11 | if nx < 0 or nx >= n or ny < 0 or ny >= m: continue 12 | if visit[nx][ny]: continue 13 | if a[nx][ny] == 0: continue 14 | dfs(nx, ny) 15 | 16 | while True: 17 | m, n = map(int, si().split()) 18 | if m == 0 and n == 0: 19 | break 20 | a = [list(map(int, si().split())) for _ in range(n)] 21 | 22 | visit = [[False] * m for _ in range(n)] 23 | 24 | ans = 0 25 | for i in range(n): 26 | for j in range(m): 27 | if a[i][j] == 0 or visit[i][j]: 28 | continue 29 | ans += 1 30 | dfs(i, j) 31 | 32 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/5567-결혼식/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 505 6 | 7 | #include 8 | vector adj[NM]; 9 | int n, m, dist[NM]; 10 | void input() { 11 | cin >> n; 12 | cin >> m; 13 | for (int i = 1; i <= m; i++) { 14 | int u, v; 15 | cin >> u >> v; 16 | adj[u].push_back(v); 17 | adj[v].push_back(u); 18 | } 19 | } 20 | 21 | #include 22 | int BFS(int S) { 23 | int ret = 0; 24 | for (int i = 1; i <= n; i++) dist[i] = -1; 25 | queue Q; 26 | Q.push(S), dist[S] = 0; 27 | while (!Q.empty()) { 28 | int x = Q.front(); Q.pop(); 29 | if (1 <= dist[x] && dist[x] <= 2) ret++; 30 | if (dist[x] == 2) continue; 31 | for (int y : adj[x]) { 32 | if (dist[y] != -1) continue; 33 | dist[y] = dist[x] + 1; 34 | Q.push(y); 35 | } 36 | } 37 | return ret; 38 | } 39 | 40 | void pro(){ 41 | cout << BFS(1); 42 | } 43 | 44 | int main() { 45 | ios::sync_with_stdio(false); 46 | cin.tie(0); 47 | input(); 48 | pro(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/5567-결혼식/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | m = int(si()) 6 | 7 | adj = [[] for _ in range(n + 1)] 8 | 9 | for _ in range(m): 10 | x, y = list(map(int, si().split())) 11 | adj[x].append(y) 12 | adj[y].append(x) 13 | 14 | def bfs(x): 15 | dist = [-1] * (n + 1) 16 | ret = 0 17 | queue = deque() 18 | queue.append(x) 19 | dist[x] = 0 20 | while queue: 21 | x = queue.popleft() 22 | if 1 <= dist[x] <= 2: 23 | ret += 1 24 | if dist[x] == 2: 25 | continue 26 | for y in adj[x]: 27 | if dist[y] != -1: 28 | continue 29 | dist[y] = dist[x] + 1 30 | queue.append(y) 31 | return ret 32 | 33 | print(bfs(1)) 34 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/09~11-그래프 탐색/문제별 코드/7562-나이트의 이동/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | TT = int(si()) 5 | for _TT in range(TT): 6 | n = int(si()) 7 | sx, sy = list(map(int, si().split())) 8 | ex, ey = list(map(int, si().split())) 9 | 10 | dist = [[-1] * n for _ in range(n)] 11 | dir = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)] 12 | 13 | q = deque() 14 | q.append((sx, sy)) 15 | dist[sx][sy] = 0 16 | while q: 17 | x, y = q.popleft() 18 | for dx, dy in dir: 19 | nx, ny = x + dx, y + dy 20 | if nx < 0 or ny < 0 or nx >= n or ny >= n: 21 | continue 22 | if dist[nx][ny] != -1: 23 | continue 24 | q.append((nx, ny)) 25 | dist[nx][ny] = dist[x][y] + 1 26 | print(dist[ex][ey]) 27 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/02-알고리즘-12-트리.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/12-트리/02-알고리즘-12-트리.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1068-트리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 55 6 | 7 | #include 8 | int n, root, erased, leaf[NM]; 9 | vector child[NM]; 10 | 11 | void input() { 12 | cin >> n; 13 | for (int i = 0; i < n; i++) { 14 | int par; 15 | cin >> par; 16 | if (par == -1) { 17 | root = i; 18 | } 19 | else { 20 | child[par].push_back(i); 21 | } 22 | } 23 | cin >> erased; 24 | } 25 | 26 | void DFS(int x, int par) { 27 | if (child[x].empty()) 28 | leaf[x]++; 29 | for (int y : child[x]) { 30 | if (y == par) continue; 31 | DFS(y, x); 32 | leaf[x] += leaf[y]; 33 | } 34 | } 35 | 36 | void pro() { 37 | for (int i = 0; i < n; i++) { 38 | for (auto it = child[i].begin(); it != child[i].end(); it++){ 39 | if (*it == erased) { 40 | child[i].erase(it); 41 | break; 42 | } 43 | } 44 | } 45 | 46 | if (root != erased) DFS(root, -1); 47 | 48 | cout << leaf[root]; 49 | } 50 | 51 | int main() { 52 | ios::sync_with_stdio(false); 53 | cin.tie(0); 54 | input(); 55 | pro(); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1068-트리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | parent = list(map(int, si().split())) 6 | child = [[] for _ in range(n)] 7 | leaf = [0] * n 8 | erased = int(si()) 9 | root = 0 10 | for i in range(n): 11 | if parent[i] == -1: 12 | root = i 13 | else: 14 | if erased == i: 15 | continue 16 | child[parent[i]].append(i) 17 | 18 | def dfs(x, par): 19 | if not child[x]: 20 | leaf[x] = 1 21 | for y in child[x]: 22 | if y == par: 23 | continue 24 | dfs(y, x) 25 | leaf[x] += leaf[y] 26 | 27 | if root != erased: 28 | dfs(root, -1) 29 | print(leaf[root]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/11725-트리의 부모 찾기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 100005 6 | 7 | #include 8 | vector adj[NM]; 9 | int n, m, parent[NM]; 10 | void input() { 11 | cin >> n; 12 | for (int i = 1; i < n; i++) { 13 | int u, v; 14 | cin >> u >> v; 15 | adj[u].push_back(v); 16 | adj[v].push_back(u); 17 | } 18 | } 19 | 20 | bool visit[NM]; 21 | void DFS(int x, int par) { 22 | for (int y: adj[x]){ 23 | if (y == par) continue; 24 | parent[y] = x; 25 | DFS(y, x); 26 | } 27 | } 28 | 29 | void pro(){ 30 | DFS(1, -1); 31 | for (int i = 2; i <= n; i++) { 32 | cout << parent[i] << '\n'; 33 | } 34 | } 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | input(); 40 | pro(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/11725-트리의 부모 찾기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | adj = [[] for _ in range(n + 1)] 6 | parent = [0] * (n + 1) 7 | sys.setrecursionlimit(100005) 8 | 9 | for _ in range(n - 1): 10 | x, y = list(map(int, si().split())) 11 | adj[x].append(y) 12 | adj[y].append(x) 13 | 14 | def dfs(x, par): 15 | for y in adj[x]: 16 | if y == par: 17 | continue 18 | parent[y] = x 19 | dfs(y, x) 20 | 21 | dfs(1, -1) 22 | for i in range(2, n + 1): 23 | print(parent[i]) 24 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1240-노드 사이의 거리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int n, m; 9 | vector> con[NM]; 10 | 11 | void input() { 12 | cin >> n >> m; 13 | for (int i = 1; i < n; i++) { 14 | int u, v, c; 15 | cin >> u >> v >> c; 16 | con[u].push_back({ v, c }); 17 | con[v].push_back({ u, c }); 18 | } 19 | } 20 | 21 | int ans = 0; 22 | // 현재 x 에 있으며, 부모 노드는 prev 이며, 목표 지점은 goal, 23 | // 그리고 root부터 지금까지 이동 거리가 dist 이다. 24 | void DFS(int x, int prev, int goal, int dist) { 25 | if (x == goal) { 26 | ans = dist; 27 | return; 28 | } 29 | for (auto& p : con[x]) { 30 | auto [y, c] = p; 31 | if (y == prev) continue; 32 | DFS(y, x, goal, dist + c); 33 | } 34 | } 35 | 36 | #include 37 | void pro() { 38 | // 세 가지 풀이가 가능하다. 39 | // 1. 트리도 일종의 그래프이므로 최단거리 알고리즘인 Dijkstra 사용 40 | // 2. 트리에서 두 노드 사이의 경로는 유일한 점을 활용해서 BFS 사용 41 | // 3. 트리를 Rooted 트리로 변환해서 구현이 짧은 DFS를 통해 거리 계산하기. 42 | // 마찬가지로 두 노드 사이의 경로가 유일함을 확인한다. 43 | 44 | // 본 풀이는 3 번 접근을 다룬다. 45 | 46 | int x, y; 47 | cin >> x >> y; 48 | DFS(x, -1, y, 0); 49 | cout << ans << '\n'; 50 | } 51 | 52 | int main() { 53 | ios::sync_with_stdio(false); 54 | cin.tie(0); 55 | input(); 56 | while (m--) { 57 | pro(); 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1240-노드 사이의 거리/solution.py: -------------------------------------------------------------------------------- 1 | # 세 가지 풀이가 가능하다. 2 | # 1. 트리도 일종의 그래프이므로 최단거리 알고리즘인 Dijkstra 사용 3 | # 2. 트리에서 두 노드 사이의 경로는 유일한 점을 활용해서 BFS 사용 4 | # 3. 트리를 Rooted 트리로 변환해서 구현이 짧은 DFS를 통해 거리 계산하기. 5 | # 마찬가지로 두 노드 사이의 경로가 유일함을 확인한다. 6 | # 본 풀이는 3 번 접근을 다룬다. 7 | 8 | import sys 9 | si = sys.stdin.readline 10 | 11 | n, m = map(int, si().split()) 12 | con = [[] for _ in range(n + 1)] 13 | for i in range(n - 1): 14 | x, y, c = map(int, si().split()) 15 | con[x].append((y, c)) 16 | con[y].append((x, c)) 17 | 18 | # 현재 x 에 있으며, 부모 노드는 prev 이며, 목표 지점은 goal, 19 | # 그리고 root부터 지금까지 이동 거리가 dist 이다. 20 | def DFS(x, prev, goal, dist): 21 | if x == goal: 22 | print(dist) 23 | return 24 | for y, c in con[x]: 25 | if y == prev: 26 | continue 27 | DFS(y, x, goal, dist + c) 28 | 29 | for _ in range(m): 30 | x, y = map(int, si().split()) 31 | DFS(x, -1, y, 0) 32 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/14267-회사 문화 1/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int n, m; 9 | int Dy[NM]; 10 | vector children[NM]; 11 | 12 | void input() { 13 | cin >> n >> m; 14 | for (int i = 1; i <= n; i++) { 15 | int par; 16 | cin >> par; 17 | if (i == 1) continue; 18 | children[par].push_back(i); 19 | } 20 | for (int i = 1; i <= m; i++) { 21 | int x, v; 22 | cin >> x >> v; 23 | Dy[x] += v; 24 | } 25 | } 26 | 27 | // 내가 받은 칭찬을 부하 직원들에게 누적시켜주는 작업을 해주면 된다. 28 | void DFS(int x) { 29 | for (int y : children[x]) { 30 | Dy[y] += Dy[x]; 31 | DFS(y); 32 | } 33 | } 34 | void pro() { 35 | DFS(1); 36 | for (int i = 1; i <= n; i++) cout << Dy[i] << " "; 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/14267-회사 문화 1/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n, m = map(int, si().split()) 5 | Dy = [0] * (n + 1) 6 | children = [[] for _ in range(n + 1)] 7 | sys.setrecursionlimit(100005) 8 | 9 | par = list(map(int, si().split())) 10 | for i in range(2, n + 1): 11 | children[par[i - 1]].append(i) 12 | 13 | for _ in range(m): 14 | x, v = map(int, si().split()) 15 | Dy[x] += v 16 | 17 | # 내가 받은 칭찬을 부하 직원들에게 누적시켜주는 작업을 해주면 된다. 18 | def dfs(x): 19 | for y in children[x]: 20 | Dy[y] += Dy[x] 21 | dfs(y) 22 | 23 | dfs(1) 24 | for i in range(1, n + 1): 25 | print(Dy[i], end=' ') 26 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/15681-트리와 쿼리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int n, R, Q; 9 | int Dy[NM]; 10 | vector con[NM]; 11 | 12 | void input() { 13 | cin >> n >> R >> Q; 14 | for (int i = 1; i < n; i++) { 15 | int x, y; 16 | cin >> x >> y; 17 | con[x].push_back(y); 18 | con[y].push_back(x); 19 | } 20 | } 21 | 22 | // Dy[x] 를 계산하는 함수 23 | void DFS(int x, int prev) { 24 | Dy[x] = 1; 25 | for (int y : con[x]) { 26 | if (y == prev) continue; 27 | DFS(y, x); 28 | Dy[x] += Dy[y]; 29 | } 30 | } 31 | void pro() { 32 | DFS(R, -1); 33 | while (Q--) { 34 | int x; cin >> x; 35 | cout << Dy[x] << '\n'; 36 | } 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/15681-트리와 쿼리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | n, R, Q = map(int, si().split()) 6 | con = [[] for _ in range(n + 1)] 7 | for _ in range(n - 1): 8 | x, y = map(int, si().split()) 9 | con[x].append(y) 10 | con[y].append(x) 11 | 12 | Dy = [0] * (n + 1) 13 | # Dy[x] 를 계산하는 함수 14 | def DFS(x, prev): 15 | global Dy 16 | Dy[x] = 1 17 | for y in con[x]: 18 | if y == prev: continue 19 | DFS(y, x) 20 | Dy[x] += Dy[y] 21 | 22 | DFS(R, -1) 23 | 24 | for _ in range(Q): 25 | print(Dy[int(si())]) 26 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/15900-나무 탈출/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 500005 6 | 7 | #include 8 | int n; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n; 13 | for (int i = 1; i < n; i++) { 14 | int x, y; 15 | cin >> x >> y; 16 | con[x].push_back(y); 17 | con[y].push_back(x); 18 | } 19 | } 20 | 21 | // 모든 leaf node 들의 깊이의 합이 짝수냐 홀수냐가 승리 요건 22 | // sum_leaf_depth: 모든 leaf node 들의 깊이의 합 23 | int sum_leaf_depth; 24 | void dfs(int x, int prev, int depth) { 25 | if (x != 1 && con[x].size() == 1) sum_leaf_depth += depth; 26 | for (int y : con[x]) { 27 | if (y == prev) continue; 28 | dfs(y, x, depth + 1); 29 | } 30 | } 31 | 32 | void pro() { 33 | dfs(1, -1, 0); 34 | if (sum_leaf_depth % 2 == 0) cout << "No"; 35 | else cout << "Yes"; 36 | } 37 | 38 | int main() { 39 | ios::sync_with_stdio(false); 40 | cin.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/15900-나무 탈출/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(500005) 3 | si = sys.stdin.readline 4 | 5 | # 모든 leaf node 들의 깊이의 합이 짝수냐 홀수냐가 승리 요건 6 | # sum_leaf_depth: 모든 leaf node 들의 깊이의 합 7 | sum_leaf_depth = 0 8 | con = [] 9 | 10 | def input(): 11 | global con 12 | n = int(si()) 13 | con = [[] for _ in range(n + 1)] 14 | for i in range(n - 1): 15 | x, y = map(int, si().split()) 16 | con[x].append(y) 17 | con[y].append(x) 18 | 19 | 20 | def dfs(x, prev, depth): 21 | global con, sum_leaf_depth 22 | if x != 1 and len(con[x]) == 1: 23 | sum_leaf_depth += depth 24 | for y in con[x]: 25 | if y == prev: continue 26 | dfs(y, x, depth + 1) 27 | 28 | 29 | def pro(): 30 | dfs(1, -1, 0) 31 | print("Yes" if sum_leaf_depth % 2 == 1 else "No") 32 | 33 | 34 | if __name__ == "__main__": 35 | input() 36 | pro() -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1991-트리 순회/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 55 6 | 7 | #include 8 | int n; 9 | int childs[NM][2]; 10 | 11 | void input() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) { 14 | char x; 15 | cin >> x; 16 | int X = x - 'A'; 17 | for (int k = 0; k < 2; k++) { 18 | char ch; 19 | cin >> ch; 20 | if (ch == '.') childs[X][k] = -1; 21 | else childs[X][k] = ch - 'A'; 22 | } 23 | } 24 | } 25 | 26 | void in_order(int x) { 27 | if (x == -1) return; 28 | in_order(childs[x][0]); 29 | cout << (char)(x + 'A'); 30 | in_order(childs[x][1]); 31 | } 32 | 33 | void pre_order(int x) { 34 | if (x == -1) return; 35 | cout << (char)(x + 'A'); 36 | pre_order(childs[x][0]); 37 | pre_order(childs[x][1]); 38 | } 39 | 40 | void post_order(int x) { 41 | if (x == -1) return; 42 | post_order(childs[x][0]); 43 | post_order(childs[x][1]); 44 | cout << (char)(x + 'A'); 45 | } 46 | 47 | void pro() { 48 | pre_order(0); 49 | cout << '\n'; 50 | in_order(0); 51 | cout << '\n'; 52 | post_order(0); 53 | } 54 | 55 | int main() { 56 | ios::sync_with_stdio(false); 57 | cin.tie(0); 58 | input(); 59 | pro(); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/1991-트리 순회/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | n = int(si()) 5 | childs = [[-1, -1] for _ in range(n)] 6 | 7 | def input(): 8 | for _ in range(n): 9 | x, l, r = si().strip().split() 10 | if l != '.': childs[ord(x) - ord('A')][0] = ord(l) - ord('A') 11 | if r != '.': childs[ord(x) - ord('A')][1] = ord(r) - ord('A') 12 | 13 | 14 | def in_order(x): 15 | if x == -1: return 16 | in_order(childs[x][0]) 17 | print(chr(x + 65), end='') 18 | in_order(childs[x][1]) 19 | 20 | 21 | def pre_order(x): 22 | if x == -1: return 23 | print(chr(x + 65), end='') 24 | pre_order(childs[x][0]) 25 | pre_order(childs[x][1]) 26 | 27 | 28 | def post_order(x): 29 | if x == -1: return 30 | post_order(childs[x][0]) 31 | post_order(childs[x][1]) 32 | print(chr(x + 65), end='') 33 | 34 | 35 | def pro(): 36 | pre_order(0) 37 | print() 38 | in_order(0) 39 | print() 40 | post_order(0) 41 | 42 | 43 | if __name__ == "__main__": 44 | input() 45 | pro() -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/20364-부동산 다툼/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 1000005 6 | 7 | #include 8 | int n, Q, estate[NM]; 9 | 10 | void input() { 11 | cin >> n >> Q; 12 | } 13 | 14 | void pro() { 15 | // 사고 싶은 땅에서 시작해서 루트 땅까지 올라오면서 정답을 찾으면 편하다. 16 | // 왜냐하면 부모 노드는 찾기 쉽기 때문이다. 17 | while (Q--) { 18 | int x, y, res = 0; 19 | cin >> x; 20 | y = x; 21 | while (x) { 22 | if (estate[x]) res = x; 23 | x >>= 1; 24 | } 25 | estate[y] = 1; 26 | cout << res << "\n"; 27 | } 28 | } 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); 32 | cin.tie(0); 33 | input(); 34 | pro(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/20364-부동산 다툼/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(500005) 3 | si = sys.stdin.readline 4 | 5 | n, Q = map(int, si().split()) 6 | 7 | 8 | def pro(): 9 | # 사고 싶은 땅에서 시작해서 루트 땅까지 올라오면서 정답을 찾으면 편하다. 10 | # 왜냐하면 부모 노드는 찾기 쉽기 때문이다. 11 | estate = [False] * (n + 1) 12 | for _ in range(Q): 13 | x = int(si()) 14 | y = x 15 | res = 0 16 | while x: 17 | if estate[x]: res = x 18 | x //= 2 19 | estate[y] = True 20 | print(res) 21 | 22 | 23 | if __name__ == "__main__": 24 | pro() 25 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/3584-가장 가까운 공통 조상/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 10005 6 | 7 | int n, par[NM], check[NM], TT; 8 | 9 | void input() { 10 | cin >> n; 11 | for (int i = 1; i <= n; i++) par[i] = 0; 12 | for (int i = 1; i < n; i++) { 13 | int u, v; 14 | cin >> u >> v; 15 | par[v] = u; 16 | } 17 | } 18 | 19 | void pro() { 20 | int x, y; 21 | cin >> x >> y; 22 | // x 에서 루트까지 체크를 해놓는다. 23 | while (x) check[x] = TT, x = par[x]; 24 | 25 | // y 에서 루트로 이동하면서 처음으로 만나는 체크된 정점을 찾는다. 26 | while (y && check[y] != TT) y = par[y]; 27 | 28 | cout << y << '\n'; 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(false); 33 | cin.tie(0); 34 | int T; cin >> T; 35 | for (TT = 1; TT <= T; TT++) { 36 | input(); 37 | pro(); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/3584-가장 가까운 공통 조상/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | 5 | def solve(): 6 | n = int(si()) 7 | par = [0] * (n + 1) 8 | for _ in range(n - 1): 9 | u, v = map(int, si().split()) 10 | par[v] = u 11 | 12 | check = [0] * (n + 1) 13 | x, y = map(int, si().split()) 14 | 15 | # x 에서 루트까지 체크를 해놓는다. 16 | while x: 17 | check[x] = 1 18 | x = par[x] 19 | 20 | 21 | # y 에서 루트로 이동하면서 처음으로 만나는 체크된 정점을 찾는다. 22 | while y and check[y] == 0: 23 | y = par[y] 24 | print(y) 25 | 26 | 27 | T = int(si()) 28 | for _ in range(T): 29 | solve() 30 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/4803-트리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | 6 | #define NM 505 7 | 8 | #include 9 | int n, m; 10 | vector con[NM]; 11 | int visit[NM]; 12 | 13 | void input() { 14 | cin >> n >> m; 15 | for (int i = 1; i <= n; i++) con[i].clear(); 16 | for (int i = 1; i <= m; i++) { 17 | int x, y; 18 | cin >> x >> y; 19 | con[x].push_back(y); 20 | con[y].push_back(x); 21 | } 22 | } 23 | 24 | int v_cnt = 0, edge_cnt = 0; 25 | void dfs(int x) { 26 | visit[x] = 1; 27 | v_cnt++; 28 | edge_cnt += con[x].size(); 29 | for (int y : con[x]) { 30 | if (visit[y]) continue; 31 | dfs(y); 32 | } 33 | } 34 | 35 | void pro() { 36 | for (int i = 1; i <= n; i++) visit[i] = 0; 37 | int ans = 0; 38 | for (int i = 1; i <= n; i++) { 39 | if (visit[i] == 1) continue; 40 | v_cnt = 0; 41 | edge_cnt = 0; 42 | dfs(i); 43 | if (2 * (v_cnt - 1) == edge_cnt) ans++; 44 | } 45 | if (ans == 0) cout << "No trees.\n"; 46 | if (ans == 1) cout << "There is one tree.\n"; 47 | if (ans >= 2) cout << "A forest of " << ans << " trees.\n"; 48 | } 49 | 50 | int main() { 51 | ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 52 | for (int t=1;;t++) { 53 | input(); 54 | if (n == 0 && m == 0) break; 55 | cout << "Case " << t << ": "; 56 | pro(); 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/4803-트리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | def solve(tt): 5 | n, m = map(int, si().split()) 6 | if n == 0 and m == 0: 7 | return False 8 | adj = [[] for _ in range(n + 1)] 9 | visit = [0] * (n + 1) 10 | sys.setrecursionlimit(100005) 11 | 12 | for _ in range(m): 13 | x, y = list(map(int, si().split())) 14 | adj[x].append(y) 15 | adj[y].append(x) 16 | 17 | vertex_cnt = 0 18 | edge_cnt = 0 19 | def dfs(x): 20 | nonlocal vertex_cnt, edge_cnt 21 | visit[x] = True 22 | vertex_cnt += 1 23 | edge_cnt += len(adj[x]) 24 | for y in adj[x]: 25 | if visit[y]: 26 | continue 27 | dfs(y) 28 | 29 | ans = 0 30 | for i in range(1, n + 1): 31 | if visit[i]: continue 32 | vertex_cnt, edge_cnt = 0, 0 33 | dfs(i) 34 | if edge_cnt == (vertex_cnt - 1) * 2: 35 | ans += 1 36 | print(f'Case {tt}: ', end='') 37 | if ans == 0: 38 | print("No trees.") 39 | elif ans == 1: 40 | print("There is one tree.") 41 | else: 42 | print(f"A forest of {ans} trees.") 43 | return True 44 | 45 | tt = 0 46 | while True: 47 | tt = tt + 1 48 | if not solve(tt): 49 | break 50 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/5639-이진 검색 트리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #pragma warning(disable:4996) 4 | using namespace std; 5 | #define NM 10005 6 | 7 | #include 8 | int n; 9 | int a[NM]; 10 | 11 | void input() { 12 | while (!cin.eof()) { 13 | cin >> a[++n]; 14 | } 15 | n--; 16 | } 17 | 18 | // 현재 Subtree가 a[l...r] 일 때, 후위 순회를 하는 함수. 19 | void traverse(int l, int r) { 20 | if (l > r) return; 21 | int mid = r; // 왼쪽과 오른쪽 subtree를 가르는 기준 위치를 나타내는 변수 22 | for (int i = l + 1; i <= r; i++) { 23 | if (a[i] > a[l]) { 24 | mid = i - 1; 25 | break; 26 | } 27 | } 28 | 29 | // 후위 순회 30 | traverse(l + 1, mid); 31 | traverse(mid + 1, r); 32 | cout << a[l] << "\n"; 33 | } 34 | 35 | void pro() { 36 | traverse(1, n); 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/5639-이진 검색 트리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(10005) 3 | si = sys.stdin.readline 4 | a = [] 5 | 6 | def input(): 7 | while True: 8 | line = si() 9 | if not line: 10 | break 11 | a.append(int(line)) 12 | 13 | 14 | def traverse(l, r): 15 | if l > r: return 16 | mid = r 17 | for i in range(l + 1, r + 1): 18 | if a[i] > a[l]: 19 | mid = i - 1 20 | break 21 | 22 | traverse(l + 1, mid) 23 | traverse(mid + 1, r) 24 | print(a[l]) 25 | 26 | 27 | def pro(): 28 | traverse(0, len(a) - 1) 29 | 30 | 31 | if __name__ == "__main__": 32 | input() 33 | pro() -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/9489-사촌/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int n, K, a[NM], par[NM]; 9 | 10 | void input() { 11 | cin >> n >> K; 12 | for (int i = 1; i <= n; i++) { 13 | cin >> a[i]; 14 | } 15 | } 16 | 17 | #include 18 | void pro() { 19 | // 각 노드의 부모 노드 인덱스를 계산하자. 루트 노드가 1번 노드임을 주의하라. 20 | // 편의상 0번 정점의 부모를 -1 로 하자. 21 | par[0] = -1; 22 | par[1] = 0; 23 | 24 | // last := 이번에 자식 정점들을 찾을 노드의 인덱스 25 | int last = 1; 26 | for (int i = 2; i <= n; i++, last++) { 27 | for (; i <= n; i++) { // i번부터 연속한 수를 가진 정점을 모두 last 의 자식으로 묶는다. 28 | par[i] = last; 29 | if (i < n && a[i] + 1 != a[i + 1]) { 30 | break; 31 | } 32 | } 33 | } 34 | 35 | int kIdx = 0; // 수가 K인 정점의 인덱스 36 | for (int i = 1; i <= n; i++) if (a[i] == K) kIdx = i; 37 | int ans = 0; 38 | for (int i = 1; i <= n; i++) 39 | // 사촌 := 부모의 부모는 동일하나 부모는 다른 정점의 개수 40 | if (par[par[i]] == par[par[kIdx]] && par[i] != par[kIdx]) 41 | ans++; 42 | cout << ans << '\n'; 43 | } 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); 47 | cin.tie(0); 48 | for (;;) { 49 | input(); 50 | if (n == 0 && K == 0) break; 51 | pro(); 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/12-트리/문제별 코드/9489-사촌/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | while True: 5 | n, K = map(int, si().split()) 6 | if n == 0 and K == 0: break 7 | a = [0] + list(map(int, si().split())) 8 | 9 | # 각 노드의 부모 노드 인덱스를 계산하자. 루트 노드가 1번 노드임을 주의하라. 10 | # 편의상 0번 정점의 부모를 -1 로 하자. 11 | par = [0] * (n + 1) 12 | par[0] = -1 13 | 14 | # last := 이번에 자식 정점들을 찾을 노드의 인덱스 15 | last = 1 16 | # i := 이번에 부모 정점을 찾을 노드의 인덱스 17 | i = 2 18 | while i <= n: 19 | while i <= n: # i번부터 연속한 수를 가진 정점을 모두 last 의 자식으로 묶는다. 20 | par[i] = last 21 | if i < n and a[i] + 1 != a[i + 1]: 22 | break 23 | i += 1 24 | i += 1 25 | last += 1 26 | 27 | kIdx = 0 # 수가 K인 정점의 인덱스 28 | for i in range(1, n + 1): 29 | if a[i] == K: 30 | kIdx = i 31 | 32 | ans = 0 33 | for i in range(1, n + 1): 34 | # 사촌 := 부모의 부모는 동일하나 부모는 다른 정점의 개수 35 | if par[par[i]] == par[par[kIdx]] and par[i] != par[kIdx]: 36 | ans += 1 37 | 38 | print(ans) 39 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/02-알고리즘-13-위상정렬.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/13-위상정렬/02-알고리즘-13-위상정렬.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/1005-ACM Craft/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int n, m, indeg[NM], T[NM], T_done[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n >> m; 13 | for (int i = 1; i <= n; i++) { 14 | cin >> T[i]; 15 | T_done[i] = 0; 16 | indeg[i] = 0; 17 | con[i].clear(); 18 | } 19 | for (int i = 1; i <= m; i++) { 20 | int x, y; 21 | cin >> x >> y; 22 | con[x].push_back(y); 23 | // indegree 계산하기 24 | indeg[y]++; 25 | } 26 | } 27 | 28 | #include 29 | void pro() { 30 | queue Q; 31 | 32 | // 제일 앞에 "정렬될 수 있는" 정점 찾기 33 | for (int i = 1; i <= n; i++) 34 | if (indeg[i] == 0) { 35 | Q.push(i); 36 | T_done[i] = T[i]; 37 | } 38 | 39 | // 위상 정렬 순서대로 T_done 계산을 함께 해주기 40 | while (!Q.empty()) { 41 | int x = Q.front(); 42 | Q.pop(); 43 | for (int y : con[x]) { 44 | indeg[y]--; 45 | if (indeg[y] == 0) Q.push(y); 46 | T_done[y] = max(T_done[y], T_done[x] + T[y]); 47 | } 48 | } 49 | 50 | int W; 51 | cin >> W; 52 | cout << T_done[W] << '\n'; 53 | } 54 | 55 | int main() { 56 | ios::sync_with_stdio(false); 57 | cin.tie(0); 58 | int Q; 59 | cin >> Q; 60 | while (Q--) { 61 | input(); 62 | pro(); 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/1005-ACM Craft/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | for _ in range(Q): 7 | n, m = map(int, si().split()) 8 | T = [0] + list(map(int, si().split())) 9 | T_done = [0] * (n + 1) 10 | con = [[] for _ in range(n + 1)] 11 | indeg = [0] * (n + 1) 12 | 13 | for _ in range(m): 14 | x, y = map(int, si().split()) 15 | con[x].append(y) 16 | # indegree 계산하기 17 | indeg[y] += 1 18 | 19 | # 제일 앞에 "정렬될 수 있는" 정점 찾기 20 | Q = deque() 21 | for i in range(1, n + 1): 22 | if indeg[i] == 0: 23 | Q.append(i) 24 | T_done[i] = T[i] 25 | 26 | # 위상 정렬 순서대로 T_done 계산을 함께 해주기 27 | while Q: 28 | x = Q.popleft() 29 | for y in con[x]: 30 | indeg[y] -= 1 31 | if indeg[y] == 0: 32 | Q.append(y) 33 | T_done[y] = max(T_done[y], T_done[x] + T[y]) 34 | 35 | W = int(si()) 36 | print(T_done[W]) 37 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/14676-영우는 사기꾼/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int K, n, m, indeg[NM], cnt[NM], satisfaction[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n >> m >> K; 13 | for (int i = 1; i <= m; i++) { 14 | int x, y; 15 | cin >> x >> y; 16 | con[x].push_back(y); 17 | indeg[y]++; 18 | } 19 | } 20 | 21 | void pro() { 22 | bool abnormal = false; 23 | while (K--) { 24 | int t, x; 25 | cin >> t >> x; 26 | if (t == 1){ 27 | // x 의 선행 건물들이 모두 지어졌는 지 확인하기 28 | if (satisfaction[x] < indeg[x]) 29 | abnormal = true; 30 | 31 | cnt[x]++; 32 | // x 가 처음 지어진 것이라면 x가 영향을 주는 건물들에 "너희의 선행 건물 중 하나가 처음 지어졌어" 라고 알려주기 33 | if (cnt[x] == 1) 34 | for (int y: con[x]) 35 | satisfaction[y]++; 36 | } 37 | else{ 38 | // x 라는 건물이 한 개 이상 지어져 있는 지 확인하기 39 | if (cnt[x] == 0) 40 | abnormal = true; 41 | 42 | cnt[x]--; 43 | // x 가 더 이상 남아있지 않다면, "너희의 선행 건물 중 하나가 사라졌어" 라고 알려주기 44 | if (cnt[x] == 0) 45 | for (int y: con[x]) 46 | satisfaction[y]--; 47 | } 48 | } 49 | if (abnormal) cout << "Lier!"; 50 | else cout << "King-God-Emperor"; 51 | } 52 | 53 | int main() { 54 | ios::sync_with_stdio(false); 55 | cin.tie(0); 56 | input(); 57 | pro(); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/14676-영우는 사기꾼/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n, m, K = map(int, si().split()) 6 | cnt = [0] * (n + 1) 7 | con = [[] for _ in range(n + 1)] 8 | indeg = [0] * (n + 1) 9 | satisfaction = [0] * (n + 1) 10 | 11 | for _ in range(m): 12 | x, y = map(int, si().split()) 13 | con[x].append(y) 14 | indeg[y] += 1 15 | 16 | abnormal = False 17 | for _ in range(K): 18 | t, x = map(int, si().split()) 19 | if t == 1: 20 | if satisfaction[x] < indeg[x]: 21 | abnormal = True 22 | cnt[x] += 1 23 | if cnt[x] == 1: 24 | for y in con[x]: 25 | satisfaction[y] += 1 26 | else: 27 | if cnt[x] == 0: 28 | abnormal = True 29 | cnt[x] -= 1 30 | if cnt[x] == 0: 31 | for y in con[x]: 32 | satisfaction[y] -= 1 33 | 34 | print("King-God-Emperor" if not abnormal else "Lier!") 35 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/1516-게임 개발/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 505 7 | 8 | int n, m, indeg[NM], T[NM], T_done[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) { 14 | cin >> T[i]; 15 | int y; 16 | while ((cin >> y) && y != -1){ 17 | con[y].push_back(i); 18 | indeg[i]++; 19 | } 20 | } 21 | } 22 | 23 | #include 24 | void pro() { 25 | queue Q; 26 | 27 | // 제일 앞에 "정렬될 수 있는" 정점 찾기 28 | for (int i = 1; i <= n; i++) 29 | if (indeg[i] == 0) { 30 | Q.push(i); 31 | T_done[i] = T[i]; 32 | } 33 | 34 | // 위상 정렬 순서대로 T_done 계산을 함께 해주기 35 | while (!Q.empty()) { 36 | int x = Q.front(); 37 | Q.pop(); 38 | for (int y : con[x]) { 39 | indeg[y]--; 40 | if (indeg[y] == 0) Q.push(y); 41 | T_done[y] = max(T_done[y], T_done[x] + T[y]); 42 | } 43 | } 44 | 45 | for (int i = 1; i <= n; i++){ 46 | cout << T_done[i] << "\n"; 47 | } 48 | } 49 | 50 | int main() { 51 | ios::sync_with_stdio(false); 52 | cin.tie(0); 53 | input(); 54 | pro(); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/1516-게임 개발/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | T = [0] * (n + 1) 7 | T_done = [0] * (n + 1) 8 | con = [[] for _ in range(n + 1)] 9 | indeg = [0] * (n + 1) 10 | 11 | for i in range(1, n + 1): 12 | line = list(map(int, si().split())) 13 | T[i] = line[0] 14 | for j in range(1, len(line) - 1): 15 | y = line[j] 16 | con[y].append(i) 17 | # indegree 계산하기 18 | indeg[i] += 1 19 | 20 | # 제일 앞에 "정렬될 수 있는" 정점 찾기 21 | Q = deque() 22 | for i in range(1, n + 1): 23 | if indeg[i] == 0: 24 | Q.append(i) 25 | T_done[i] = T[i] 26 | 27 | # 위상 정렬 순서대로 T_done 계산을 함께 해주기 28 | while Q: 29 | x = Q.popleft() 30 | for y in con[x]: 31 | indeg[y] -= 1 32 | if indeg[y] == 0: 33 | Q.append(y) 34 | T_done[y] = max(T_done[y], T_done[x] + T[y]) 35 | 36 | for i in range(1, n + 1): 37 | print(T_done[i]) 38 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2056-작업/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 10005 7 | 8 | int n, m, indeg[NM], T[NM], T_done[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n; 13 | for (int i = 1; i <= n; i++) { 14 | cin >> T[i]; 15 | int cnt; 16 | cin >> cnt; 17 | while (cnt--){ 18 | int y; 19 | cin >> y; 20 | con[y].push_back(i); 21 | indeg[i]++; 22 | } 23 | } 24 | } 25 | 26 | #include 27 | void pro() { 28 | queue Q; 29 | 30 | // 제일 앞에 "정렬될 수 있는" 정점 찾기 31 | for (int i = 1; i <= n; i++) 32 | if (indeg[i] == 0) { 33 | Q.push(i); 34 | T_done[i] = T[i]; 35 | } 36 | 37 | // 위상 정렬 순서대로 T_done 계산을 함께 해주기 38 | while (!Q.empty()) { 39 | int x = Q.front(); 40 | Q.pop(); 41 | for (int y : con[x]) { 42 | indeg[y]--; 43 | if (indeg[y] == 0) Q.push(y); 44 | T_done[y] = max(T_done[y], T_done[x] + T[y]); 45 | } 46 | } 47 | 48 | int ans = 0; 49 | for (int i = 1; i <= n; i++){ 50 | ans = max(ans, T_done[i]); 51 | } 52 | cout << ans; 53 | } 54 | 55 | int main() { 56 | ios::sync_with_stdio(false); 57 | cin.tie(0); 58 | input(); 59 | pro(); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2056-작업/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | T = [0] * (n + 1) 7 | T_done = [0] * (n + 1) 8 | con = [[] for _ in range(n + 1)] 9 | indeg = [0] * (n + 1) 10 | 11 | for i in range(1, n + 1): 12 | line = list(map(int, si().split())) 13 | T[i] = line[0] 14 | for j in range(2, len(line)): 15 | y = line[j] 16 | con[y].append(i) 17 | # indegree 계산하기 18 | indeg[i] += 1 19 | 20 | # 제일 앞에 "정렬될 수 있는" 정점 찾기 21 | Q = deque() 22 | for i in range(1, n + 1): 23 | if indeg[i] == 0: 24 | Q.append(i) 25 | T_done[i] = T[i] 26 | 27 | # 위상 정렬 순서대로 T_done 계산을 함께 해주기 28 | while Q: 29 | x = Q.popleft() 30 | for y in con[x]: 31 | indeg[y] -= 1 32 | if indeg[y] == 0: 33 | Q.append(y) 34 | T_done[y] = max(T_done[y], T_done[x] + T[y]) 35 | 36 | print(max(T_done)) 37 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2252-줄 세우기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 32005 7 | 8 | int n, m, indeg[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n >> m; 13 | for (int i = 1; i <= m; i++) { 14 | int x, y; 15 | cin >> x >> y; 16 | con[x].push_back(y); 17 | indeg[y]++; 18 | } 19 | } 20 | 21 | #include 22 | void pro() { 23 | queue Q; 24 | 25 | // 제일 앞에 "정렬될 수 있는" 정점 찾기 26 | for (int i = 1; i <= n; i++) if (indeg[i] == 0) Q.push(i); 27 | 28 | // 정렬될 수 있는 정점이 있다면? 29 | // 1. 정렬 결과에 추가하기 30 | // 2. 정점과 연결된 간선 제거하기 31 | // 3. 새롭게 "정렬될 수 있는" 정점 Queue에 추가하기 32 | while (!Q.empty()) { 33 | int x = Q.front(); 34 | Q.pop(); 35 | cout << x << " "; 36 | for (int y : con[x]) { 37 | indeg[y]--; 38 | if (indeg[y] == 0) Q.push(y); 39 | } 40 | } 41 | } 42 | 43 | int main() { 44 | ios::sync_with_stdio(false); 45 | cin.tie(0); 46 | input(); 47 | pro(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2252-줄 세우기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n, m = map(int, si().split()) 6 | con = [[] for _ in range(n + 1)] 7 | indeg = [0] * (n + 1) 8 | 9 | for _ in range(m): 10 | x, y = map(int, si().split()) 11 | con[x].append(y) 12 | indeg[y] += 1 13 | 14 | # 제일 앞에 "정렬될 수 있는" 정점 찾기 15 | Q = deque() 16 | for i in range(1, n + 1): 17 | if indeg[i] == 0: 18 | Q.append(i) 19 | 20 | # 정렬될 수 있는 정점이 있다면? 21 | # 1. 정렬 결과에 추가하기 22 | # 2. 정점과 연결된 간선 제거하기 23 | # 3. 새롭게 "정렬 될 수 있는" 정점 Queue에 추가하기 24 | while Q: 25 | x = Q.popleft() 26 | print(x, end=' ') 27 | for y in con[x]: 28 | indeg[y] -= 1 29 | if indeg[y] == 0: 30 | Q.append(y) 31 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2623-음악 프로그램/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int n, m, indeg[NM]; 9 | vector con[NM]; 10 | 11 | void input() { 12 | cin >> n >> m; 13 | for (int i = 1; i <= m; i++) { 14 | int cnt, x, y; 15 | cin >> cnt; 16 | cin >> x; 17 | // A1, A2, ... Acnt 순서로 나가야 한다면, 18 | // A1 -> A2 19 | // A2 -> A3 20 | // ... 21 | // A(cnt-1) -> Acnt 22 | // 의 간선을 만들어주면 된다. 23 | for (int j = 2; j <= cnt; j++) { 24 | cin >> y; 25 | con[x].push_back(y); 26 | indeg[y]++; 27 | x = y; 28 | } 29 | } 30 | } 31 | 32 | #include 33 | void pro() { 34 | queue Q; 35 | 36 | for (int i = 1; i <= n; i++) 37 | if (indeg[i] == 0) { 38 | Q.push(i); 39 | } 40 | 41 | vector ans; 42 | // 우선순위에 대한 조건을 간선으로 표현했으므로 위상정렬을 수행하면 된다. 43 | while (!Q.empty()) { 44 | int x = Q.front(); 45 | ans.push_back(x); 46 | Q.pop(); 47 | for (int y : con[x]) { 48 | indeg[y]--; 49 | if (indeg[y] == 0) Q.push(y); 50 | } 51 | } 52 | if (ans.size() != n) cout << 0; 53 | else for (int x : ans) cout << x << " "; 54 | } 55 | 56 | int main() { 57 | ios::sync_with_stdio(false); 58 | cin.tie(0); 59 | input(); 60 | pro(); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2623-음악 프로그램/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n, m = map(int, si().split()) 6 | con = [[] for _ in range(n + 1)] 7 | indeg = [0] * (n + 1) 8 | 9 | # A1, A2, ... Acnt 순서로 나가야 한다면, 10 | # A1 -> A2 11 | # A2 -> A3 12 | # ... 13 | # A(cnt-1) -> Acnt 14 | # 의 간선을 만들어주면 된다. 15 | 16 | for _ in range(m): 17 | order = list(map(int, si().split())) 18 | for i in range(2, len(order)): 19 | con[order[i - 1]].append(order[i]) 20 | indeg[order[i]] += 1 21 | 22 | 23 | # 우선순위에 대한 조건을 간선으로 표현했으므로 위상정렬을 수행하면 된다. 24 | Q = deque() 25 | for i in range(1, n + 1): 26 | if indeg[i] == 0: 27 | Q.append(i) 28 | 29 | ans = [] 30 | while Q: 31 | x = Q.popleft() 32 | ans.append(x) 33 | for y in con[x]: 34 | indeg[y] -= 1 35 | if indeg[y] == 0: 36 | Q.append(y) 37 | 38 | if len(ans) != n: 39 | print(0) 40 | else: 41 | for x in ans: print(x) 42 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2637-장난감 조립/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 105 7 | 8 | int n, m, indeg[NM]; 9 | vector > con[NM]; 10 | int cnt[NM][NM]; 11 | 12 | void input() { 13 | cin >> n >> m; 14 | for (int i = 1; i <= m; i++) { 15 | int x, y, v; 16 | cin >> y >> x >> v; 17 | con[x].push_back({y, v}); 18 | indeg[y]++; 19 | } 20 | } 21 | 22 | #include 23 | void pro() { 24 | queue Q; 25 | 26 | // 기본 부품 찾기 27 | for (int i = 1; i <= n; i++) 28 | if (indeg[i] == 0){ 29 | Q.push(i); 30 | cnt[i][i] = 1; 31 | } 32 | 33 | // 먼저 만들 수 있는 제품들부터 차례대로 만들어서 필요 기본 부품 개수 계산하기 34 | while (!Q.empty()) { 35 | int x = Q.front(); 36 | Q.pop(); 37 | for (auto p : con[x]) { 38 | auto [y, v] = p; 39 | indeg[y]--; 40 | 41 | // 제품 y에 제품 x 가 v 개 필요하므로 기본 부품 개수에 v 를 곱해서 누적시켜 준다. 42 | for (int i = 1; i <= n; i++) 43 | cnt[y][i] += cnt[x][i] * v; 44 | if (indeg[y] == 0) Q.push(y); 45 | } 46 | } 47 | for (int i = 1; i <= n; i++) 48 | if (cnt[n][i] != 0) 49 | cout << i << " " << cnt[n][i] << "\n"; 50 | } 51 | 52 | int main() { 53 | ios::sync_with_stdio(false); 54 | cin.tie(0); 55 | input(); 56 | pro(); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/2637-장난감 조립/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | m = int(si()) 7 | con = [[] for _ in range(n + 1)] 8 | indeg = [0] * (n + 1) 9 | cnt = [[0] * (n + 1) for _ in range(n + 1)] 10 | 11 | for _ in range(m): 12 | y, x, v = map(int, si().split()) 13 | con[x].append((y, v)) 14 | indeg[y] += 1 15 | 16 | # 기본 부품 찾기 17 | Q = deque() 18 | for i in range(1, n + 1): 19 | if indeg[i] == 0: 20 | Q.append(i) 21 | cnt[i][i] = 1 22 | 23 | # 먼저 만들 수 있는 제품들부터 차례대로 만들어서 필요 기본 부품 개수 계산하기 24 | while Q: 25 | x = Q.popleft() 26 | for y, v in con[x]: 27 | indeg[y] -= 1 28 | # 제품 y에 제품 x 가 v 개 필요하므로 기본 부품 개수에 v 를 곱해서 누적시켜 준다. 29 | for i in range(1, n + 1): 30 | cnt[y][i] += cnt[x][i] * v 31 | if indeg[y] == 0: 32 | Q.append(y) 33 | 34 | for i in range(n + 1): 35 | if cnt[n][i] != 0: 36 | print(i, cnt[n][i]) 37 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/13-위상정렬/문제별 코드/9470-Strahler 순서/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from collections import deque 3 | si = sys.stdin.readline 4 | 5 | T = int(si()) 6 | for _ in range(T): 7 | K, n, m = map(int, si().split()) 8 | con = [[] for _ in range(n + 1)] 9 | indeg = [0] * (n + 1) 10 | 11 | for _ in range(m): 12 | x, y = map(int, si().split()) 13 | con[x].append(y) 14 | indeg[y] += 1 15 | 16 | # 제일 앞에 "정렬될 수 있는" 정점 찾기 17 | order = [0] * (n + 1) 18 | maxCnt = [0] * (n + 1) 19 | Q = deque() 20 | for i in range(1, n + 1): 21 | if indeg[i] == 0: 22 | Q.append(i) 23 | order[i] = maxCnt[i] = 1 24 | 25 | ans = 0 26 | while Q: 27 | x = Q.popleft() 28 | if maxCnt[x] >= 2: 29 | order[x] += 1 30 | ans = max(ans, order[x]) 31 | for y in con[x]: 32 | indeg[y] -= 1 33 | if indeg[y] == 0: 34 | Q.append(y) 35 | if order[y] == order[x]: maxCnt[y] += 1 36 | elif order[y] < order[x]: 37 | order[y] = order[x] 38 | maxCnt[y] = 1 39 | print(K, ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/14-최단거리/02-알고리즘-14-다익스트라.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/14-최단거리/02-알고리즘-14-다익스트라.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/14-최단거리/README.md: -------------------------------------------------------------------------------- 1 | |문제 이름|문제 링크|답안 코드 링크| 2 | |---|---|---| 3 | |최소비용 구하기|[링크](http://boj.kr/1916)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/14-최단거리/문제별%20코드/1916-최소비용%20구하기)| 4 | |최단경로|[링크](http://boj.kr/1753)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/14-최단거리/문제별%20코드/1753-최단경로)| -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/14-최단거리/문제별 코드/1753-최단경로/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | si = sys.stdin.readline 4 | 5 | n, m = map(int, si().split()) 6 | start = int(si()) 7 | con = [[] for _ in range(n + 1)] 8 | for _ in range(m): 9 | u, v, weight = map(int, si().split()) 10 | con[u].append((v, weight)) 11 | 12 | # 모든 정점까지에 대한 거리를 무한대로 초기화 해주기. 13 | # ※주의사항※ 14 | # 문제의 정답으로 가능한 거리의 최댓값보다 큰 값임을 보장해야 한다. 15 | dist = [20001 * 10] * (n + 1) 16 | dist[start] = 0 17 | 18 | # 최소 힙 생성 19 | Q = [] 20 | heapq.heappush(Q, (0, start)) 21 | 22 | # 거리 정보들이 모두 소진될 때까지 거리 갱신을 반복한다. 23 | while Q: 24 | dist_x, x = heapq.heappop(Q) 25 | 26 | # 꺼낸 정보가 최신 정보랑 다르면, 의미없이 낡은 정보이므로 폐기한다. 27 | if dist[x] != dist_x: continue 28 | 29 | # 연결된 모든 간선들을 통해서 다른 정점들에 대한 정보를 갱신해준다. 30 | for u, weight in con[x]: 31 | # u 까지 갈 수 있는 더 짧은 거리를 찾았다면 이에 대한 정보를 갱신하고 PQ에 기록해준다. 32 | if dist[u] > dist[x] + weight: 33 | dist[u] = dist[x] + weight 34 | heapq.heappush(Q, (dist[u], u)) 35 | 36 | for i in range(1, n + 1): 37 | if dist[i] != 20001 * 10: 38 | print(dist[i]) 39 | else: 40 | print('INF') 41 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/14-최단거리/문제별 코드/1916-최소비용 구하기/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | m = int(si()) 7 | con = [[] for _ in range(n + 1)] 8 | for _ in range(m): 9 | u, v, weight = map(int, si().split()) 10 | con[u].append((v, weight)) 11 | start, destination = map(int, si().split()) 12 | 13 | # 모든 정점까지에 대한 거리를 무한대로 초기화 해주기. 14 | # ※주의사항※ 15 | # 문제의 정답으로 가능한 거리의 최댓값보다 큰 값임을 보장해야 한다. 16 | dist = [1005 * 100000] * (n + 1) 17 | dist[start] = 0 18 | 19 | # 최소 힙 생성 20 | Q = [] 21 | heapq.heappush(Q, (0, start)) 22 | 23 | # 거리 정보들이 모두 소진될 때까지 거리 갱신을 반복한다. 24 | while Q: 25 | dist_x, x = heapq.heappop(Q) 26 | 27 | # 꺼낸 정보가 최신 정보랑 다르면, 의미없이 낡은 정보이므로 폐기한다. 28 | if dist[x] != dist_x: continue 29 | 30 | # 연결된 모든 간선들을 통해서 다른 정점들에 대한 정보를 갱신해준다. 31 | for u, weight in con[x]: 32 | # u 까지 갈 수 있는 더 짧은 거리를 찾았다면 이에 대한 정보를 갱신하고 PQ에 기록해준다. 33 | if dist[u] > dist[x] + weight: 34 | dist[u] = dist[x] + weight 35 | heapq.heappush(Q, (dist[u], u)) 36 | 37 | print(dist[destination]) 38 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-15-동적프로그래밍.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-15-동적프로그래밍.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-16-동적프로그래밍.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-16-동적프로그래밍.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-17-동적프로그래밍.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/02-알고리즘/15~17-동적 프로그래밍/02-알고리즘-17-동적프로그래밍.pdf -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1003-피보나치 함수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 45 7 | 8 | int Q; 9 | long long int dy[NM][2]; 10 | 11 | void input() { 12 | cin >> Q; 13 | } 14 | 15 | // dy[][] 를 미리 계산해 놓기 16 | void preprocess() { 17 | // dy[i][k] := fibonacci(i) 를 호출했을 때, k 가 출력되는 횟수 18 | dy[0][0] = 1; 19 | dy[1][1] = 1; 20 | for (int i = 2; i <= 40; i++) { 21 | dy[i][0] = dy[i - 1][0] + dy[i - 2][0]; 22 | dy[i][1] = dy[i - 1][1] + dy[i - 2][1]; 23 | } 24 | } 25 | 26 | void pro() { 27 | while (Q--) { 28 | int x; cin >> x; 29 | cout << dy[x][0] << " " << dy[x][1] << "\n"; 30 | } 31 | } 32 | 33 | int main() { 34 | ios::sync_with_stdio(false); 35 | cin.tie(0); 36 | input(); 37 | preprocess(); 38 | pro(); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1003-피보나치 함수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | dy = [[0, 0] for _ in range(50)] 7 | 8 | # dy[][] 를 미리 계산해 놓기 9 | def preprocess(): 10 | # dy[i][k] := fibonacci(i) 를 호출했을 때, k 가 출력되는 횟수 11 | dy[0][0], dy[1][1] = 1, 1 12 | for i in range(2, 41): 13 | dy[i][0] = dy[i - 1][0] + dy[i - 2][0] 14 | dy[i][1] = dy[i - 1][1] + dy[i - 2][1] 15 | 16 | preprocess() 17 | 18 | for _ in range(Q): 19 | x = int(si()) 20 | print(dy[x][0], dy[x][1]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/10870-피보나치 수 5/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 25 7 | 8 | int Q; 9 | int dy[NM]; 10 | 11 | void input() { 12 | cin >> Q; 13 | } 14 | 15 | void pro() { 16 | dy[0] = 0; 17 | dy[1] = 1; 18 | for (int i = 2; i <= Q; i++) 19 | dy[i] = dy[i - 1] + dy[i - 2]; 20 | cout << dy[Q]; 21 | } 22 | 23 | int main() { 24 | ios::sync_with_stdio(false); 25 | cin.tie(0); 26 | input(); 27 | pro(); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/10870-피보나치 수 5/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | dy = [0] * (Q + 2) 7 | 8 | dy[0], dy[1] = 0, 1 9 | 10 | for i in range(2, Q + 1): 11 | dy[i] = dy[i - 1] + dy[i - 2] 12 | 13 | print(dy[Q]) 14 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11052-카드 구매하기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int N; 9 | int A[NM], Dy[NM]; 10 | 11 | void input(){ 12 | cin >> N; 13 | for (int i = 1; i <= N; i++) cin >> A[i]; 14 | } 15 | 16 | void pro() { 17 | // 초기값 구하기 18 | Dy[0] = 0; 19 | 20 | // 점화식을 토대로 Dy 배열 채우기 21 | for (int i = 1; i <= N; i++) { 22 | for (int cnt = 1; cnt <= i; cnt++) { 23 | Dy[i] = max(Dy[i], Dy[i - cnt] + A[cnt]); 24 | } 25 | } 26 | 27 | // Dy배열로 정답 계산하기 28 | cout << Dy[N]; 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(false); 33 | cin.tie(0); 34 | input(); 35 | pro(); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11052-카드 구매하기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | a = [0] + list(map(int, si().split())) 6 | dy = [0] * (n + 1) 7 | 8 | # 초기값 구하기 9 | pass 10 | 11 | # 점화식을 토대로 dy 배열 채우기 12 | for i in range(1, n + 1): 13 | for cnt in range(1, i + 1): 14 | dy[i] = max(dy[i], dy[i - cnt] + a[cnt]) 15 | 16 | print(dy[n]) 17 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11057-오르막 수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int N; 9 | int A[NM], Dy[NM][10]; 10 | 11 | void input(){ 12 | cin >> N; 13 | } 14 | 15 | void pro() { 16 | // 초기값 구하기 17 | for (int num = 0; num <= 9; num++) { 18 | Dy[1][num] = 1; 19 | } 20 | 21 | // 점화식을 토대로 Dy 배열 채우기 22 | for (int len = 2; len <= N; len++) { 23 | for (int num = 0; num <= 9; num++) { 24 | // 길이가 len이고 num으로 끝나는 개수를 계산하자 == Dy[len][num] 을 계산하자. 25 | for (int prev = 0; prev <= num; prev++) { 26 | Dy[len][num] += Dy[len - 1][prev]; 27 | Dy[len][num] %= 10007; 28 | } 29 | } 30 | } 31 | 32 | // Dy배열로 정답 계산하기 33 | int ans = 0; 34 | for (int num = 0; num <= 9; num++) { 35 | ans += Dy[N][num]; 36 | ans %= 10007; 37 | } 38 | 39 | cout << ans; 40 | } 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); 44 | cin.tie(0); 45 | input(); 46 | pro(); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11057-오르막 수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | dy = [[0] * 10 for _ in range(n + 1)] 6 | 7 | # 초기값 구하기 8 | for num in range(10): 9 | dy[1][num] = 1 10 | 11 | # 점화식을 토대로 dy 배열 채우기 12 | for len in range(2, n + 1): 13 | for num in range(10): 14 | for prev in range(num + 1): 15 | dy[len][num] += dy[len - 1][prev] 16 | dy[len][num] %= 10007 17 | 18 | print(sum(dy[n]) % 10007) 19 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11066-파일 합치기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 505 8 | 9 | int n, a[NM], sum[NM][NM], dy[NM][NM]; 10 | void input() { 11 | cin >> n; 12 | for (int i = 1; i <= n; i++) 13 | cin >> a[i]; 14 | } 15 | void preprocess() { 16 | for (int i = 1; i <= n; i++) 17 | for (int j = i; j <= n; j++) 18 | sum[i][j] = sum[i][j - 1] + a[j]; 19 | } 20 | void pro() { 21 | for (int len = 2; len <= n; len++) { 22 | for (int i = 1; i <= n - len + 1; i++) { 23 | int j = i + len - 1; 24 | dy[i][j] = 0x7fffffff; 25 | for (int k = i; k < j; k++) 26 | dy[i][j] = min(dy[i][j], dy[i][k] + dy[k + 1][j] + sum[i][j]); 27 | } 28 | } 29 | cout << dy[1][n] << "\n"; 30 | } 31 | 32 | int main() { 33 | ios::sync_with_stdio(false); cin.tie(0); 34 | int TT; 35 | cin >> TT; 36 | while (TT--) { 37 | input(); 38 | preprocess(); 39 | pro(); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11066-파일 합치기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | def solve(): 5 | n = int(si()) 6 | a = [0] + list(map(int, si().split())) 7 | 8 | sum = [[0] * (n + 1) for _ in range(n + 1)] 9 | dy = [[0] * (n + 1) for _ in range(n + 1)] 10 | 11 | # Preprocess 12 | for i in range(n + 1): 13 | for j in range(i, n + 1): 14 | sum[i][j] = sum[i][j - 1] + a[j] 15 | 16 | # 점화식을 토대로 dy 배열 채우기 17 | for len in range(2, n + 1): 18 | for i in range(1, n - len + 2): 19 | j = i + len - 1 20 | dy[i][j] = 500 * 500 * 10000 21 | for k in range(i, j): 22 | dy[i][j] = min(dy[i][j], dy[i][k] + dy[k + 1][j] + sum[i][j]) 23 | 24 | print(dy[1][n]) 25 | 26 | T = int(si()) 27 | for _ in range(T): 28 | solve() 29 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1149-RGB 거리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int N; 9 | int A[3], Dy[NM][3]; 10 | 11 | void input(){ 12 | cin >> N; 13 | } 14 | 15 | void pro() { 16 | // Dy[i][j] := 1 ~ i번 집까지 칠했고, i번 집을 j번 색으로 색칠하는 경우 중 최소 비용 17 | 18 | // 초기값 구하기 19 | Dy[0][0] = Dy[0][1] = Dy[0][2] = 0; 20 | 21 | // 점화식을 토대로 Dy 배열 채우기 22 | for (int i = 1; i <= N; i++) { 23 | for (int j = 0; j < 3; j++) cin >> A[j]; 24 | Dy[i][0] = min(Dy[i - 1][1], Dy[i - 1][2]) + A[0]; 25 | Dy[i][1] = min(Dy[i - 1][0], Dy[i - 1][2]) + A[1]; 26 | Dy[i][2] = min(Dy[i - 1][0], Dy[i - 1][1]) + A[2]; 27 | } 28 | 29 | // Dy배열로 정답 계산하기 30 | cout << min(Dy[N][0], min(Dy[N][1], Dy[N][2])); 31 | } 32 | 33 | int main() { 34 | ios::sync_with_stdio(false); 35 | cin.tie(0); 36 | input(); 37 | pro(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1149-RGB 거리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | dy = [[0] * 3 for _ in range(n + 1)] 6 | 7 | # 초기값 구하기 8 | pass 9 | 10 | # dy[i][j] := 1 ~ i번 집까지 칠했고, i번 집을 j번 색으로 색칠하는 경우 중 최소 비용 11 | # 점화식을 토대로 dy 배열 채우기 12 | for i in range(1, n + 1): 13 | a = list(map(int, si().split())) 14 | dy[i][0] = min(dy[i - 1][1], dy[i - 1][2]) + a[0] 15 | dy[i][1] = min(dy[i - 1][0], dy[i - 1][2]) + a[1] 16 | dy[i][2] = min(dy[i - 1][0], dy[i - 1][1]) + a[2] 17 | 18 | print(min(dy[n])) 19 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11726-2xN 타일링/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int n; 9 | int Dy[NM]; 10 | 11 | void input(){ 12 | cin >> n; 13 | } 14 | 15 | void pro() { 16 | Dy[1] = 1; 17 | Dy[2] = 2; 18 | 19 | // 점화식을 토대로 Dy 배열 채우기 20 | for (int i = 3; i <= n; i++){ 21 | Dy[i] = (Dy[i - 1] + Dy[i - 2]) % 10007; 22 | } 23 | 24 | cout << Dy[n]; 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(false); 29 | cin.tie(0); 30 | input(); 31 | pro(); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/11726-2xN 타일링/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | 7 | dy = [0] * (n + 2) 8 | # 초기값 채우기 9 | dy[1], dy[2] = 1, 2 10 | 11 | # 점화식을 토대로 dy 배열 채우기 12 | for i in range(3, n + 1): 13 | dy[i] = (dy[i - 1] + dy[i - 2]) % 10007 14 | 15 | print(dy[n]) 16 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1309-동물원/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | #define MOD 9901 8 | 9 | int N; 10 | int A[NM][2], Dy[NM][3]; 11 | 12 | void input() { 13 | cin >> N; 14 | } 15 | 16 | void pro() { 17 | // 초기값 구하기 18 | Dy[1][0] = 1; 19 | Dy[1][1] = 1; 20 | Dy[1][2] = 1; 21 | 22 | // 점화식을 토대로 Dy 배열 채우기 23 | for (int i = 2; i <= N; i++) { 24 | for (int prev = 0; prev <= 2; prev++) { 25 | Dy[i][0] += Dy[i - 1][prev]; 26 | Dy[i][0] %= MOD; 27 | for (int j = 0; j <= 1; j++) { 28 | if (prev & (1 << j)) continue; 29 | Dy[i][1 << j] += Dy[i - 1][prev]; 30 | Dy[i][1 << j] %= MOD; 31 | } 32 | } 33 | } 34 | 35 | cout << (Dy[N][0] + Dy[N][1] + Dy[N][2]) % MOD; 36 | } 37 | 38 | int main() { 39 | ios::sync_with_stdio(false); 40 | cin.tie(0); 41 | input(); 42 | pro(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1309-동물원/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | MOD = 9901 4 | 5 | n = int(si()) 6 | 7 | dy = [[0, 0, 0] for _ in range(n)] 8 | 9 | # 초기값 채우기 10 | dy[0][0], dy[0][1], dy[0][2] = 1, 1, 1 11 | 12 | # 점화식을 토대로 dy 배열 채우기 13 | for i in range(1, n): 14 | for prev in range(0, 3): 15 | dy[i][0] += dy[i - 1][prev] 16 | dy[i][0] %= MOD 17 | for j in range(2): 18 | if prev & (1 << j): continue 19 | dy[i][1 << j] += dy[i - 1][prev] 20 | dy[i][1 << j] %= MOD 21 | 22 | print(sum(dy[n - 1]) % MOD) 23 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1495-기타리스트/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 55 7 | 8 | int N, S, M; 9 | int A[NM]; 10 | bool Dy[NM][1001]; 11 | 12 | void input() { 13 | cin >> N >> S >> M; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | // 초기값 구하기 19 | Dy[0][S] = true; 20 | 21 | // 점화식을 토대로 Dy 배열 채우기 22 | int ans = 0; 23 | for (int i = 1; i <= N; i++) { 24 | bool flag = false; 25 | ans = 0; 26 | for (int prev = 0; prev <= M; prev++) { 27 | if (!Dy[i - 1][prev]) continue; 28 | for (int cur : {prev - A[i], prev + A[i]}) { 29 | if (cur < 0 || cur > M) continue; 30 | ans = max(ans, cur); 31 | Dy[i][cur] = true; 32 | flag = true; 33 | } 34 | } 35 | if (!flag) { 36 | cout << -1; 37 | return; 38 | } 39 | } 40 | 41 | // Dy배열로 정답 계산하기 42 | cout << ans; 43 | } 44 | 45 | int main() { 46 | ios::sync_with_stdio(false); 47 | cin.tie(0); 48 | input(); 49 | pro(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1495-기타리스트/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n, S, M = list(map(int, si().split())) 5 | a = [0] + list(map(int, si().split())) 6 | dy = [[False for __ in range(M + 1)] for _ in range(n + 1)] 7 | 8 | # 초기값 구하기 9 | dy[0][S] = True 10 | 11 | # 점화식을 토대로 dy 배열 채우기 12 | for i in range(1, n + 1): 13 | ans = -1 14 | for prev in range(M + 1): 15 | if not dy[i - 1][prev]: continue 16 | for cur in [prev - a[i], prev + a[i]]: 17 | if cur < 0 or cur > M: continue 18 | ans = max(ans, cur) 19 | dy[i][cur] = True 20 | 21 | print(ans) 22 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1562-계단 수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 105 7 | #define MOD 1000000000 8 | 9 | int N; 10 | int Dy[NM][10][10][10]; 11 | 12 | void input(){ 13 | cin >> N; 14 | } 15 | 16 | void pro() { 17 | // 초기값 구하기 18 | for (int num = 1; num <= 9; num++) { 19 | Dy[1][num][num][num] = 1; 20 | } 21 | 22 | // 점화식을 토대로 Dy 배열 채우기 23 | for (int len = 2; len <= N; len++) { 24 | for (int prev = 0; prev <= 9; prev++) { 25 | for (int low = 0; low <= 9; low++) { 26 | for (int high = 0; high <= 9; high++) { 27 | for (int cur: {prev - 1, prev + 1}) { 28 | if (cur < 0 || cur > 9) continue; 29 | Dy[len][cur][min(low, cur)][max(high, cur)] += Dy[len - 1][prev][low][high]; 30 | Dy[len][cur][min(low, cur)][max(high, cur)] %= MOD; 31 | } 32 | } 33 | } 34 | } 35 | } 36 | 37 | // Dy배열로 정답 계산하기 38 | int ans = 0; 39 | for (int num = 0; num <= 9; num++) { 40 | ans += Dy[N][num][0][9]; 41 | ans %= MOD; 42 | } 43 | 44 | cout << ans; 45 | } 46 | 47 | int main() { 48 | ios::sync_with_stdio(false); 49 | cin.tie(0); 50 | input(); 51 | pro(); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1562-계단 수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | dy = [[[[0 for _4 in range(10)] for _3 in range(10)] for _2 in range(10)] for _1 in range(n + 1)] 6 | MOD = 1000000000 7 | 8 | # 초기값 구하기 9 | for num in range(1, 10): 10 | dy[1][num][num][num] = 1 11 | 12 | # 점화식을 토대로 dy 배열 채우기 13 | for len in range(2, n + 1): 14 | for prev in range(10): 15 | for low in range(10): 16 | for high in range(10): 17 | for cur in [prev - 1, prev + 1]: 18 | if cur < 0 or cur > 9: continue 19 | dy[len][cur][min(low, cur)][max(high, cur)] += dy[len - 1][prev][low][high] 20 | dy[len][cur][min(low, cur)][max(high, cur)] %= MOD 21 | 22 | ans = 0 23 | for i in range(10): 24 | ans += dy[n][i][0][9] 25 | ans %= MOD 26 | 27 | print(ans) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15681-트리와 쿼리/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int n, R, Q; 9 | int Dy[NM]; 10 | vector con[NM]; 11 | 12 | void input() { 13 | cin >> n >> R >> Q; 14 | for (int i = 1; i < n; i++) { 15 | int x, y; 16 | cin >> x >> y; 17 | con[x].push_back(y); 18 | con[y].push_back(x); 19 | } 20 | } 21 | 22 | // Dy[x] 를 계산하는 함수 23 | void DFS(int x, int prev) { 24 | Dy[x] = 1; 25 | for (int y : con[x]) { 26 | if (y == prev) continue; 27 | DFS(y, x); 28 | Dy[x] += Dy[y]; 29 | } 30 | } 31 | void pro() { 32 | DFS(R, -1); 33 | while (Q--) { 34 | int x; cin >> x; 35 | cout << Dy[x] << '\n'; 36 | } 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15681-트리와 쿼리/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | n, R, Q = map(int, si().split()) 6 | con = [[] for _ in range(n + 1)] 7 | for _ in range(n - 1): 8 | x, y = map(int, si().split()) 9 | con[x].append(y) 10 | con[y].append(x) 11 | 12 | Dy = [0] * (n + 1) 13 | # Dy[x] 를 계산하는 함수 14 | def DFS(x, prev): 15 | global Dy 16 | Dy[x] = 1 17 | for y in con[x]: 18 | if y == prev: continue 19 | DFS(y, x) 20 | Dy[x] += Dy[y] 21 | 22 | DFS(R, -1) 23 | 24 | for _ in range(Q): 25 | print(Dy[int(si())]) 26 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15988-1, 2, 3 더하기 3/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1000005 7 | 8 | int Q, MOD = 1000000009; 9 | int dy[NM]; 10 | 11 | void input() { 12 | cin >> Q; 13 | } 14 | 15 | void preprocess() { 16 | // dy[i] := i 를 1, 2, 3의 합으로 표현하는 방법 17 | // dy[0] 은 사실 문제 정의 상 말이 안되지만, 이후 점화식을 간단하게 적용하기 위해서 18 | // "아무것도 안 더한다"는 것을 하나의 경우로 생각해줘서 1을 적어준다. 19 | // 이를 통해서 dy[1, 2, 3]에 대해서도 일관성있는 점화식을 작성할 수 있기 때문이다. 20 | // 이게 불편하다면 dy[1, 2, 3] 에 대해 모두 값을 적어주고 시작해도 상관없다. 21 | dy[0] = 1; 22 | for (int i = 1; i <= 1000000; i++) { 23 | dy[i] = dy[i - 1]; // 마지막에 1을 더하는 경우의 수 24 | if (i - 2 >= 0) dy[i] += dy[i - 2]; // 마지막에 2을 더하는 경우의 수 25 | dy[i] %= MOD; 26 | if (i - 3 >= 0) dy[i] += dy[i - 3]; // 마지막에 3을 더하는 경우의 수 27 | dy[i] %= MOD; 28 | } 29 | } 30 | 31 | void pro() { 32 | preprocess(); 33 | while (Q-- > 0) { 34 | int x; 35 | cin >> x; 36 | cout << dy[x] << '\n'; 37 | } 38 | } 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15988-1, 2, 3 더하기 3/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | dy = [0] * (1000005) 7 | 8 | # dy[i] := i 를 1, 2, 3의 합으로 표현하는 방법 9 | # dy[0] 은 사실 문제 정의 상 말이 안되지만, 이후 점화식을 간단하게 적용하기 위해서 10 | # "아무것도 안 더한다"는 것을 하나의 경우로 생각해줘서 1을 적어준다. 11 | # 이를 통해서 dy[1, 2, 3]에 대해서도 일관성있는 점화식을 작성할 수 있기 때문이다. 12 | # 이게 불편하다면 dy[1, 2, 3] 에 대해 모두 값을 적어주고 시작해도 상관없다. 13 | dy[0] = 1 14 | for i in range(1, 1000001): 15 | dy[i] = dy[i - 1] # 마지막에 1을 더하는 경우의 수 16 | if i >= 2: dy[i] += dy[i - 2] # 마지막에 1을 더하는 경우의 수 17 | if i >= 3: dy[i] += dy[i - 3] # 마지막에 1을 더하는 경우의 수 18 | dy[i] %= 1000000009 19 | 20 | for _ in range(Q): 21 | print(dy[int(si())]) 22 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15990-1, 2, 3 더하기 5/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | #define MOD 1000000009 8 | 9 | int Dy[NM][4]; 10 | 11 | void preprocess() { 12 | // 초기값 구하기 13 | Dy[1][1] = 1; 14 | Dy[2][2] = 1; 15 | Dy[3][1] = Dy[3][2] = Dy[3][3] = 1; 16 | 17 | // 점화식을 토대로 Dy 배열 채우기 18 | for (int i = 4; i <= 100000; i++) { 19 | for (int cur = 1; cur <= 3; cur++) { 20 | for (int prev = 1; prev <= 3; prev++) { 21 | if (cur == prev) continue; 22 | Dy[i][cur] += Dy[i - cur][prev]; 23 | Dy[i][cur] %= MOD; 24 | } 25 | } 26 | } 27 | } 28 | 29 | void pro() { 30 | int T; cin >> T; 31 | while (T--) { 32 | int n; cin >> n; 33 | int ans = 0; 34 | for (int cur = 1; cur <= 3; cur++) { 35 | ans += Dy[n][cur]; 36 | ans %= MOD; 37 | } 38 | cout << ans << "\n"; 39 | } 40 | } 41 | 42 | int main() { 43 | ios::sync_with_stdio(false); 44 | cin.tie(0); 45 | preprocess(); 46 | pro(); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15990-1, 2, 3 더하기 5/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | si = sys.stdin.readline 4 | MOD = 1000000009 5 | 6 | dy = [[0, 0, 0, 0] for _ in range(100005)] 7 | # 초기값 채우기 8 | dy[1][1] = 1 9 | dy[2][2] = 1 10 | dy[3][1] = 1 11 | dy[3][2] = 1 12 | dy[3][3] = 1 13 | 14 | # 점화식을 토대로 dy 배열 채우기 15 | for i in range(4, 100001): 16 | for cur in range(1, 4): 17 | for prev in range(1, 4): 18 | if cur == prev: continue 19 | dy[i][cur] += dy[i - cur][prev] 20 | dy[i][cur] %= MOD 21 | 22 | T = int(si()) 23 | for _ in range(T): 24 | print(sum(dy[int(si())]) % MOD) 25 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/15991-1, 2, 3 더하기 6/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | dy = [0] * (100005) 7 | 8 | # dy[i] := i 를 1, 2, 3의 합으로 표현하는 방법 9 | # dy[0] 은 사실 문제 정의 상 말이 안되지만, 이후 점화식을 간단하게 적용하기 위해서 10 | # "아무것도 안 더한다"는 것을 하나의 경우로 생각해줘서 1을 적어준다. 11 | # 이를 통해서 dy[1, 2, 3]에 대해서도 일관성있는 점화식을 작성할 수 있기 때문이다. 12 | # 이게 불편하다면 dy[1, 2, 3] 에 대해 모두 값을 적어주고 시작해도 상관없다. 13 | dy[0] = 1 14 | for i in range(1, 100001): 15 | dy[i] = dy[i - 1] # 마지막에 1을 더하는 경우의 수 16 | if i >= 2: dy[i] += dy[i - 2] # 마지막에 1을 더하는 경우의 수 17 | if i >= 3: dy[i] += dy[i - 3] # 마지막에 1을 더하는 경우의 수 18 | dy[i] %= 1000000009 19 | 20 | for _ in range(Q): 21 | x = int(si()) 22 | res = 0 23 | # 홀수 개를 더한 경우 24 | for mid in range(1, 4): # 가운데에 mid 를 더한 상황 25 | if x - mid >= 0 and (x - mid) % 2 == 0: # 양쪽이 대칭을 이루게 더하기 위함 26 | res += dy[(x - mid) // 2] 27 | 28 | # 짝수 개를 더한 경우 (사실상 위에서 mid 가 0 인 경우. 즉, for loop을 0 부터 시작해도 됨) 29 | if x % 2 == 0: 30 | res += dy[x // 2] 31 | 32 | res %= 1000000009 33 | 34 | print(res) 35 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1949-우수 마을/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long int ll; 5 | using namespace std; 6 | 7 | #define NM 100005 8 | 9 | #include 10 | int n, a[NM], dy[NM][2]; 11 | vector con[NM]; 12 | 13 | void input() { 14 | cin >> n; 15 | for (int i = 1; i <= n; i++) cin >> a[i]; 16 | for (int i = 1; i < n; i++) { 17 | int x, y; cin >> x >> y; 18 | con[x].push_back(y); 19 | con[y].push_back(x); 20 | } 21 | } 22 | void dfs(int x, int par) { 23 | dy[x][1] = a[x]; 24 | for (int y : con[x]) { 25 | if (y == par) continue; 26 | dfs(y, x); 27 | dy[x][0] += max(dy[y][0], dy[y][1]); 28 | dy[x][1] += dy[y][0]; 29 | } 30 | } 31 | 32 | int main() { 33 | ios::sync_with_stdio(false); cin.tie(0); 34 | input(); 35 | dfs(1, -1); 36 | cout << max(dy[1][0], dy[1][1]); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/1949-우수 마을/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(10005) 3 | si = sys.stdin.readline 4 | 5 | n = int(si()) 6 | a = [0] + list(map(int, si().split())) 7 | con = [[] for _ in range(n + 1)] 8 | for _ in range(n - 1): 9 | x, y = list(map(int, si().split())) 10 | con[x].append(y) 11 | con[y].append(x) 12 | 13 | dy = [[0, 0] for _ in range(n + 1)] 14 | def dfs(x, par): 15 | dy[x][1] = a[x] 16 | for y in con[x]: 17 | if y == par: continue 18 | dfs(y, x) 19 | dy[x][0] += max(dy[y]) 20 | dy[x][1] += dy[y][0] 21 | 22 | dfs(1, -1) 23 | print(max(dy[1])) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2011-암호코드/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 5005 7 | 8 | #include 9 | string str; 10 | int N, MOD = 1000000, Dy[NM]; 11 | 12 | void input(){ 13 | cin >> str; 14 | N = str.size(); 15 | } 16 | 17 | bool check(char A, char B) { // 'AB' 라는 두 자리 숫자가 하나의 수로 해독이 가능한가? 18 | if (A == '0') return false; 19 | if (A == '1') return true; 20 | if (A >= '3') return false; 21 | return B <= '6'; 22 | } 23 | 24 | void pro() { 25 | 26 | // 초기값 구하기 27 | if (str[0] != '0') Dy[0] = 1; 28 | 29 | // 점화식을 토대로 Dy 배열 채우기 30 | for (int i = 1; i < N; i++) { 31 | // i 번 숫자를 단독으로 해석 가능할 때 32 | if (str[i] != '0') Dy[i] = Dy[i - 1]; 33 | 34 | // i - 1번과 i 번 숫자를 하나의 문자로 해석 가능할 때 35 | if (check(str[i - 1], str[i])) { 36 | if (i >= 2) Dy[i] += Dy[i - 2]; 37 | else Dy[i] += 1; 38 | Dy[i] %= MOD; 39 | } 40 | } 41 | 42 | // Dy배열로 정답 계산하기 43 | cout << Dy[N - 1]; 44 | } 45 | 46 | int main() { 47 | ios::sync_with_stdio(false); 48 | cin.tie(0); 49 | input(); 50 | pro(); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2011-암호코드/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | a = si().strip() 5 | n = len(a) 6 | 7 | def check(A: chr, B: chr): # 'AB' 라는 두 자리 숫자가 하나의 수로 해독이 가능한가? 8 | if A == '0': return False 9 | if A == '1': return True 10 | if A >= '3': return False 11 | return B <= '6' 12 | 13 | dy = [0] * n 14 | 15 | # 초기값 구하기 16 | if a[0] != '0': 17 | dy[0] = 1 18 | 19 | # 점화식을 토대로 Dy 배열 채우기 20 | for i in range(1, n): 21 | # i 번 숫자를 단독으로 해석 가능할 때 22 | if a[i] != '0': 23 | dy[i] = dy[i - 1] 24 | 25 | # i - 1번과 i 번 숫자를 하나의 문자로 해석 가능할 때 26 | if check(a[i - 1], a[i]): 27 | if i >= 2: dy[i] += dy[i - 2] 28 | else: dy[i] += 1 29 | dy[i] %= 1000000 30 | 31 | print(dy[n - 1]) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2096-내려가기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int N; 9 | int A[4], dy_min[2][4], dy_max[2][4]; 10 | 11 | void input() { 12 | cin >> N; 13 | } 14 | 15 | int Max(int x, int y) { return x > y ? x : y; } 16 | int Min(int x, int y) { return x < y ? x : y; } 17 | void pro() { 18 | // 초기값 구하기 19 | for (int j = 1; j <= 3; j++) cin >> dy_min[1][j], dy_max[1][j] = dy_min[1][j]; 20 | 21 | // 점화식을 통해 dy 배열 계산하기 22 | for (int i = 2; i <= N; i++) { 23 | for (int j = 1; j <= 3; j++) cin >> A[j]; 24 | for (int cur = 1; cur <= 3; cur++) { 25 | dy_max[i % 2][cur] = 0; 26 | dy_min[i % 2][cur] = 1e8; 27 | for (int prev = 1; prev <= 3; prev++) { 28 | if (abs(cur - prev) > 1) continue; 29 | dy_max[i % 2][cur] = max(dy_max[i % 2][cur], dy_max[(i + 1) % 2][prev] + A[cur]); 30 | dy_min[i % 2][cur] = min(dy_min[i % 2][cur], dy_min[(i + 1) % 2][prev] + A[cur]); 31 | } 32 | } 33 | } 34 | 35 | cout << max(max(dy_max[N % 2][1], dy_max[N % 2][2]), dy_max[N % 2][3]) << " "; 36 | cout << min(min(dy_min[N % 2][1], dy_min[N % 2][2]), dy_min[N % 2][3]) << " "; 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | pro(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2096-내려가기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | a= [0] * 4 6 | dy_min = [[10000000, 10000000, 10000000, 10000000] for _ in range(2)] 7 | dy_max = [[0, 0, 0, 0] for _ in range(2)] 8 | 9 | # 초기값 채우기 10 | a = [0] + list(map(int, si().split())) 11 | for j in range(1, 4): 12 | dy_min[1][j] = a[j] 13 | dy_max[1][j] = a[j] 14 | 15 | # 점화식을 토대로 dy 배열 채우기 16 | for i in range(2, n + 1): 17 | a = [0] + list(map(int, si().split())) 18 | for cur in range(1, 4): 19 | dy_min[i % 2][cur] = 10000000 20 | dy_max[i % 2][cur] = 0 21 | for prev in range(1, 4): 22 | if abs(cur - prev) > 1: 23 | continue 24 | dy_min[i % 2][cur] = min(dy_min[i % 2][cur], dy_min[(i + 1) % 2][prev] + a[cur]) 25 | dy_max[i % 2][cur] = max(dy_max[i % 2][cur], dy_max[(i + 1) % 2][prev] + a[cur]) 26 | 27 | print(max(dy_max[n % 2][1:]), end=' ') 28 | print(min(dy_min[n % 2][1:])) 29 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2156-포도주 시식/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 10005 7 | 8 | int N; 9 | int A[NM], Dy[NM][2]; 10 | 11 | void input() { 12 | cin >> N; 13 | for (int i = 1; i <= N; i++) cin >> A[i]; 14 | } 15 | 16 | void pro() { 17 | // 초기값 구하기 18 | Dy[1][0] = A[1]; 19 | Dy[1][1] = A[1]; 20 | 21 | if (N >= 2) { 22 | Dy[2][0] = A[2]; 23 | Dy[2][1] = A[1] + A[2]; 24 | } 25 | 26 | // 점화식을 토대로 Dy 배열 채우기 27 | for (int i = 3; i <= N; i++) { 28 | Dy[i][0] = max(Dy[i - 2][0] + A[i], Dy[i - 2][1] + A[i]); 29 | Dy[i][0] = max(Dy[i][0], max(Dy[i - 3][0], Dy[i - 3][1]) + A[i]); 30 | Dy[i][1] = Dy[i - 1][0] + A[i]; 31 | } 32 | 33 | cout << max({ Dy[N][0], Dy[N][1], Dy[N - 1][0], Dy[N - 1][1] }); 34 | } 35 | 36 | int main() { 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | input(); 40 | pro(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2156-포도주 시식/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | a= [0] * n 6 | for i in range(n): 7 | a[i] = int(si()) 8 | 9 | dy = [[0, 0] for _ in range(n + 2)] 10 | 11 | # 초기값 채우기 12 | dy[0][0], dy[0][1] = 0, a[0] 13 | 14 | if n >= 2: 15 | dy[1][0], dy[1][1] = a[1], a[0] + a[1] 16 | 17 | # 점화식을 토대로 dy 배열 채우기 18 | for i in range(2, n): 19 | dy[i][0] = max(dy[i - 2][0] + a[i], dy[i - 2][1] + a[i]) 20 | if i >= 3: 21 | dy[i][0] = max(dy[i][0], max(dy[i - 3][0] + a[i], dy[i - 3][1] + a[i])) 22 | dy[i][1] = dy[i - 1][0] + a[i] 23 | 24 | ans = max(dy[n - 1]) 25 | if n >= 2: 26 | ans = max(ans, max(dy[n - 2])) 27 | 28 | print(ans) 29 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2193-이친수/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 95 7 | 8 | int Q; 9 | long long int dy[NM][2]; 10 | 11 | void input() { 12 | cin >> Q; 13 | } 14 | 15 | void pro() { 16 | // dy[i][j] = i자리 이친수 중에서 마지막 숫자가 j 인 경우의 수 17 | 18 | dy[1][0] = 0; 19 | dy[1][1] = 1; 20 | 21 | for (int i = 2; i <= Q; i++) { 22 | dy[i][0] = dy[i - 1][0] + dy[i - 1][1]; 23 | dy[i][1] = dy[i - 1][0]; 24 | } 25 | cout << dy[Q][0] + dy[Q][1]; 26 | } 27 | 28 | int main() { 29 | ios::sync_with_stdio(false); 30 | cin.tie(0); 31 | input(); 32 | pro(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2193-이친수/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | si = sys.stdin.readline 4 | 5 | Q = int(si()) 6 | dy = [[0, 0] for _ in range(Q + 1)] 7 | 8 | # dy[i][k] := i자리 이친수 중에서 마지막 숫자가 j 인 경우의 수 9 | dy[1][1] = 1 10 | for i in range(2, Q + 1): 11 | dy[i][0] = dy[i - 1][0] + dy[i - 1][1] 12 | dy[i][1] = dy[i - 1][0] 13 | 14 | print(sum(dy[Q])) -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2579-계단 오르기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 1005 7 | 8 | int N; 9 | int A[NM], Dy[NM][2]; 10 | 11 | void input(){ 12 | cin >> N; 13 | for (int i = 1; i <= N; i++) cin >> A[i]; 14 | } 15 | 16 | void pro() { 17 | // 초기값 구하기 18 | Dy[1][0] = 0; 19 | Dy[1][1] = A[1]; 20 | 21 | if (N >= 2){ 22 | Dy[2][0] = A[2]; 23 | Dy[2][1] = A[1] + A[2]; 24 | } 25 | 26 | // 점화식을 토대로 Dy 배열 채우기 27 | for (int i = 3; i <= N; i++){ 28 | Dy[i][0] = max(Dy[i - 2][0] + A[i], Dy[i - 2][1] + A[i]); 29 | Dy[i][1] = Dy[i - 1][0] + A[i]; 30 | } 31 | 32 | cout << max(Dy[N][0], Dy[N][1]); 33 | } 34 | 35 | int main() { 36 | ios::sync_with_stdio(false); 37 | cin.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2579-계단 오르기/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | a= [0] * n 6 | for i in range(n): 7 | a[i] = int(si()) 8 | 9 | dy = [[0, 0] for _ in range(n + 2)] 10 | 11 | # 초기값 채우기 12 | dy[0][0], dy[0][1] = 0, a[0] 13 | 14 | if n >= 2: 15 | dy[1][0], dy[1][1] = a[1], a[0] + a[1] 16 | 17 | # 점화식을 토대로 dy 배열 채우기 18 | for i in range(2, n): 19 | dy[i][0] = max(dy[i - 2][0] + a[i], dy[i - 2][1] + a[i]) 20 | dy[i][1] = dy[i - 1][0] + a[i] 21 | 22 | print(max(dy[n - 1][0], dy[n - 1][1])) 23 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2688-줄어들지 않아/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int Q; 9 | long long int Dy[65][10]; 10 | 11 | void input() { 12 | cin >> Q; 13 | } 14 | 15 | void preprocess() { 16 | // 초기값 구하기 17 | for (int num = 0; num <= 9; num++) { 18 | Dy[1][num] = 1; 19 | } 20 | 21 | // 점화식을 토대로 Dy 배열 채우기 22 | for (int len = 2; len <= 64; len++) { 23 | for (int num = 0; num <= 9; num++) { 24 | // 길이가 len이고 num으로 끝나는 개수를 계산하자 == Dy[len][num] 을 계산하자. 25 | for (int prev = 0; prev <= num; prev++) { 26 | Dy[len][num] += Dy[len - 1][prev]; 27 | } 28 | } 29 | } 30 | } 31 | 32 | void pro() { 33 | for (int rep = 1; rep <= Q; rep++) { 34 | int N; cin >> N; 35 | long long int ans = 0; 36 | for (int num = 0; num <= 9; num++) ans += Dy[N][num]; 37 | cout << ans << "\n"; 38 | } 39 | } 40 | 41 | int main() { 42 | ios::sync_with_stdio(false); 43 | cin.tie(0); 44 | preprocess(); 45 | input(); 46 | pro(); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/2688-줄어들지 않아/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | Dy = [[0 for _ in range(10)] for _ in range(65)] 5 | 6 | for num in range(10): 7 | Dy[1][num] = 1 8 | 9 | for len in range(2, 65): 10 | for num in range(10): 11 | for prev in range(num + 1): 12 | Dy[len][num] += Dy[len - 1][prev] 13 | 14 | Q = int(si()) 15 | for _ in range(Q): 16 | n = int(si()) 17 | print(sum(Dy[n])) 18 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/5557-1학년/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 105 7 | 8 | int N; 9 | int A[NM]; 10 | long long int Dy[NM][21]; 11 | 12 | void input() { 13 | cin >> N; 14 | for (int i = 1; i <= N; i++) cin >> A[i]; 15 | } 16 | 17 | void pro() { 18 | // 초기값 구하기 19 | Dy[1][A[1]] = 1; 20 | 21 | // 점화식을 토대로 Dy 배열 채우기 22 | for (int i = 2; i <= N - 1; i++) { 23 | for (int prev = 0; prev <= 20; prev++) { 24 | for (int cur : {prev - A[i], prev + A[i]}) { 25 | if (cur < 0 || cur > 20) continue; 26 | Dy[i][cur] += Dy[i - 1][prev]; 27 | } 28 | } 29 | } 30 | 31 | // Dy배열로 정답 계산하기 32 | cout << Dy[N - 1][A[N]]; 33 | } 34 | 35 | int main() { 36 | ios::sync_with_stdio(false); 37 | cin.tie(0); 38 | input(); 39 | pro(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/5557-1학년/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | 4 | n = int(si()) 5 | a = list(map(int, si().split())) 6 | dy = [[0] * 21 for _ in range(n)] 7 | 8 | # 초기값 구하기 9 | dy[0][a[0]] = 1 10 | 11 | # 점화식을 토대로 dy 배열 채우기 12 | for i in range(1, n - 1): 13 | for prev in range(21): 14 | for cur in [prev - a[i], prev + a[i]]: 15 | if cur < 0 or cur > 20: continue 16 | dy[i][cur] += dy[i - 1][prev] 17 | 18 | print(dy[n - 2][a[n - 1]]) 19 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/9095-1, 2, 3 더하기/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 15 7 | 8 | int Dy[NM]; 9 | 10 | void preprocess() { 11 | // 초기값 구하기 12 | Dy[1] = 1; 13 | Dy[2] = 2; 14 | Dy[3] = 4; 15 | 16 | // 점화식을 토대로 Dy 배열 채우기 17 | for (int i = 4; i <= 11; i++) { 18 | Dy[i] = Dy[i - 1] + Dy[i - 2] + Dy[i - 3]; 19 | } 20 | } 21 | 22 | void pro() { 23 | int T; cin >> T; 24 | while (T--) { 25 | int n; cin >> n; 26 | cout << Dy[n] << "\n"; 27 | } 28 | } 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); 32 | cin.tie(0); 33 | preprocess(); 34 | pro(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/9095-1, 2, 3 더하기/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | si = sys.stdin.readline 4 | 5 | dy = [0] * 12 6 | # 초기값 채우기 7 | dy[1], dy[2], dy[3] = 1, 2, 4 8 | 9 | # 점화식을 토대로 dy 배열 채우기 10 | for i in range(4, 12): 11 | dy[i] = dy[i - 1] + dy[i - 2] + dy[i - 3] 12 | 13 | T = int(si()) 14 | for _ in range(T): 15 | print(dy[int(si())]) 16 | -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/9465-스티커/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #pragma warning(disable:4996) 5 | using namespace std; 6 | #define NM 100005 7 | 8 | int N; 9 | int A[NM][2], Dy[NM][3]; 10 | 11 | void input() { 12 | cin >> N; 13 | for (int j = 0; j <= 1; j++) 14 | for (int i = 1; i <= N; i++) 15 | cin >> A[i][j]; 16 | } 17 | 18 | void pro() { 19 | for (int i = 1; i <= N; i++) for (int j = 0; j < 3; j++) Dy[i][j] = 0; 20 | 21 | // 초기값 구하기 22 | Dy[1][1] = A[1][0]; 23 | Dy[1][2] = A[1][1]; 24 | 25 | // 점화식을 토대로 Dy 배열 채우기 26 | for (int i = 2; i <= N; i++) { 27 | for (int prev = 0; prev <= 2; prev++) { 28 | Dy[i][0] = max(Dy[i][0], Dy[i - 1][prev]); 29 | for (int j = 0; j <= 1; j++) { 30 | if (prev & (1 << j)) continue; 31 | Dy[i][1 << j] = max(Dy[i][1 << j], Dy[i - 1][prev] + A[i][j]); 32 | } 33 | } 34 | } 35 | 36 | cout << max({ Dy[N][0], Dy[N][1], Dy[N][2] }); 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | int TT; cin >> TT; 43 | while (TT--) { 44 | input(); 45 | pro(); 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /강의 자료/02-알고리즘/15~17-동적 프로그래밍/문제별 코드/9465-스티커/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | si = sys.stdin.readline 3 | TT = int(si()) 4 | for _TT in range(TT): 5 | n = int(si()) 6 | a= [[0, 0] for _ in range(n)] 7 | for j in range(2): 8 | b = list(map(int, si().split())) 9 | for i in range(n): 10 | a[i][j] = b[i] 11 | 12 | dy = [[0, 0, 0] for _ in range(n)] 13 | 14 | # 초기값 채우기 15 | dy[0][1], dy[0][2] = a[0][0], a[0][1] 16 | 17 | # 점화식을 토대로 dy 배열 채우기 18 | for i in range(1, n): 19 | for prev in range(0, 3): 20 | dy[i][0] = max(dy[i][0], dy[i - 1][prev]) 21 | for j in range(2): 22 | if prev & (1 << j): continue 23 | dy[i][1 << j] = max(dy[i][1 << j], dy[i - 1][prev] + a[i][j]) 24 | 25 | print(max(dy[n - 1])) 26 | -------------------------------------------------------------------------------- /강의 자료/03-모의 코딩테스트/03-코딩테스트-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/03-모의 코딩테스트/03-코딩테스트-1.pdf -------------------------------------------------------------------------------- /강의 자료/03-모의 코딩테스트/03-코딩테스트-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/03-모의 코딩테스트/03-코딩테스트-2.pdf -------------------------------------------------------------------------------- /강의 자료/03-모의 코딩테스트/03-코딩테스트-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rhs0266/FastCampus/ea5abca871c17b7eef7b50d9c236dd59ff6e631b/강의 자료/03-모의 코딩테스트/03-코딩테스트-3.pdf -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/README.md: -------------------------------------------------------------------------------- 1 | # 류호석배 알고리즘 코딩 테스트 2 | 3 | 최신 코딩 테스트의 경향을 파악하여 모의로 제공하는 대회입니다. 풀이 영상은 Fast Campus 강의에 추가될 예정입니다. 4 | 5 | 본 Github Repository는 C++, JAVA, Python으로 작성된 정답 코드를 대회별로 제공할 예정입니다. 6 | 7 | [백준 온라인 저지](https://www.acmicpc.net/)에서 진행합니다. 8 | 9 | 문제는 [대회 리스트](https://www.acmicpc.net/contest/official/list)에서 "류호석배"를 검색하시거나, 전체 문제에서 "호석"을 검색하시면 푸실 수 있습니다. -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/1번-홀수 홀릭 호석/solution.py: -------------------------------------------------------------------------------- 1 | # Input & Init 2 | n = int(input()) 3 | ans_min = 0x7fffffff 4 | ans_max = 0 5 | 6 | def get_odd_cnt(x: int): 7 | res = 0 8 | while (x > 0): 9 | res += x % 2 10 | x = x // 10 11 | return res 12 | 13 | def dfs(x: int, total_odd_cnt: int): 14 | if x <= 9: 15 | global ans_max, ans_min 16 | ans_min = min(ans_min, total_odd_cnt) 17 | ans_max = max(ans_max, total_odd_cnt) 18 | return 19 | 20 | if x <= 99: 21 | nx = x//10 + x%10 22 | dfs(nx, get_odd_cnt(nx) + total_odd_cnt) 23 | return 24 | 25 | x = str(x) 26 | for i in range(0, len(x) - 2): 27 | for j in range(i + 1, len(x) - 1): 28 | x1 = x[: i + 1] 29 | x2 = x[i + 1 : j + 1] 30 | x3 = x[j + 1 :] 31 | 32 | nx = int(x1) + int(x2) + int(x3) 33 | dfs(nx, get_odd_cnt(nx) + total_odd_cnt) 34 | 35 | dfs(n, get_odd_cnt(n)) 36 | print(ans_min, ans_max) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/2번-인내의 도미노 장인 호석/solution.py: -------------------------------------------------------------------------------- 1 | import queue 2 | 3 | # input & init 4 | n, m, r = map(int, input().split()) 5 | domino = [] 6 | check = [] 7 | ddir = {'E': (0, 1), 'W': (0, -1), 'S': (1, 0), 'N': (-1, 0)} 8 | for i in range(n): 9 | domino.append(list(map(int, input().split()))) 10 | check.append([True] * m) 11 | 12 | def attack(x, y, dir): 13 | global score 14 | if not check[x][y]: 15 | return 16 | dx, dy = ddir[dir] 17 | cnt = domino[x][y] 18 | while x >= 0 and y >= 0 and x < n and y < m and cnt >= 1: 19 | if check[x][y]: 20 | score += 1 21 | cnt = max(cnt - 1, domino[x][y] - 1) 22 | else: 23 | cnt = cnt - 1 24 | check[x][y] = False 25 | x += dx 26 | y += dy 27 | 28 | score = 0 29 | # simulation start 30 | for i in range(r): 31 | # attack 32 | x, y, k = input().split() 33 | x = int(x) - 1 34 | y = int(y) - 1 35 | attack(x, y, k) 36 | 37 | # defense 38 | x, y = map(int, input().split()) 39 | x -= 1 40 | y -= 1 41 | check[x][y] = True 42 | 43 | # print 44 | print(score) 45 | for i in range(n): 46 | for j in range(m): 47 | print('S' if check[i][j] else 'F', end=' ') 48 | print() -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/3번-문자열 지옥에 빠진 호석/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | typedef pair pii; 7 | #define NM 15 8 | #define INF 0x7fffffff 9 | 10 | int n, m, K; 11 | char a[NM][NM], target[10005][NM]; 12 | void input() { 13 | cin >> n >> m >> K; 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | } 17 | for (int i = 1; i <= K; i++) { 18 | cin >> target[i]; 19 | } 20 | } 21 | 22 | #include 23 | #include 24 | #include 25 | unordered_map mem; 26 | 27 | int dir[8][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} }; 28 | int ans[10005]; 29 | void dfs(int i, int j, string path,int len) { 30 | mem[path]++; 31 | if (len == 5) return; 32 | for (int k = 0; k < 8; k++) { 33 | int ni = (i + dir[k][0]) % n; 34 | int nj = (j + dir[k][1]) % m; 35 | if (ni < 0) ni += n; 36 | if (nj < 0) nj += m; 37 | 38 | dfs(ni, nj, path + a[ni][nj], len+1); 39 | } 40 | } 41 | 42 | void pro() { 43 | for (int i = 0; i < n; i++) { 44 | for (int j = 0; j < m; j++) { 45 | dfs(i, j, string(1, a[i][j]), 1); 46 | } 47 | } 48 | for (int i = 1; i <= K; i++) { 49 | cout << mem[string(target[i])] << "\n"; 50 | } 51 | } 52 | 53 | int main() { 54 | ios::sync_with_stdio(false); 55 | cin.tie(0); 56 | input(); 57 | pro(); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/3번-문자열 지옥에 빠진 호석/solution.py: -------------------------------------------------------------------------------- 1 | tile=[] 2 | find=dict() 3 | dx=[1,1,1,0,0,-1,-1,-1] 4 | dy=[1,0,-1,1,-1,1,0,-1] 5 | 6 | # dfs function 7 | def dfs(x, y, dstr): 8 | find[dstr] = find.get(dstr, 0) + 1 9 | if len(dstr)==5: 10 | return 11 | for i in range(8): 12 | nx=(x+dx[i]+n)%n 13 | ny=(y+dy[i]+m)%m 14 | dfs(nx, ny, dstr+tile[nx][ny]) 15 | 16 | # input 17 | n, m, k = map(int, input().split()) 18 | for i in range(n): 19 | tile.append(input()) 20 | 21 | # dfs for tile 22 | for i in range(n): 23 | for j in range(m): 24 | dfs(i, j, tile[i][j]) 25 | 26 | # print 27 | for i in range(k): 28 | print(find.get(input(), 0)) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/4번-꿈틀꿈틀 호석 애벌레/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | #define INF 0x7fffffff 7 | #define NM 100005 8 | 9 | using namespace std; 10 | 11 | int n, K; 12 | ll a[NM], dy[NM]; 13 | void input() { 14 | cin >> n >> K; 15 | for (int i = 1; i <= n; i++) cin >> a[i]; 16 | } 17 | 18 | void pro() { 19 | int right = 1; 20 | ll sum = 0, dyLeftMax = 0; 21 | 22 | for (int left = 1;left<=n;left++){ 23 | dyLeftMax = max(dyLeftMax, dy[left-1]); 24 | 25 | while (right <= n && sum < K){ 26 | sum += a[right++]; 27 | } 28 | 29 | if (sum >= K){ 30 | dy[right - 1] = max(dy[right - 1], dyLeftMax + (sum - K)); 31 | } 32 | else break; 33 | sum -= a[left]; 34 | } 35 | ll ans = 0; 36 | for (int i = 1; i <= n + 1; i++) ans = max(ans, dy[i]); 37 | cout << ans; 38 | } 39 | 40 | int main() { 41 | ios::sync_with_stdio(false); 42 | cin.tie(0); 43 | input(); 44 | pro(); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/4번-꿈틀꿈틀 호석 애벌레/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100005) 3 | 4 | # Input 5 | n, k = map(int, sys.stdin.readline().strip().split()) 6 | food = list(map(int, sys.stdin.readline().strip().split())) 7 | 8 | ans = 0 9 | def dfs(idx: int, energy: int): 10 | global ans 11 | ans = max(ans, energy) 12 | if idx == n: 13 | return 14 | 15 | sum = 0 16 | for i in range(idx, n): 17 | sum += food[i] 18 | if sum >= k: 19 | dfs(i + 1, energy + (sum - k)) 20 | break 21 | 22 | dfs(idx + 1, energy) 23 | 24 | 25 | dfs(0, 0) 26 | print(ans) 27 | -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/4번-꿈틀꿈틀 호석 애벌레/subsolution(brute force).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | #define INF 0x7fffffff 7 | #define NM 100005 8 | 9 | using namespace std; 10 | 11 | int n, K, D; 12 | ll a[NM], ans; 13 | void input() { 14 | cin >> n >> K; 15 | D = 0; 16 | for (int i = 1; i <= n; i++) cin >> a[i]; 17 | } 18 | 19 | void dfs(int x, ll energy) { 20 | if (x > n) { 21 | ans = max(ans, energy); 22 | return; 23 | } 24 | ll sum = 0; 25 | 26 | // x 번 먹이를 먹기로 결심! 27 | for (int i = x; i <= n; i++) { 28 | sum += a[i]; 29 | if (sum >= K) { 30 | dfs(i + D + 1, energy + sum - K); 31 | break; 32 | } 33 | } 34 | 35 | // x 번 먹이를 안 먹기로 결심! 36 | dfs(x + 1, energy); 37 | } 38 | 39 | int main() { 40 | ios::sync_with_stdio(false); 41 | cin.tie(0); 42 | input(); 43 | dfs(1, 0); 44 | cout << ans; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제1회/README.md: -------------------------------------------------------------------------------- 1 | # 제1회 2 | 3 | 홍보글: https://www.acmicpc.net/board/view/59258 4 | 5 | -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/1번-폰 호석만/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | a_str, b_str = sys.stdin.readline().strip().split() 3 | 4 | a_min_base = 2 5 | b_min_base = 2 6 | 7 | for x in a_str: 8 | if x.isalpha(): 9 | a_min_base = max(a_min_base, ord(x) - ord('a') + 11) 10 | if x.isdigit(): 11 | a_min_base = max(a_min_base, ord(x) - ord('0') + 1) 12 | 13 | for x in b_str: 14 | if x.isalpha(): 15 | b_min_base = max(b_min_base, ord(x) - ord('a') + 11) 16 | if x.isdigit(): 17 | b_min_base = max(b_min_base, ord(x) - ord('0') + 1) 18 | 19 | x = -1 20 | a = -1 21 | b = -1 22 | cnt = 0 23 | for i in range(a_min_base, 37): 24 | for j in range(b_min_base, 37): 25 | a_num = int(a_str, i) 26 | b_num = int(b_str, j) 27 | if a_num >= 2**63 or b_num >= 2**63: 28 | continue 29 | if a_num == b_num and i != j: 30 | x = a_num 31 | a = i 32 | b = j 33 | cnt += 1 34 | 35 | if cnt==0: 36 | print('Impossible') 37 | elif cnt>1: 38 | print('Multiple') 39 | else: 40 | print(x, a, b) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/2번-계보 복원가 호석/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import queue 3 | 4 | n = int(sys.stdin.readline().strip()) 5 | name = list(sys.stdin.readline().strip().split()) 6 | name.sort() 7 | number = dict() 8 | for i in range(n): 9 | number[name[i]] = i 10 | 11 | m = int(sys.stdin.readline().strip()) 12 | graph = [[] for i in range(n + 1)] 13 | indeg = [0 for i in range(n + 1)] 14 | for i in range(m): 15 | x, y = sys.stdin.readline().strip().split() 16 | graph[number[y]].append(number[x]) 17 | indeg[number[x]] += 1 18 | 19 | que = queue.Queue() 20 | parent = [] 21 | for i in range(n): 22 | if indeg[i] == 0: 23 | que.put(i) 24 | parent.append(name[i]) 25 | 26 | child = [[] for i in range(n + 1)] 27 | while que.qsize(): 28 | now = que.get() 29 | for next in graph[now]: 30 | indeg[next] -= 1 31 | if indeg[next] == 0: 32 | que.put(next) 33 | child[now].append(next) 34 | 35 | print(len(parent)) 36 | for x in parent: 37 | print(x, end = ' ') 38 | print() 39 | 40 | for i in range(n): 41 | print(name[i], len(child[i]), end = ' ') 42 | child[i].sort() 43 | for x in child[i]: 44 | print(name[x], end = ' ') 45 | print() -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/3번-짠돌이 호석/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n1, m1 = map(int, sys.stdin.readline().strip().split()) 4 | pict1 = [] 5 | for i in range(n1): 6 | pict1.append(sys.stdin.readline().strip()) 7 | 8 | n2, m2 = map(int, sys.stdin.readline().strip().split()) 9 | pict2 = [] 10 | for i in range(n2): 11 | pict2.append(sys.stdin.readline().strip()) 12 | 13 | res = 40404 14 | for _ in range(4): 15 | temp = [[0 for j in range(n2)] for i in range(m2)] 16 | for i in range(n2): 17 | for j in range(m2): 18 | temp[j][n2 - i - 1] = pict2[i][j] 19 | pict2 = temp 20 | n2, m2 = m2, n2 21 | for i in range(-51, 51): 22 | for j in range(-51, 51): 23 | pos = True 24 | for x in range(n1): 25 | for y in range(m1): 26 | if 0 <= x + i < n2 and 0 <= y + j < m2 and pict1[x][y] == '1' and pict2[x + i][y + j] == '1': 27 | pos = False 28 | break 29 | if not pos: 30 | break 31 | if pos: 32 | res = min(res, (max(n1 + i, n2) - min(0, i)) * (max(m1 + j, m2) - min(0, j))) 33 | print(res) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/4번-호석이 두 마리 치킨/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n, m = map(int, sys.stdin.readline().strip().split()) 3 | adj = [[101 for j in range(n)] for i in range(n)] 4 | 5 | for i in range(m): 6 | a, b = map(int, sys.stdin.readline().strip().split()) 7 | adj[a - 1][b - 1] = 1 8 | adj[b - 1][a - 1] = 1 9 | 10 | for i in range(n): 11 | adj[i][i] = 0 12 | 13 | for k in range(n): 14 | for i in range(n): 15 | if i == k: 16 | continue 17 | for j in range(n): 18 | if i == j or j == k: 19 | continue 20 | if adj[i][j] > adj[i][k] + adj[k][j]: 21 | adj[i][j] = adj[i][k] + adj[k][j] 22 | 23 | 24 | building = [1, 2] 25 | res = 101010 26 | for i in range(n): 27 | for j in range(i + 1, n): 28 | now = 0 29 | for k in range(n): 30 | if i == k or j == k: 31 | continue 32 | now += min(adj[i][k], adj[j][k]) 33 | if res > now: 34 | building = [i + 1, j + 1] 35 | res = now 36 | 37 | print(building[0], building[1], res * 2) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/5번-광부 호석/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n, c = map(int, sys.stdin.readline().strip().split()) 4 | info = [] 5 | seq_x = [] 6 | seq_y = [] 7 | for i in range(n): 8 | x, y, v = map(int, sys.stdin.readline().strip().split()) 9 | info.append((x, y, v)) 10 | seq_x.append((x, i)) 11 | seq_y.append((y, i)) 12 | seq_x.sort() 13 | seq_y.sort() 14 | seq_y.reverse() 15 | 16 | res = 0 17 | now = 0 18 | cnt = 0 19 | w = 0 20 | h = 0 21 | while w < n and h < n: 22 | if cnt < c: 23 | now_x = seq_x[w][0] 24 | while w < n and now_x == seq_x[w][0]: 25 | if info[seq_x[w][1]][1] > seq_y[h][0]: 26 | w += 1 27 | continue 28 | now += info[seq_x[w][1]][2] 29 | cnt += 1 30 | w += 1 31 | else: 32 | now_y = seq_y[h][0] 33 | while h < n and now_y == seq_y[h][0]: 34 | if info[seq_y[h][1]][0] >= seq_x[w][0]: 35 | h += 1 36 | continue 37 | now -= info[seq_y[h][1]][2] 38 | cnt -= 1 39 | h += 1 40 | if cnt <= c: 41 | res = max(res, now) 42 | print(res) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제2회/README.md: -------------------------------------------------------------------------------- 1 | # 제2회 2 | 3 | 홍보글: https://www.acmicpc.net/board/view/65936 4 | 5 | 정답 코드: Cpp는 류호석님, Java는 BarkingDog님, Python은 dlstj0923님의 코드입니다. -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/1번-빌런 호석/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int ll; 4 | using namespace std; 5 | #define NM 100005 6 | int N, K, P, X; 7 | int num_flag[10][7] = { 8 | {1, 1, 1, 0, 1, 1, 1}, 9 | {0, 0, 1, 0, 0, 1, 0}, 10 | {1, 0, 1, 1, 1, 0, 1}, 11 | {1, 0, 1, 1, 0, 1, 1}, 12 | {0, 1, 1, 1, 0, 1, 0}, 13 | {1, 1, 0, 1, 0, 1, 1}, 14 | {1, 1, 0, 1, 1, 1, 1}, 15 | {1, 0, 1, 0, 0, 1, 0}, 16 | {1, 1, 1, 1, 1, 1, 1}, 17 | {1, 1, 1, 1, 0, 1, 1} 18 | }; 19 | 20 | void input(){ 21 | cin >> N >> K >> P >> X; 22 | } 23 | int diff_one(int x,int y) { 24 | int res = 0; 25 | for (int i = 0; i < 7; i++) res += num_flag[x][i] != num_flag[y][i]; 26 | return res; 27 | } 28 | int diff(int x,int y) { 29 | int res = 0; 30 | for (int rep = 1; rep <= K; rep++) { 31 | res += diff_one(x % 10, y % 10); 32 | x /= 10; 33 | y /= 10; 34 | } 35 | return res; 36 | } 37 | void pro() { 38 | int ans = 0; 39 | for (int i = 1; i <= N; i++) { 40 | if (i == X) continue; 41 | if (diff(i, X) <= P) ans++; 42 | } 43 | cout << ans; 44 | } 45 | 46 | int main() { 47 | ios::sync_with_stdio(false); cin.tie(0); 48 | input(); 49 | pro(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/1번-빌런 호석/solution.py: -------------------------------------------------------------------------------- 1 | # 전처리 과정 - 불 반전 갯수 계산 2 | segment = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f] 3 | number = [[0 for _ in range(10)] for _ in range(10)] 4 | for i in range(10): 5 | for j in range(10): 6 | xor = segment[i] ^ segment[j] 7 | while xor: 8 | number[i][j] += (xor % 2) 9 | xor //= 2 10 | 11 | # 입출력 - 경우의 수 계산 12 | n, k, p, x = map(int, input().split()) 13 | count = 0 14 | for i in range(1, n + 1): 15 | reverse = 0 16 | temp_i = i 17 | temp_x = x 18 | for j in range(k): 19 | reverse += number[temp_i % 10][temp_x % 10] 20 | temp_i //= 10 21 | temp_x //= 10 22 | if 1 <= reverse <= p: 23 | count += 1 24 | print(count) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/2번-정보 상인 호석/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | typedef pair pii; 7 | #define NM 100005 8 | #define INF 0x7fffffff 9 | 10 | #include 11 | #include 12 | unordered_map name_map; 13 | int Q, pid_cnt; 14 | priority_queue info[NM]; 15 | 16 | void input(){ 17 | cin >> Q; 18 | } 19 | 20 | int get_pid(string name) { 21 | if (!name_map.count(name)) name_map[name] = ++pid_cnt; 22 | return name_map[name]; 23 | } 24 | 25 | void pro() { 26 | long long int ans = 0; 27 | while (Q--) { 28 | int query; 29 | cin >> query; 30 | string name; 31 | cin >> name; 32 | int pid = get_pid(name); 33 | if (query == 1) { 34 | int k; 35 | cin >> k; 36 | while (k--) { 37 | int x; 38 | cin >> x; 39 | info[pid].push(x); 40 | } 41 | } else { 42 | int b; 43 | cin >> b; 44 | while (b-- && !info[pid].empty()) { 45 | ans += info[pid].top(); 46 | info[pid].pop(); 47 | } 48 | } 49 | } 50 | cout << ans; 51 | } 52 | 53 | int main() { 54 | input(); 55 | pro(); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/2번-정보 상인 호석/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | q = int(sys.stdin.readline()) 4 | gorilla = dict() 5 | heap = [] 6 | result = 0 7 | 8 | for i in range(q): 9 | now = sys.stdin.readline().split() 10 | name = now[1] 11 | if now[0] == "1": 12 | idx = len(gorilla) 13 | if name in gorilla: 14 | idx = gorilla[name] 15 | else: 16 | gorilla[name] = idx 17 | heap.append([]) 18 | for i in range(3, int(now[2]) + 3): 19 | heapq.heappush(heap[idx], int(now[i]) * -1) 20 | else: 21 | idx = len(gorilla) 22 | if name in gorilla: 23 | idx = gorilla[name] 24 | else: 25 | continue 26 | for i in range(int(now[2])): 27 | if heap[idx]: 28 | result -= heapq.heappop(heap[idx]) 29 | print(result) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/3번-트리 디자이너 호석/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | typedef pair pii; 7 | #define NM 100005 8 | #define INF 0x7fffffff 9 | #define MOD 1000000007 10 | 11 | int n, a[NM], dy[NM][10]; 12 | vector con[NM]; 13 | 14 | void input() { 15 | cin >> n; 16 | for (int i = 1; i <= n; i++) { 17 | cin >> a[i]; 18 | } 19 | for (int i = 1;i <= n-1;i++){ 20 | int x, y; 21 | cin >> x >> y; 22 | con[x].push_back(y); 23 | con[y].push_back(x); 24 | } 25 | } 26 | 27 | void dfs(int x, int prev){ 28 | dy[x][a[x]] = 1; 29 | for (int y: con[x]){ 30 | if (y == prev) continue; 31 | dfs(y, x); 32 | for (int num=0;num<=9;num++){ 33 | dy[x][num] += dy[y][num]; 34 | dy[x][num] %= MOD; 35 | } 36 | for (int num=a[x];num<=9;num++){ 37 | dy[x][a[x]] += dy[y][num]; 38 | dy[x][a[x]] %= MOD; 39 | } 40 | } 41 | } 42 | 43 | void pro() { 44 | dfs(1, -1); 45 | int ans = 0; 46 | for (int i = 0; i <= 9; i++) { 47 | ans += dy[1][i]; 48 | ans %= MOD; 49 | } 50 | cout << ans; 51 | } 52 | int main() { 53 | input(); 54 | pro(); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/3번-트리 디자이너 호석/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(10**6) 3 | 4 | MOD = 1000000007 5 | n = int(input()) 6 | bulb = list(map(int, input().split())) 7 | adj = [[] for _ in range(n)] 8 | dp = [[0 for _ in range(10)] for _ in range(n)] 9 | for i in range(n - 1): 10 | x, y = map(int, input().split()) 11 | adj[x - 1].append(y - 1) 12 | adj[y - 1].append(x - 1) 13 | 14 | def dfs(parent, now): 15 | nowb = bulb[now] 16 | dp[now][nowb] = 1 17 | for nex in adj[now]: 18 | if nex == parent: 19 | continue 20 | dfs(now, nex) 21 | for i in range(10): 22 | dp[now][i] = (dp[now][i] + dp[nex][i]) % MOD 23 | for i in range(nowb, 10): 24 | dp[now][nowb] = (dp[now][nowb] + dp[nex][i]) % MOD 25 | 26 | dfs(-1, 0) 27 | res = 0 28 | for i in range(10): 29 | res = (res + dp[0][i]) % MOD 30 | print(res) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/4번-공정 컨설턴트 호석/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | typedef pair pii; 7 | #define NM 100005 8 | #define INF 0x7fffffff 9 | 10 | int n, X, a[NM]; 11 | 12 | void input() { 13 | cin >> n >> X; 14 | for (int i = 1; i <= n; i++) { 15 | cin >> a[i]; 16 | } 17 | } 18 | 19 | bool check(int num) { 20 | vector vec(num, 0); 21 | priority_queue, greater> Q(vec.begin(), vec.end()); 22 | for (int i = 1; i <= n; i++) { 23 | int pick = Q.top(); 24 | Q.pop(); 25 | if (pick + a[i] > X) return false; 26 | Q.push(pick + a[i]); 27 | } 28 | return true; 29 | } 30 | 31 | void pro() { 32 | int L = 1, R = n, ans = n; 33 | while (L <= R) { 34 | int mid = (L + R) / 2; 35 | if (check(mid)) { 36 | ans = mid; 37 | R = mid - 1; 38 | } else { 39 | L = mid + 1; 40 | } 41 | } 42 | cout << ans; 43 | } 44 | 45 | int main() { 46 | input(); 47 | pro(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/4번-공정 컨설턴트 호석/solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n, x = map(int, input().split()) 4 | time = list(map(int, input().split())) 5 | 6 | start = 1 7 | end = n 8 | while start <= end: 9 | mid = (start + end) // 2 10 | flag = True 11 | heap = [0] * mid 12 | for t in time: 13 | now = heapq.heappop(heap) 14 | if now + t > x: 15 | flag = False 16 | break 17 | heapq.heappush(heap, now + t) 18 | if flag: 19 | end = mid - 1 20 | else: 21 | start = mid + 1 22 | 23 | print(start) -------------------------------------------------------------------------------- /류호석배 알고리즘 코딩 테스트/제3회/README.md: -------------------------------------------------------------------------------- 1 | # 제3회 2 | 3 | 홍보글: https://www.acmicpc.net/board/view/71333 4 | 5 | 정답 코드: Cpp는 류호석님, Java는 류호석님, Python은 dlstj0923님의 코드입니다. --------------------------------------------------------------------------------