| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1311497 | thuhienne | Shopping Plans (CCO20_day2problem3) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define re exit(0);
int n,m,k;
vector <int> good[200009];
pair <int,int> lim[200009];
vector <ll> print;
struct temptation {
int type,pos;
ll cost;
const bool operator < (const temptation & other) const {
return cost > other.cost;
}
};
void djt() {
// 1. Sort mỗi vector good[i]
// 2. Tạo vector chứa cặp {good[i][1]-good[i][0], i} và sort theo hiệu số
// 3. PQ lưu: {tổng_hiện_tại, idx_trong_danh_sách_đã_sort, idx_phần_tử_trong_nhóm}
while(pq.size() && print.size() < k) {
auto [val, i, j] = pq.top(); pq.pop();
print.push_back(val);
// Tăng phần tử trong nhóm hiện tại
if (j + 1 < groups[i].size())
pq.push({val + groups[i][j+1] - groups[i][j], i, j + 1});
// Thử nhóm tiếp theo
if (i + 1 < m) {
// Nhánh mới: Thêm phần tử thứ 2 của nhóm tiếp theo
pq.push({val + groups[i+1][1] - groups[i+1][0], i + 1, 1});
// Nhánh "đổi vai": Bỏ phần tử thứ 2 nhóm i, thay bằng phần tử thứ 2 nhóm i+1
if (j == 1)
pq.push({val - (groups[i][1] - groups[i][0]) + (groups[i+1][1] - groups[i+1][0]), i + 1, 1});
}
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 1;i <= n;i++) {
int a,c;cin >> a >> c;
good[a].push_back(c);
}
for (int i = 1;i <= m;i++) cin >> lim[i].first >> lim[i].second;
for (int i = 1;i <= m;i++) sort(good[i].begin(),good[i].end());
djt();
}
