C++ Algorithm/백준 알고리즘 문제 풀이
우선순위 큐
3영2
2019. 8. 27. 18:20
반응형
11279번 문제 : 최대 힙
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> #include <string> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); vector<int> v; int num; cin >> num; while (num > 0) { int input; cin >> input; if (input == 0) { if (v.empty()) { cout << "0" << "\n"; } else { int max = v.front(); int index = 0; for (int i = 0; i < v.size(); i++) { if (v[i] > max) { max = v[i]; index = i; } } cout << max << "\n"; v.erase(v.begin() + index); } } else { v.push_back(input); } num--; } return 0; } | cs |
메모
-처음엔 시간초과 발생
-endl 그리고
ios::sync_with_stdio(0);
cin.tie(0);
추가하면 시간초과 해결됨
1927번 문제 : 최소 힙
https://www.acmicpc.net/problem/1927
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> #include <string> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); vector<int> v; int num; cin >> num; while (num > 0) { int input; cin >> input; if (input == 0) { if (v.empty()) { cout << "0" << "\n"; } else { int min = v.front(); int index = 0; for (int i = 0; i < v.size(); i++) { if (v[i] < min) { min = v[i]; index = i; } } cout << min << "\n"; v.erase(v.begin() + index); } } else { v.push_back(input); } num--; } return 0; } | cs |
반응형