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;
}