HNCPC2023 Container Orders
Description
binarycopycode research in using Deep Reinforcement Learning(DRL) to solve Container Relocation Problem(CRP) during his master degree. Now he want to ask you one question about containers orders. Of course not about DRL.
There are n containers, where the ith container has a weight of 2ki and an associated cost value of Wi.
Here comes m orders, each specifying a desired combination of containers whose total weight should exactly equal hj∗2tj. Our objective is to determine the minimum cost required to fulfill all of these orders.
Input
First line, one integer n(1≤n≤10,000) .
next n lines, ki(0≤ki≤1,000) and Wi(0≤Wi≤10,000).
next one line, one integer m(1≤m≤10,000).
next m lines, two integer tj(0≤t≤1,000) and hj(0≤h,∑h≤5,000).
Output
one line, the minimum cost. If it is impossible to fulfill all the orders, output “-1”.
Sample
Input
1
10 0
1
9 2
Output
0
优先队列数组, 对于同一重量, 对其价值进行降序排序
合并订单
最终目标:完成所有的订单,所以我们只需要知道完成所有的订单需要的各种重量的箱子的个数
sum[i]表示完成这个订单需要的重量为2的i次方的箱子的个数
合并箱子
当sum[i] < pq[i].size()时
需要的重量为2的i次方的箱子的个数 < 提供的重量为2的i次方的箱子的个数(不需要那么多重量为2的i次方的箱子时)
就把剩余的箱子合并成更大的箱子, 2个重量为2的3次方的箱子可以组合成一个重量为2的4次方的箱子
(向上合并是因为,我们必须先提供重量小的箱子,小的可以合并成大的但大的不能拆解成小的,满足了小的箱子的需求后,就应该尽量去满足大箱子的需求)
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> pq[1013];
vector<int> sum(1013, 0);
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
pq[a].push(b);
}
int m;
cin >> m;
//输入并合并订单
for (int i = 1; i <= m; i++) {
int t, h;
cin >> t >> h;
while (h) {//2的t次方
if (h % 2 != 0) sum[t]++; //如果h的最低位置为1, 就++, 为0不++ //if(h & 1) sum[t]++;
h /= 2; //h >>= 1
t++;
} //sum[i]需要重量为2的i次方的箱子的个数
}
int ans = 0;
for (int i = 0; i <= 1012; i++) {
if (pq[i].size() < sum[i]) { // 2的i次方的个数 < 需要的2的i次方的个数
printf("-1");
return 0;
}
while (sum[i]--) { //把用到的箱子的价值累加
ans += pq[i].top();
pq[i].pop();
}
while (pq[i].size() >= 2) { //如果箱子还有剩余,就合并可合并的箱子的价值 两个2的3次方合并成一个2的4次方
int temp = pq[i].top();
pq[i].pop();
temp += pq[i].top();
pq[i].pop();
pq[i + 1].push(temp);
}
}
printf("%d", ans);
return 0;
}