본문 바로가기

알고리즘74

백준 15903번 : 카드 합체 놀이 문제 링크 : www.acmicpc.net/problem/15903 내 풀이(2021.1.4.) : #include #include using namespace std; priority_queue pq; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i > temp; pq.push(temp); } for (int i = 0; i < m; i++) { long long temp1 = pq.top(); pq.pop(); long long temp2 = pq.top(); pq.pop(); pq.push(temp1 +.. 2021. 1. 5.
우선순위 큐와 힙 개념 및 C++ STL 개념 출처 : chanhuiseok.github.io/posts/ds-4/ STL 출처 : chanhuiseok.github.io/posts/algo-54/ * 우선순위 큐 - 우선순위가 높은 순서대로 pop하는 큐 - 우선순위 큐는 배열이나 연결리스트로 구현하는 것보다 힙으로 구현하는 것이 삽입 속도가 더 빠르므로, 힙으로 많이 구현함 * 복잡도 비교 1) 삽입 - 배열/연결리스트 : O(n) - 힙 : O(log2n) 2) 삭제 - 배열/연결리스트 : O(1) - 힙 : O(log2n) * 삽입 방법 1. 새로운 노드를 완전 이진 트리 구조를 유지할 수 있는 위치에 추가한다. 2. 새로운 노드로부터 시작해서, 부모/자식 노드 간에 비교를 통해 swap을 반복한다. * 삭제 방법 1. 루트 노드를 삭제.. 2021. 1. 4.
백준 10757번 : 큰 수 A+B 문제 링크 : www.acmicpc.net/problem/10757 내 풀이(2021.1.4.) : #include #include using namespace std; int a[1251]; // A int b[1251]; // B string c[1251]; // 결과 // 1~8글자짜리 int를 8글자짜리 string으로 변환 string num2str(int num) { string str = to_string(num); string result = "00000000"; for (int i = 0; i < str.size(); i++) { result[8 - str.size() + i] = str[i]; } return result; } int main() { ios_base::sync_with_.. 2021. 1. 4.
백준 11724번 : 연결 요소의 개수 문제 링크 : www.acmicpc.net/problem/11724 내 풀이(2021.1.3.) : #include #include using namespace std; int visit[1001] = { 0, }; int edge[1001][1001] = { 0, }; int N, M; int cnt = 0; void BFS(int root) { if (visit[root] == 1) return; visit[root] = 1; cnt++; queue q; q.push(root); while (!q.empty()) { int n = q.front(); q.pop(); for (int i = 1; i > N >> M; for (int i = 0; i > u.. 2021. 1. 3.