[Baekjoon] 11501λ²: μ£Όμ
π₯ λ¬Έμ
https://www.acmicpc.net/problem/11501
π μ€κ³
μ΄λ λ μ κ°κ²©λ³΄λ€ μ΄νμ λ κ°κ²©λ€ μ€ κ°μ₯ λμ λ νμμΌ μ΄μ΅μ μ΅λνν μ μλ€. κ°μ₯ κ°κ²©μ΄ λμ λ μ νκ³ , κ·Έ λ μ΄νμ λ λ€ μ€μμ κ°μ₯ κ°κ²©μ΄ λμ λ νκ³ , μ΄ λ°©μμ λ°λ³΅ν΄μΌ νλ€.
νμ¬ μν©μμ κ°μ₯ λμ κ°κ²©μ ꡬνκΈ° μν΄ μ°μ μμ νλ₯Ό νμ©νλλ°, μ€μν κ²μ λ¨μμλ κ°κ²©λ€ μ€ κ°μ₯ λμ κ°κ²©μ μ°¨μ΅μ λ³΄κ³ νλ§€ν μ£Όμ κ°κ²©λ€μ΄ μμΌλ―λ‘, μΈλ±μ€λ₯Ό ν¨κ» λ΄μ μ κ±°ν΄μΌ νλ€.
맨 λ€κΉμ§ κ³ λ €νμ λ, μ£Όμ κ°κ²©μ΄ κ°μ₯ λμ μ§μ μμ νμμΌ νλ€. μμλ₯Ό λ°κΏμ 맨 λ€λΆν° μ΅λκ°μ κ°±μ νλ©΄μ μ°¨μ΄λ₯Ό λΉΌμ£Όλ©΄ κ°λ¨νκ² ν΄κ²°ν μ μλ€.
π νμ΄
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int t, n;
vector<int> prices(1000000);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
priority_queue<pair<int, int>> pq;
long long result = 0;
for (int j = 0; j < n; j++) {
cin >> prices[j];
pq.push({ prices[j], j });
}
int max_value;
for (int j = 0; j < n; j++) {
while (pq.top().second < j) {
pq.pop();
}
max_value = pq.top().first;
if (max_value > prices[j]) {
result += (max_value - prices[j]);
}
}
cout << result << "\n";
}
return 0;
}
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int t, n;
vector<int> stock(1000000);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
for (int u = 1; u <= t; u++) {
long long result = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> stock[i];
}
int max_value = stock[n - 1];
for (int i = n - 2; i >= 0; i--) {
max_value = max(max_value, stock[i]);
result += max_value - stock[i];
}
cout << result << "\n";
}
return 0;
}