AT_abc195_d [ABC195D] Shipping Center 的题解

文章介绍了如何解决一个看似背包问题但实际上可以通过排序和贪心策略处理的问题。通过对包裹和盒子进行排序,然后依次为每个盒子选择价值最高的包裹,可以在O(NMQ)的时间复杂度内得到解决方案。代码示例使用C++编写,展示了具体的实现过程。
摘要由CSDN通过智能技术生成

AT_abc195_d [ABC195D] Shipping Center 的题解

洛谷传送门
AT传送门

思路

这道题看似很像背包问题,其实不然。我们只需升序排序数组 X X X 后,再按顺序贪心地为每个盒子选择它能拿到的价值最高的包裹即可。总时间复杂度为 O ( N M Q ) \mathcal O(NMQ) O(NMQ)

代码

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
typedef pair<int, int> pii;
pii bags[55], boxes[55];
bool taken[55];
int main() {
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int n, m, q;
	n = read(), m = read(), q = read();
	for(int i = 0; i < n; i ++) {
		bags[i].second = read(), bags[i].first = read();
	}
	sort(bags, bags + n, greater<pii>());
	for(int i = 0; i < m; i ++){
		boxes[i].first = read();
		boxes[i].second = i;
	}	
	sort(boxes, boxes + m);
	while(q --) {
		int l, r, ans = 0;
		l = read(), r = read();
		l --, r --;
		fill(taken, taken + n, false);
		for(int i = 0; i < m; i ++) {
			auto [size, idx] = boxes[i];
			if(idx < l || idx > r) {
				int j = 0;
				for(; j < n; j ++) {
					if(!taken[j] && bags[j].second <= size) {
						break;
					}
				}
				if(j < n) {
					ans += bags[j].first;
					taken[j] = true;
				}
					
			}
		}
		write(ans), putchar('\n');
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值