HNCPC2023 D ContainerOrders

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;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值