-
백준(BOJ) 15831번 준표의 조약돌알고리즘 풀이/백준(Boj) 2020. 4. 15. 03:26
문제 : https://www.acmicpc.net/problem/15831
15831번: 준표의 조약돌
첫 줄에 조약돌의 총 개수 N, 준표가 원하는 검은 조약돌의 최대개수 B와 하얀 조약돌의 최소개수 W가 주어진다. 둘째 줄에는 N개의 조약돌의 정보가 한 줄로 주어진다. i번째 문자가 B라면 i번 조약돌은 검은색이고, W라면 흰색이다.
www.acmicpc.net
풀이 :
완전 탐색은 시간 초과가 나므로 투 포인터를 이용해서 처리하자. lo와 hi 인덱스를 0으로 둔 후
조건을 만족하면 범위의 최댓값을 구하는 연산을 해준다.
코드 ( C++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; int n, b, w; const int INF = 987654321; int main() { cin >> n >> b >> w; string s; cin >> s; int lo = 0, hi = 0; int bc = 0, wc = 0; int ans = 0; while (lo < n) { if (hi < n && s[hi++] == 'W') wc++; else bc++; if (bc <= b && wc >= w) { ans = max(ans, hi - lo); } else if (bc > b) { if (s[lo++] == 'W') wc--; else bc--; } } cout << ans << "\n"; } '알고리즘 풀이 > 백준(Boj)' 카테고리의 다른 글
백준(BOJ) 2174번 로봇 시뮬레이션 (0) 2020.04.20 백준(BOJ) 17085번 십자가 2개 놓기 (수정) (0) 2020.04.18 백준(BOJ) 1806번 부분합 (0) 2020.04.15 백준(BOJ) 1022번 소용돌이 예쁘게 출력하기 (0) 2020.04.14 백준(BOJ) 16638번 괄호 추가하기 2 (0) 2020.04.09