#include "souvenirs.h"
#include <vector>
#include <numeric>
#include <algorithm>
// Hàm kiểm tra xem một giá trị có trong vector đã sắp xếp không
bool isInVector(const std::vector<int>& vec, int val) {
auto it = std::lower_bound(vec.begin(), vec.end(), val);
return (it != vec.end() && *it == val);
}
void buy_souvenirs(int N, long long P0) {
// --- Giai đoạn 1: Khám phá giá tiền ---
std::vector<long long> P;
P.push_back(P0);
for (int i = 1; i < N; ++i) {
long long high = P.back() - 1;
long long low = 1;
long long found_price = P.back();
while (low <= high) {
long long mid = low + (high - low) / 2;
// Thực hiện giao dịch để kiểm tra P[i]
std::pair<std::vector<int>, long long> result = transaction(mid);
std::vector<int> bought_items = result.first;
// Vì mid < P[i-1], các món 0..i-1 sẽ không được mua.
// Ta chỉ cần kiểm tra món i có được mua không.
if (isInVector(bought_items, i)) {
// mid >= P[i]. Giá có thể là mid hoặc nhỏ hơn.
found_price = mid;
high = mid - 1;
} else {
// mid < P[i]. Cần giá cao hơn.
low = mid + 1;
}
}
P.push_back(found_price);
}
// --- Giai đoạn 2: Mua hàng theo lô ---
std::vector<long long> to_buy_prices;
for (int i = 1; i < N; ++i) {
for (int j = 0; j < i; ++j) {
to_buy_prices.push_back(P[i]);
}
}
// Sắp xếp giá giảm dần (tương đương index i tăng dần),
// danh sách đã được tạo theo thứ tự này rồi.
if (to_buy_prices.empty()) {
return;
}
long long current_batch_sum = 0;
for (long long price : to_buy_prices) {
if (current_batch_sum > 0 && current_batch_sum + price >= P[0]) {
transaction(current_batch_sum);
current_batch_sum = price;
} else {
current_batch_sum += price;
}
}
if (current_batch_sum > 0) {
transaction(current_batch_sum);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |