<< 문제 >>
https://www.acmicpc.net/problem/1026
<< 풀이 >>
단순히 생각하면 A의 큰 값과 B의 작은 값을 곱해주면 작은 값이 됩니다.
B에 있는 수를 재배열하면 안된다고 조건이 있지만, 사실 이는 문제 풀이에서 크게 상관이 없습니다.
만약 B를 재배열하지 않도록 하려면 <B의 값, index>로 Map을 만들어서 사용하면 될 것 같네요.
<< 코드 >>
#include <queue>
#include <iostream>
using namespace std;
int main() {
priority_queue<int> pqA;
priority_queue<int> pqB;
int n;
cin >> n;
for(int i=0; i<n; i++)
{
int temp;
cin >> temp;
pqA.push(-temp);
}
for(int i=0; i<n; i++)
{
int temp;
cin >> temp;
pqB.push(temp);
}
int result = 0;
for(int i=0; i<n; i++)
{
result -= pqA.top() * pqB.top();
pqA.pop();
pqB.pop();
}
cout << result;
}
A에서 -를 붙인 이유는 pq가 내림 차순으로 정렬되기 때문에 -를 붙인다면 오름차순으로 사용할 수 있기 때문입니다.
<< 깃헙 >>
StudyAlgorithm_Part2/백준/Silver/1026. 보물 at main · J2on/StudyAlgorithm_Part2
This is a auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - J2on/StudyAlgorithm_Part2
github.com
< 백준 BaekJoon : 13305번 주유소 > C++ (0) | 2024.07.02 |
---|---|
< 백준 BaekJoon : 9935번 문자열 폭발> C++ (0) | 2024.06.20 |
< 백준 BaekJoon : 17520번 Balanced String > C++ (0) | 2024.06.20 |
< 백준 BaekJoon : 1931번 회의실 배정 > C++ (1) | 2024.06.18 |
< 백준 BaekJoon : 20058번 마법사 상어와 파이어스톰 > C++ (0) | 2024.04.09 |
댓글 영역