문제 풀기 : https://school.programmers.co.kr/learn/courses/30/lessons/42628

CPP 연습에 의의가 있다.
그러므로. 완전탐색으로 역값을 찾을 필요는 없다.
최솟값을 찾으려면, node/2까지만 탐색해도 된다.
#include <string>을 도입해주어야 사용가능하다!
stoi -> string to intsubstr(begin,size) -> begin~size 까지의 인덱스로 string을 자른다.
#include <algorithm>을 도입해주어야 사용가능하다!
둘다 사용패턴은 아래와 같다.
somevector.push_back(...);
somevector.push_heap(somevector.begin(),somevector.end());
pop_heap(heap.begin(),heap.end());
heap.pop_back();
make_heap : vector 자체를 힙으로 만든다. (기본값 최대힙)sort_heap : 힙 자체를 재정렬하는데, 이때 어떤 연산자를 주지 않는다면 최소힙을 만든다partial_sort : 힙기반 정렬. > 3개를 받는데 중간을 잘라서 정렬하는 함수이다.D 1은 최댓값을 찾아 제거한다.D -1은 최솟값을 찾아서 제거하는데...최솟값을 찾기 위해서는, 위에서 최대 길이의 n/2까지만 돌아도 충분함을 보였으므로,
그 범위내에서 가장 최솟값을 찾으면 된다.
// 살짝 난잡하니 나중에 코드 정리하자...
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> heap = {};
for(auto now = operations.begin(); now != operations.end(); now++)
{
string st = (*now);
int val = stoi(st.substr(2,st.size()-2));
if(st.at(0) == 'I')
{
heap.push_back(val);
push_heap(heap.begin(),heap.end());
}
else if (st.at(0) != 'D') continue;
else if (val == -1)
{
if(heap.size() == 0)
continue;
else if(heap.size() == 1)
{
heap.erase(heap.begin());
continue;
}
int min_check = *(heap.rbegin());
int min_idx = heap.size()-1;
for(int now = heap.size()-1; now >= heap.size()/2 ; now-- )
{
if(heap.at(now) < min_check)
{
min_check = heap.at(now);
min_idx = now;
}
}
heap.erase(heap.begin() + min_idx);
make_heap(heap.begin(),heap.end());
}
else if (val == 1)
{
if(heap.size() == 0) continue;
pop_heap(heap.begin(),heap.end());
heap.pop_back();
}
}
vector<int> answer = {0,0};
if(heap.size() == 0)
return answer;
sort_heap(heap.begin(),heap.end());
pop_heap(heap.begin(),heap.end());
int min_check = *(heap.rbegin());
int min_idx = heap.size()-1;
int max_check = *(heap.begin());
int max_idx = 0;
if(heap.size() > 1)
{
for(int now = heap.size()-1; now >= heap.size()/2 ; now-- )
{
if(heap.at(now) < min_check)
{
min_check = heap.at(now);
min_idx = now;
}
}
for(int now = 0 ; now >= 2 ; now ++ )
if(heap.at(now) > max_check)
{
max_check = heap.at(now);
max_idx = now;
}
}
answer[0] = heap.at(max_idx);
answer[1] = heap.at(min_idx);
return answer;
}
