diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/14503.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/14503.cpp" new file mode 100644 index 0000000..7c0944b --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/14503.cpp" @@ -0,0 +1,60 @@ +//11_투포인터 필수 3번: 14503.cpp + +#include +#include + +using namespace std; + +int n, m, r, c, d; +int arr[50][50]; +int count = 1; // 시작 지점은 항상 청소되어 있지 않음 +int dx[] = {-1, 0, 1, 0}; +int dy[] = {0, 1, 0, -1}; + +void clean(int x, int y, int dir) +{ + arr[x][y] = -1; + + for (int i = 0; i < 4; ++i) + { + dir = (dir + 3) % 4; + + int nx = x + dx[dir]; + int ny = y + dy[dir]; + if (nx >= 0 && ny >= 0 && nx < n && ny < m) + { + if (arr[nx][ny] == 0) + { + count++; + clean(nx, ny, dir); + return; + } + } + } + + int d = (dir + 2) % 4; // 반대 방향으로 후진 + int bx = x + dx[d]; + int by = y + dy[d]; + if (bx >= 0 && by >= 0 && bx < n && by < m && arr[bx][by] != 1) { + clean(bx, by, dir); // 후진이니까 바라보는 방향은 유지 + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n >> m >> r >> c >> d; + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) { + cin >> arr[i][j]; + } + } + + clean(r, c, d); + cout << count << "\n"; + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/20437.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/20437.cpp" new file mode 100644 index 0000000..291f65e --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/20437.cpp" @@ -0,0 +1,63 @@ +//11_투포인터 도전 1번: 20437 + +#include +#include +#include + +using namespace std; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + for (int i = 0; i < t; ++i) + { + string str; + cin >> str; + + int k; + cin >> k; + + if (k == 1) + { // k가 1일 때 + cout << "1 1\n"; + continue; + } + + vector alpha(26, 0); // 알파벳 별 개수를 저장한다. + for (char c : str) { + alpha[c - 'a']++; + } + + int minLen = numeric_limits::max(); + int maxLen = -1; + for (int j = 0; j < str.length(); ++j) + { + if (alpha[str[j] - 'a'] < k) continue; + + int count = 1; + for (int l = j + 1; l < str.length(); ++l) + { + if (str[j] == str[l]) count++; + if (count == k) + { + minLen = min(minLen, l - j + 1); + maxLen = max(maxLen, l - j + 1); + break; + } + } + } + + if (minLen == numeric_limits::max() || maxLen == -1) { + cout << "-1\n"; + } + else { + cout << minLen << " " << maxLen << "\n"; + } + } + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/20922.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/20922.cpp" new file mode 100644 index 0000000..fbe21fc --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/20922.cpp" @@ -0,0 +1,58 @@ +//11_투포인터 필수 2번: 20922 + +#include +#include +#include + +using namespace std; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int n, k; + cin >> n >> k; + + int limit[100001] = {0}; + vector num(n); + + for (int i = 0; i < n; ++i) { + cin >> num[i]; + } + + int maxLen = 0; + int startIdx = 0; + int endIdx = 0; + limit[num[startIdx]]++; + + for (int i = 1; i < n; ++i) + { + if (limit[num[i]] < k) + { + endIdx = i; + limit[num[endIdx]]++; + maxLen = max(endIdx - startIdx + 1, maxLen); + } + else + { + while (true) + { + if (num[startIdx] == num[i]) + { + startIdx++; + endIdx = i; + break; + } + else + { + limit[num[startIdx]]--; + startIdx++; + } + } + } + } + + cout << maxLen << "\n"; + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/2473.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/2473.cpp" new file mode 100644 index 0000000..81bb92e --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/2473.cpp" @@ -0,0 +1,65 @@ +//11_투포인터 도전 2번: 2473 + +#include +#include +#include +#include + +using namespace std; + +long long max_val = 3000000000LL; +long long pick[3]; + +void solution(vector& arr, int index) +{ + int left = index + 1; + int right = arr.size() - 1; + + while (left < right) + { + long long sum = arr[left] + arr[right] + arr[index]; + long long abs_sum = abs(sum); + + // 두 용액 갱신 + if (abs_sum < max_val) + { + pick[0] = arr[index]; + pick[1] = arr[left]; + pick[2] = arr[right]; + max_val = abs_sum; + } + + if (sum > 0) + right--; + else + left++; + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vector arr(n); + + for (int i = 0; i < n; ++i) { + cin >> arr[i]; + } + + sort(arr.begin(), arr.end()); + + for (int i = 0; i < n - 2; ++i) { + solution(arr, i); + } + + sort(pick, pick + 3); + + for (int i = 0; i < 3; ++i) { + cout << pick[i] << ' '; + } + + return 0; +} diff --git "a/11_\355\210\254\355\217\254\354\235\270\355\204\260/2531.cpp" "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/2531.cpp" new file mode 100644 index 0000000..4092ba4 --- /dev/null +++ "b/11_\355\210\254\355\217\254\354\235\270\355\204\260/2531.cpp" @@ -0,0 +1,57 @@ +//11_투포인터 필수 1번: 2531 + +#include +#include + +using namespace std; + +int main() { + // 입력값 처리 + int n, d, k, c; + cin >> n >> d >> k >> c; + + // 결과값 출력 + int eating[3001] = {0}; // 먹은 초밥 종류 관련 배열 + eating[c] = 3001; // 무료 초밥 종류 + vector sushi(N); + int count = 1; // 무료 초밥이 1개 있으므로 default Value = 1 + + // 회전 벨트 정보 저장 + for (int i = 0; i < n; ++i){ + cin >> sushi[i]; + } + + // 회전하지 않았을 때 초밥 종류 구하기 + for (int i = 0; i < k; ++i) + { + int sushiId = sushi[i]; + if (eating[sushiId] == 0){ + count++; + } + eating[sushiId]++; + } + + int maxCount = count; + + // (n-1)번 회전을 투 포인터를 이용하여 탐색 + for (int i = 0; i < n - 1; ++i) + { + int s_id = sushi[i]; // 처음 먹은 초밥 종류 + int e_id = sushi[(i + k) % n]; // 마지막 + 1번째 먹을 초밥 종류 + + if (--eating[s_id] == 0){ // 처음 먹은 초밥 종류 더 이상 없을 때 + count--; + } + + if (++eating[e_id] == 1){ // 마지막 + 1번째 먹을 초밥이 처음 먹는 것일 때 + count++; + } + + maxCount = max(maxCount, count); // 최대값 비교 + } + + // 최대 가짓수 출력 + cout << maxCount << "\n"; + + return 0; +}