![[백준 C++] 4949 : 균형잡힌 세상](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbBjfTi%2FbtsKZ71viT4%2FynF6bJSkIcxSyclZ6s9BkK%2Fimg.png)
https://www.acmicpc.net/problem/4949괄호라는 문제가 나오면 필자는 스택이 바로 떠오른다.그래서 바로 스택으로 구현하였다.#include #include #include using namespace std;int main(){ string str = " "; while (true) { bool correct = true; stack stack; getline(cin, str); if (str.compare(".") == 0) break; for (int i = 0; i cin의 getline이 아닌 string의 getline을 사용하였다.시작 괄호 ( '(', '[' )는 판별의 의미가 없기 때문에 바로 스택에 집어넣었고, 닫는 괄호 ( ')', ']' )를 확인..
![[백준 C++] 7568 : 덩치](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcrQmm3%2FbtsKYyrLLn1%2FiUj9hyVTxm4bKUKUs76Tz1%2Fimg.png)
https://www.acmicpc.net/problem/7568비교하는 과정이 필요한 브루트포스 문제이다.#include #include using namespace std;int main(){ int N; cin >> N; vector> list; for (int i = 0; i > x >> y; list.push_back(make_pair(x, y)); } for (int i = 0; i 각각의 값을 비교해가면서 나보다 큰 값이 존재하면 랭크를 한단계씩 내리면 된다.
![[백준 C++] 11724 : 연결 요소의 개수](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbOBczF%2FbtsKZbBliGG%2F4caFGfWTKBB59M407Ckm0K%2Fimg.png)
https://www.acmicpc.net/problem/11724BFS, DFS 전부 다 상관없지만, 필자는 BFS를 사용하였다.연결 요소는 간단하게 말해서 그래프의 개수이다.#include #include #include using namespace std;vector> graph;vector visited;int bfs(int start){ if (visited[start]) return 0; queue que; que.push(start); visited[start] = true; while (!que.empty()) { int current = que.front(); que.pop(); for (int i = 0; i > N >> M; graph = vector>(N + 1); visit..
![[백준 C++] 1654 : 랜선 자르기](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcAYRMc%2FbtsKYEQRPPa%2F5HQkkKJEt4I1qq7qfkAjE1%2Fimg.png)
https://www.acmicpc.net/problem/1654이분탐색을 응용하는 문제이다.N의 개수와 맞는 최대 길이를 구해야 하는데, 가능한 범위 (1 ~ 랜선 최대 길이)에서 확인해가면서 값을 찾아야 한다.#include #include using namespace std;vector list;int N;int calculate(unsigned int start, unsigned int end, int ans){ if (start > end) return ans; unsigned int len = (start + end) / 2; int count = 0; for (int i = 0; i = N) return calculate(len + 1, end, ans > len ? ans : len);..
![[백준 C++] 10816 : 숫자 카드 2](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdXAoa3%2FbtsKS7Vmrrl%2FyKbJD2l1107ZJbDMSR5h00%2Fimg.png)
https://www.acmicpc.net/problem/10816주어지는 수의 범위가 굉장히 크기 때문에, 시간초과에 유의해서 풀어야 한다.#include #include #include using namespace std;int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int N, M; vector list; vector input; cin >> N; for (int i = 0; i > temp; list.push_back(temp); } sort(list.begin(), list.end()); cin >> M; for (int i = 0; i > temp; input.push_back(temp); } for (int i = ..
![[백준 C++] 2164 : 카드2](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcwJQGu%2FbtsKUAn2wqS%2FCQ4hXVIGtb8RNwJ8rFh4T1%2Fimg.png)
https://www.acmicpc.net/problem/2164설명만 봐도 queue를 사용해서 풀 수 있는 간단한 문제라는 것을 알 수 있다.#include #include using namespace std;int main(){ queue que; int N; cin >> N; for (int i = 1; i
![[백준 C++] 1764 : 듣보잡](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FGTDER%2FbtsKTp7LKzn%2Fy1qw5LR03wUpZEdaoLNGKk%2Fimg.png)
https://www.acmicpc.net/problem/1764시간초과를 잘 처리해야 하는 문제이다.#include #include using namespace std;int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int N, M; set list; set ans; cin >> N >> M; for (int i = 0; i > temp; list.insert(temp); } for (int i = 0; i > temp; auto check = list.find(temp); if (check != list.end()) ans.insert(temp); } cout 시간초과를 방지하기 위해 tie와 sync_with_std..
![[백준 C++] 1920 : 수 찾기](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fcf7g4w%2FbtsKQfeuG9q%2FDWiO86etXLSIzQ2Cdn7Cf0%2Fimg.png)
https://www.acmicpc.net/problem/1920첫번째로 주어진 N개의 정수 사이에, M개로 주어진 정수들이 포함되어 있는지 확인하는 문제이다.이분 탐색으로 해결하지 않으면 시간 초과에 걸릴 가능성이 높기 때문에 정렬 후 이분탐색으로 구현하였다.#include #include #include using namespace std;vector list;vector input;int findValue(int value, int v1, int v2){ if (v1 > v2) return 0; int mid = (v1 + v2) / 2; if (value == list.at(mid)) return 1; else if (value > list.at(mid)) return findValue(va..
![[백준 C++] 11866 : 요세푸스 문제 0](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FAN98j%2FbtsKOHveqPx%2FYOJCVGv1GeIlG19Uni72t0%2Fimg.png)
https://www.acmicpc.net/problem/11866solved.ac Class 2 에센셜에 포함되어 있는 구현 문제이다.#include #include using namespace std;int main(){ int list[1001] = { 0, }; int N, K; cin >> N >> K; int idx = K; vector ans; for (int i = 1; i = N) break; else { int move = 0; while (move N) idx = 1; else idx += 1; if (list[idx] != 0) move++; } } } cout \n"; return 0;}while문 안에 while문이 또 있어 ..
![[백준 C++] 7562 : 나이트의 이동](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FssIMm%2FbtsKOzXhC7N%2FgX5jv0nqETEH9V5TvSQPI0%2Fimg.png)
https://www.acmicpc.net/problem/7562문제에서 주어진 내용에 있는 '최소 몇 번' 이라는 문구를 보고 BFS로 풀었다.#include #include #include using namespace std;vector> answer;vector> visited;int dx[8] = { -2, -1, 1, 2, -2, -1, 1, 2 };int dy[8] = { -1, -2, -2, -1, 1, 2, 2, 1 };void bfs(int X, int Y, int I){ queue> que; visited[Y].at(X) = true; que.push(make_pair(Y, X)); while (!que.empty()) { int currentY = que.front().first; ..