일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 깊이우선탐색
- priority_queue
- 분할정복
- 이분탐색
- 에라토스테네스의 체
- stoi
- 최소공배수
- 문자열
- 알고리즘
- 유클리드호제법
- BFS
- 우선순위큐
- Sort
- 티스토리챌린지
- 정렬
- int
- 오블완
- 다이나믹프로그래밍
- vector
- Set
- DP
- 프로그래머스
- map
- 백트래킹
- 배열
- N과M
- C++
- 그래프
- DFS
- 백준
- Today
- Total
목록백준 (63)
안녕 세상아,
https://www.acmicpc.net/problem/1260 BFS와 DFS의 기본적인 문제이다. 템플릿으로 사용해도 될듯..값을 입력할 때 양방향 간선으로 고려해야한다. 또한, 방문할 수 있는 정점이 여러 개인 경우에는 정점 번호가 작은 것을 먼저 방문 한다는 조건을 생각해서 정렬한다. #include #include #include #include #include using namespace std;int n, m, v;bool isVisited[1001] = { false };vector graph[1001];void reset() { for (int i = 1; i q; q.push(start); isVisited[start] = true; while (!q.empty()) { int ..
https://www.acmicpc.net/problem/1654이분탐색으로 풀 수 있는 문제이다.초기값(start)는 1로 두고, 마지막값(end)은 sort한 벡터 마지막 값으로 설정한다. 이분탐색을하기 위해서는 무조건 오름차순으로 정렬하고 해야한다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이기 때문에 int값은 long long타입으로 설정해야한다. 또한, 최대 길이를 구해야하기 때문에 sum==n이더라도 계속해서 값을 갱신해서 저장을 해야한다. #include #include #include using namespace std;int main() { int k, n; cin >> k >> n; vector v; for (int i = 0; i > k; ..
https://www.acmicpc.net/problem/2776가장 먼저 생각해볼 수 있는 방법은 2중 for문이다. 하지만..각각의 수첩1과 수첩2의 범위가 최대 1,000,000이기 때문에 시간초과가 된다. 그러면 생각할 수 있는 방법은 두가지이다. 첫째, 이분탐색둘째, 해시를 사용하는 것이다. (set) 나는 이 중 이분탐색을 이용해서 풀었다. binary_search를 사용해서 그냥 풀어도 되지만 오랜만에 이분탐색을 풀어봤기 때문에 직접ㅂ 함수를 만들어서 풀어봤다. 코드를 보면 알 수 있듯이 이분탐색을 위해서는 가장 먼저 오름차순 정렬을 해야한다. 그 후, left, right, mid 값을 설정한 후 target값과 비교하면서 계산해주면 된다. #include #include #include..
https://www.acmicpc.net/problem/1436풀이)'666'만 있으면 종말의 수 이기 때문에 1000으로 나누고 666이 나오면 된다.만약 바로 666이 나오지 않으면(큰 수일 경우) 10을 나눠준다.10을 계속 나눠주다가 666보다 작아지면 while문을 break 해준다.while문을 끝낸 후 i++ 하면서 계속 for문 돌리다가 주어진 n과 cnt가 같으면 i를 출력해준다.#include using namespace std;int main() { int n; cin >> n; int cnt = 0; for (int i = 666;; i++) { int tmp = i; while (tmp >= 666) { if (tmp % 1000 == 666) { cnt++; ..
https://www.acmicpc.net/problem/108141. vector pair 사용해서 값 받아오기2. stable_sort 사용해서 정렬동일한 키값을 가지는 요소들의 입력 순서 유지3. cmp 함수 만들어서 first값만 비교#include #include #include using namespace std;bool cmp(pair a, pair b) { return a.first > v; int N; cin >> N; for (int i = 0; i > x >> y; v.push_back(make_pair(x, y)); } stable_sort(v.begin(), v.end(),cmp); for (int i = 0; i
https://www.acmicpc.net/problem/1193주어진 n이 몇번째 대각선에 있는지 찾은 후 대각선에서 몇번째 있는지 찾으면 된다. 몇번째인지 찾은 후 짝수일 때와 홀수일 때 나눠서 생각한다. 짝수일 때는 분자의 숫자가 커지고, 분모의 숫자는 작아진다. 반대로 홀수일 때는 분자의 숫자는 작아지지만, 분모의 숫자는 커진다. 자세한 설명은 코드 참고!#include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int dia = 0; // 대각선 번호 int sum = 0; // 대각선 최대 번호 //n이 포함된 대각선 찾기 ..
https://www.acmicpc.net/problem/11650vector 를 사용해서 값 받은 후 정렬해서 출력하면 된다. #include #include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector> v; for (int i = 0; i > a >> b; v.push_back({ a,b }); } sort(v.begin(), v.end()); for (int i = 0; i
https://www.acmicpc.net/problem/4673배열 만들어서 소거법해서 지우는 방법으로 풀면 된다. 자릿수 대로 나눠서 풀면 된다. out of bound가 되면 안되니까 10000이하일때만 값 바꾸게 한다. #include #include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int arr[10001] = { 0 }; for (int i = 1; i
https://www.acmicpc.net/problem/2941c, d, l, n, s, z 일 때 if절로 분기 나눠서 풀면 된다. 크로아티아 숫ㅈㅏ 만나면 i++로 넘어가고 모든 조건 끝에 무조건 cnt++해주면 쉽게 풀 수 있다. #include #include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string str; cin >> str; int cnt = 0; for (int i = 0; i
https://www.acmicpc.net/problem/2751 sort 사용하면 됨.출력할 때 시간초과 대비하여 '\n' 사용ㅎㅐ주면 됨#include #include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector v; for (int i = 0; i > x; v.push_back(x); } sort(v.begin(), v.end()); for (int i = 0; i