diff --git "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/11053.cpp" "b/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/11053.cpp" deleted file mode 100644 index f9d1d1e..0000000 --- "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/11053.cpp" +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -using namespace std; - -int main() { - // 입출력 시간 단축 설정 - ios::sync_with_stdio(false); - cin.tie(NULL); - - int n; - cin >> n; // 수열의 크기 입력 - - vector arr(n); // 수열을 저장할 배열 - vector dp(n, 1); // 각 인덱스에서의 LIS 길이를 저장하는 DP 배열 (초기값 1) - - // 수열 입력 - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } - - // 동적 프로그래밍을 이용하여 LIS 계산 - for (int i = 1; i < n; i++) { - for (int j = 0; j < i; j++) { - if (arr[i] > arr[j]) { - dp[i] = max(dp[i], dp[j] + 1); // 이전 값들 중에서 더 큰 값을 선택 - } - } - } - - // dp 배열에서 가장 큰 값이 가장 긴 증가하는 부분 수열의 길이 - cout << *max_element(dp.begin(), dp.end()) << '\n'; - - return 0; -} diff --git "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/20923.cpp" "b/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/20923.cpp" deleted file mode 100644 index c09d6e4..0000000 --- "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/20923.cpp" +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -using namespace std; - -// 게임 진행 함수 -string playGame(int cardCount, int rounds, deque &dodoDeck, deque &suyeonDeck) { - deque dodoGround; // 도도의 그라운드 카드더미 - deque suyeonGround; // 수연의 그라운드 카드더미 - - bool isDodoTurn = true; // true는 도도의 차례, false는 수연의 차례 - int currentRound = 0; // 게임 진행 횟수 - string winner; // 이긴 사람 - - while (true) { - currentRound++; // 게임 진행 횟수 증가 - - // 차례인 사람이 카드 내려놓기 - if (isDodoTurn) { // 도도의 차례 - dodoGround.push_back(dodoDeck.back()); // 도도가 카드 내려놓기 - dodoDeck.pop_back(); - } else { // 수연의 차례 - suyeonGround.push_back(suyeonDeck.back()); // 수연이가 카드 내려놓기 - suyeonDeck.pop_back(); - } - - // 0개 됐는지 체크 - if (dodoDeck.empty()) { // 도도의 카드 수가 0이면 - winner = "su"; - break; - } else if (suyeonDeck.empty()) { // 수연이의 카드 수가 0이면 - winner = "do"; - break; - } - - // 수연이가 종치기 - if (!dodoGround.empty() && !suyeonGround.empty() && - (dodoGround.back() + suyeonGround.back() == 5)) { - // 도도의 그라운드의 카드더미를 수연이의 덱 밑에 합치기 - while (!dodoGround.empty()) { - suyeonDeck.push_front(dodoGround.front()); - dodoGround.pop_front(); - } - // 수연의 그라운드의 카드더미를 수연이의 덱 밑에 합치기 - while (!suyeonGround.empty()) { - suyeonDeck.push_front(suyeonGround.front()); - suyeonGround.pop_front(); - } - } - // 도도가 종치기 - else if ((!dodoGround.empty() && dodoGround.back() == 5) || - (!suyeonGround.empty() && suyeonGround.back() == 5)) { - // 수연의 그라운드의 카드더미를 도도의 덱 밑에 합치기 - while (!suyeonGround.empty()) { - dodoDeck.push_front(suyeonGround.front()); - suyeonGround.pop_front(); - } - // 도도의 그라운드의 카드더미를 도도의 덱 밑에 합치기 - while (!dodoGround.empty()) { - dodoDeck.push_front(dodoGround.front()); - dodoGround.pop_front(); - } - } - - // M번 진행한 후 - if (currentRound == rounds) { - int dodoCardCount = dodoDeck.size(); - int suyeonCardCount = suyeonDeck.size(); - if (dodoCardCount < suyeonCardCount) { - winner = "su"; - } else if (dodoCardCount > suyeonCardCount) { - winner = "do"; - } else { - winner = "dosu"; - } - break; - } - - isDodoTurn = !isDodoTurn; // 차례를 바꾼다. - } - - return winner; // 게임의 승자를 return -} - -int main() { - int n, m; // n: 카드 수, m: 게임 진행 횟수 - cin >> n >> m; - - deque dodoDeck(n); // 도도의 카드 덱 - deque suyeonDeck(n); // 수연의 카드 덱 - - for (int i = 0; i < n; i++) { - cin >> dodoDeck[i] >> suyeonDeck[i]; - } - - string gameWinner = playGame(n, m, dodoDeck, suyeonDeck); // 게임 진행 - cout << gameWinner; // 승자 출력 - - return 0; -} diff --git "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/2579.cpp" "b/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/2579.cpp" deleted file mode 100644 index af0dfab..0000000 --- "a/07_\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225/\355\225\204\354\210\230/2579.cpp" +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -using namespace std; - -int main() { - // 입출력 시간 단축을 위한 설정 - ios::sync_with_stdio(false); - cin.tie(NULL); - - int numOfStairs; // 계단 수 - cin >> numOfStairs; - - int stairs[301] = {0}; // 계단 점수 배열 (최대 300) - int maxScore[301] = {0}; // 각 계단까지의 최댓값을 저장할 배열 - - // 계단 점수 입력 - for (int i = 1; i <= numOfStairs; i++) { - cin >> stairs[i]; - } - - // 첫 번째 계단 초기화 - maxScore[1] = stairs[1]; - - // 두 번째 계단 초기화 - if (numOfStairs > 1) { - maxScore[2] = stairs[1] + stairs[2]; - } - - // 세 번째 계단부터는 규칙에 따라 계산 - for (int i = 3; i <= numOfStairs; i++) { - // 세 개 연속 밟는 것을 피하기 위해 maxScore[i-2]에서 오는 경우와 - // maxScore[i-3]에서 두 개 연속 밟는 경우 중 큰 값 선택 - maxScore[i] = max(maxScore[i-2] + stairs[i], maxScore[i-3] + stairs[i-1] + stairs[i]); - } - - // 마지막 계단까지의 최댓값 출력 - cout << maxScore[numOfStairs] << '\n'; - - return 0; -} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/20437.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/20437.cpp" new file mode 100644 index 0000000..f9b3e3c --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/20437.cpp" @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using namespace std; + +pair findMinMaxLengths(const string& w, int k) { + int min_length = INT_MAX; + int max_length = -1; + + // 각 문자의 위치를 저장 + vector> char_positions(26); + + // 각 문자의 위치를 char_positions에 저장 + for (int i = 0; i < w.size(); i++) { + char_positions[w[i] - 'a'].push_back(i); + } + + // 각 문자별 위치 리스트에서 K개씩 연속한 위치 간의 거리 계산 + for (int i = 0; i < 26; i++) { + if (char_positions[i].size() < k) continue; // 해당 문자가 K번 미만 등장 시 건너뜀 + + // 해당 문자의 위치에서 K개 연속 부분 문자열의 길이 계산 + for (int j = 0; j <= char_positions[i].size() - k; j++) { + int length = char_positions[i][j + k - 1] - char_positions[i][j] + 1; + + min_length = min(min_length, length); + max_length = max(max_length, length); + } + } + + // 가능한 문자열이 없을 경우 -1 반환 + if (min_length == INT_MAX) min_length = -1; + if (max_length == -1) max_length = -1; + + return {min_length, max_length}; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + string w; + int k; + cin >> w >> k; + + pair result = findMinMaxLengths(w, k); + + // 둘 다 -1일 경우 단일 -1만 출력 + if (result.first == -1 && result.second == -1) { + cout << -1 << '\n'; + } else { + cout << result.first << " " << result.second << '\n'; + } + } + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/2473.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/2473.cpp" new file mode 100644 index 0000000..24da0d9 --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\353\217\204\354\240\204/2473.cpp" @@ -0,0 +1,62 @@ +#include +#include +#include +#include // math.h 대신 cmath 사용 + +using namespace std; + +vector findClosestToZero(const vector& arr, int num) { + long long result = 3000000001; // 초기값 설정 (0에 가까운 값을 찾기 위해) + vector ans(3); // 결과를 저장할 벡터 + + // 배열을 오름차순으로 정렬 + vector sorted_arr = arr; + sort(sorted_arr.begin(), sorted_arr.end()); + + // 첫 번째 포인터(k)를 고정하고, 두 번째 포인터(l)와 세 번째 포인터(r)를 사용해 탐색 + for (int k = 0; k < num - 2; k++) { + int l = k + 1; + int r = num - 1; + + while (l < r) { + long long val = sorted_arr[k] + sorted_arr[l] + sorted_arr[r]; + + // 절대값을 비교하여 0에 더 가까운 값을 찾으면 결과 갱신 + if (abs(val) < result) { + result = abs(val); + ans[0] = sorted_arr[k]; + ans[1] = sorted_arr[l]; + ans[2] = sorted_arr[r]; + } + + // 합이 0 미만: 왼쪽 포인터 이동 + if (val < 0) { + l++; + } + // 합이 0 이상: 오른쪽 포인터 이동 + else { + r--; + } + } + } + + return ans; // 최종 결과 반환 +} + +int main() { + + int num; + cin >> num; + + vector arr(num); + for (int i = 0; i < num; i++) + cin >> arr[i]; + + // findClosestToZero 함수를 호출 + vector result = findClosestToZero(arr, num); + + // 오름차순 정렬된 결과만 출력 + cout << result[0] << " " << result[1] << " " << result[2] << '\n'; + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/14503.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/14503.cpp" new file mode 100644 index 0000000..40c1017 --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/14503.cpp" @@ -0,0 +1,89 @@ +#include +#include + +using namespace std; + +int n, m; // 방의 크기 N x M +int r, c, d; + +vector> room; // 방의 상태를 나타내는 2차원 벡터 +vector> cleaned; // 청소 여부를 표시하는 2차원 벡터 + +// 북, 동, 남, 서 방향에 대한 행/열 이동값 설정 +const int DR[4] = {-1, 0, 1, 0}; // 방향에 따른 행 이동 +const int DC[4] = {0, 1, 0, -1}; // 방향에 따른 열 이동 + +// 방향을 반시계 방향으로 회전시키는 함수 +int rotateLeft(int direction) { + return (direction + 3) % 4; // 왼쪽으로 90도 회전 +} + +// 현재 방향에서 후진할 위치를 계산하는 함수 +pair moveBackward(int row, int col, int direction) { + int back_dir = (direction + 2) % 4; // 현재 방향에서 뒤쪽 방향 계산 + return {row + DR[back_dir], col + DC[back_dir]}; // 후진 위치 반환 +} + +// 방을 청소하고, 청소한 칸의 개수를 반환하는 함수 +int cleanRoom() { + int cleaned_count = 0; // 청소한 칸의 개수를 저장할 변수 + + while (true) { + // 1. 현재 위치를 청소 + if (!cleaned[r][c]) { // 현재 칸이 청소되지 않았으면 + cleaned[r][c] = true; // 청소 상태로 변경 + cleaned_count++; // 청소한 칸 개수 증가 + } + + bool found_cleanable = false; // 청소 가능한 칸을 찾았는지 여부 + + // 2. 주변 4칸을 확인하며 청소할 칸을 탐색 + for (int i = 0; i < 4; i++) { + d = rotateLeft(d); + int new_row = r + DR[d]; + int new_col = c + DC[d]; + + // 청소되지 않은 빈 칸이 있는 경우 이동 + if (new_row >= 0 && new_row < n && new_col >= 0 && new_col < m && !cleaned[new_row][new_col] && room[new_row][new_col] == 0) { + r = new_row; // 청소 가능한 위치로 이동 + c = new_col; + found_cleanable = true; + break; // 청소 가능한 칸을 찾았으므로 4방향 탐색 종료 + } + } + + // 3. 주변에 청소할 빈 칸이 없는 경우 + if (!found_cleanable) { + auto [back_row, back_col] = moveBackward(r, c, d); + if (room[back_row][back_col] == 1) { // 후진하려는 위치가 벽인 경우 + break; + } + r = back_row; // 후진 가능하면 후진 + c = back_col; + } + } + return cleaned_count; // 청소한 칸의 개수 반환 +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + + // 방의 크기를 입력 받는다. + cin >> n >> m; + // 처음에 로봇 청소기가 있는 칸의 좌표 (r,c), 방향 d + cin >> r >> c >> d; + + room.resize(n, vector(m)); // 방 상태를 저장할 벡터 크기 설정 + cleaned.resize(n, vector(m, false)); // 청소 여부를 저장할 벡터 설정 + + // 방의 상태 입력 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> room[i][j]; + } + } + + cout << cleanRoom() << '\n'; // 청소한 칸의 개수 출력 + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/20922.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/20922.cpp" new file mode 100644 index 0000000..8d26b5d --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/20922.cpp" @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +using namespace std; + +int findLongestSubarray(const vector& sequence, int n, int k) { + unordered_map count_map; // 각 원소의 등장 횟수를 저장하는 해시 맵 + int left = 0, max_length = 0; // 투 포인터의 왼쪽(left)과 최대 길이(max_length) + + // 오른쪽 포인터를 이동하면서 조건을 만족하는 부분 수열의 길이 계산 + for (int right = 0; right < n; right++) { + int num = sequence[right]; + count_map[num]++; // 현재 원소의 등장 횟수 증가 + + // 현재 원소가 K번 초과로 등장할 경우 왼쪽 포인터 이동 + while (count_map[num] > k) { + count_map[sequence[left]]--; // 왼쪽 포인터가 가리키는 원소의 등장 횟수 감소 + left++; // 왼쪽 포인터 오른쪽으로 이동 + } + + // 조건을 만족하는 부분 수열의 길이 갱신 + max_length = max(max_length, right - left + 1); + } + + return max_length; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + + int n, k; + cin >> n >> k; + + vector sequence(n); + for (int i = 0; i < n; i++) { + cin >> sequence[i]; + } + + cout << findLongestSubarray(sequence, n, k) << '\n'; + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/2531.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/2531.cpp" new file mode 100644 index 0000000..a9aedf1 --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/\355\225\204\354\210\230/2531.cpp" @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +using namespace std; + +int calcMaxSushi(const vector& belt, int n, int d, int k, int c) { + unordered_map sushi_count; // 각 초밥의 등장 횟수를 저장하는 해시 맵 + int distinct_sushi = 0, max_sushi = 0; + + // 초기 k개의 초밥에 대해 처리 + for (int i = 0; i < k; i++) { + if (sushi_count[belt[i]] == 0) distinct_sushi++; + sushi_count[belt[i]]++; + } + + max_sushi = distinct_sushi; + if (sushi_count[c] == 0) max_sushi++; // 쿠폰 초밥 추가 + + // 슬라이딩 윈도우로 나머지 경우 탐색 + for (int i = 1; i < n; i++) { + int remove_idx = i - 1; + int add_idx = (i + k - 1) % n; + + // 이전 초밥 제거 + if (--sushi_count[belt[remove_idx]] == 0) distinct_sushi--; + + // 새 초밥 추가 + if (sushi_count[belt[add_idx]]++ == 0) distinct_sushi++; + + int current_max = distinct_sushi; + + if (sushi_count[c] == 0) current_max++; // 쿠폰 초밥 추가 + + max_sushi = max(max_sushi, current_max); + } + + return max_sushi; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + + // 회전 초밥 벨트에 놓인 접시의 수 n, 초밥 가짓수 d, 연속해서 먹는 접시 수 k, 쿠폰 번호 c + int n, d, k, c; + cin >> n >> d >> k >> c; + + vector belt(n); + for (int i = 0; i < n; i++) { + cin >> belt[i]; + } + + cout << calcMaxSushi(belt, n, d, k, c) << '\n'; + return 0; +}