회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
왜 시작하자마자 시간초과가 발생하는지 모르겠습니다.
7662번 - 이중 우선순위 큐
jaeochoiii0716
2년 전
0
직접 minHeap이랑 maxHeap 구현했는데 왜 시작하자마자 시간초과가 발생하는지 이유를 알고싶습니다 ㅜㅜ
let fs = require('fs'); let input = fs.readFileSync('example.txt').toString().split('\n'); let N = Number(input.shift()); // 우선순위 큐 구현 class MinHeap { constructor() { this.heap = []; } size() { return this.heap.length; } swap(idx1, idx2) { [this.heap[idx1], this.heap[idx2]] = [this.heap[idx2], this.heap[idx1]]; } add(value) { this.heap.push(value); this.bubbleUp(); } poll() { if(this.heap.length === 1) return this.heap.pop(); const value = this.heap[0]; this.heap[0] = this.heap.pop(); this.bubbleDown(); return value; } bubbleUp() { let index = this.heap.length - 1; let parentIndex = Math.floor((index - 1) / 2); while(this.heap[parentIndex] && this.heap[parentIndex] > this.heap[index]) { this.swap(index, parentIndex); index = parentIndex; parentIndex = Math.floor((index - 1) / 2); } } bubbleDown() { let index = 0; let leftIndex = index * 2 + 1; let rightIndex = index * 2 + 2; while((this.heap[leftIndex] && this.heap[leftIndex] <= this.heap[index]) || (this.heap[rightIndex] && this.heap[rightIndex] <= this.heap[index])) { let smallIndex = leftIndex; if(this.heap[smallIndex] > this.heap[rightIndex]) { smallIndex = rightIndex; } this.swap(index, smallIndex); index = smallIndex; leftIndex = index * 2 + 1; rightIndex = index * 2 + 2; } } pop() { return this.heap.pop(); } } class MaxHeap { constructor() { this.heap = []; } size() { return this.heap.length; } swap(idx1, idx2) { [this.heap[idx1], this.heap[idx2]] = [this.heap[idx2], this.heap[idx1]]; } add(value) { this.heap.push(value); this.bubbleUp(); } poll() { if(this.heap.length === 1) return this.heap.pop(); const value = this.heap[0]; this.heap[0] = this.heap.pop(); this.bubbleDown(); return value; } bubbleUp() { let index = this.heap.length - 1; let parentIndex = Math.floor((index - 1) / 2); while(this.heap[parentIndex] && this.heap[parentIndex] < this.heap[index]) { this.swap(index, parentIndex); index = parentIndex; parentIndex = Math.floor((index - 1) / 2); } } bubbleDown() { let index = 0; let leftIndex = index * 2 + 1; let rightIndex = index * 2 + 2; while((this.heap[leftIndex] && this.heap[leftIndex] >= this.heap[index]) || (this.heap[rightIndex] && this.heap[rightIndex] >= this.heap[index])) { let bigIndex = leftIndex; if(this.heap[bigIndex] < this.heap[rightIndex]) { bigIndex = rightIndex; } this.swap(index, bigIndex); index = bigIndex; leftIndex = index * 2 + 1; rightIndex = index * 2 + 2; } } pop() { return this.heap.pop(); } } for(let i = 0; i < N; i += 1) { let minQueue = new MinHeap(); let maxQueue = new MaxHeap(); let M = Number(input.shift()); let list = []; for(let j = 0; j < M; j += 1) { list.push(input.shift().split(' ')); } let map = {}; for(let j = 0; j < M; j += 1) { if(list[j][0] === 'I') { minQueue.add(Number(list[j][1])); maxQueue.add(Number(list[j][1])); map[Number(list[j][1])] = map[Number(list[j][1])] ? map[Number(list[j][1])] + 1 : 1; } else { if(list[j][1] === '-1') { while(minQueue.size() !== 0) { let value = minQueue.poll(); if(map[value] > 0) { map[value] -= 1; break; } } } else if(list[j][1] === '1') { while(maxQueue.size() !== 0) { let value = maxQueue.poll(); if(map[value] > 0) { map[value] -= 1; break; } } } } } let minHeap = []; let maxHeap = []; for(let i = 0; i < minQueue.size(); i += 1) { let value = minQueue.pop(); if(map[String(value)] !== 0) minHeap.push(value); i -= 1; } for(let i = 0; i < maxQueue.size(); i += 1) { let value = maxQueue.pop(); if(map[String(value)] !== 0) maxHeap.push(value); i -= 1; } if(minHeap.length === 0 && maxHeap.length === 0) console.log('EMPTY'); else console.log(`${Math.max(...maxHeap)} ${Math.min(...minHeap)}`); }
댓글을 작성하려면
로그인
해야 합니다.
jaeochoiii0716 2년 전
직접 minHeap이랑 maxHeap 구현했는데 왜 시작하자마자 시간초과가 발생하는지 이유를 알고싶습니다 ㅜㅜ