BZOJ 1555 KD之死 贪心 堆

版权声明:_ https://blog.csdn.net/lunch__/article/details/83901348

题意

  • 给你 n n 个盒子,每个盒子有重量 w w 和可以承受的最大重量 t t 两个属性,有些盒子是必选的,你现在要在把所有的必选的盒子选定的基础上,使选择的盒子最多,最开始你有一辆能承重 v v 的车,如果不能选完必选的盒子就输出 F o o l i s h   S D ! Foolish \ SD!

首先对于两个盒子 a a b b

如果 a a 放在 b b 上方 那么承重为 b t a w b_t-a_w

如果 b b 放在 a a 上方 那么承重为 a t b w a_t-b_w

如果 a a 放在 b b 上方更优 那么 b t a w > a t b w b_t-a_w>a_t-b_w

a t + a w < b t + b w a_t+a_w<b_t+b_w

那么我们可以贪心地按 a t + a w a_t+a_w 从小到大来从上到下确定盒子的顺序

因为从上到下每个物品上面的重量和是定值 选择没有后效性

用一个堆来维护不是必选的盒子中质量最大值

每次加入一个必选的盒子一直删除堆中的元素

直到剩余重量可以放在这个盒子上 或者堆为空

如果加入一个不是必选的 能放就直接放

不能放的话比较它和堆顶元素谁更优即可

对于车我们把它当作一个必选的盒子插在拍完序的序列后面就可以了

复杂度 O ( n log n ) O(n\log n)

Codes

#include <bits/stdc++.h>

using namespace std;

const int N = 6e5 + 10;

int n, m, maxv, Sumw, ans;

struct node {
	int w, t, mst; 
	bool operator < (const node &T) const {
		return w + t < T.w + T.t;
	}
}A[N];

priority_queue<int> q; 

int main() {
#ifndef ONLINE_JUDGE
	freopen("1555.in", "r", stdin);
	freopen("1555.out", "w", stdout);
#endif

	scanf("%d%d%d", &n, &m, &maxv);
	for (int i = 1; i <= n; ++ i)
		scanf("%d%d", &A[i].w, &A[i].t);
	for (int i = 1, x; i <= m; ++ i) 
		scanf("%d", &x), A[x].mst = 1; 
	sort(A + 1, A + n + 1);
	A[++ n] = (node){0, maxv, 1};
	
	for (int i = 1; i <= n; ++ i) {
		if (A[i].mst) {
			while (Sumw > A[i].t) {
				if (q.empty()) return puts("Foolish SD!"), 0;
				Sumw -= q.top(), q.pop(), -- ans;
			}
			Sumw += A[i].w;
		}
		else {
			if (Sumw > A[i].t) {
				if (!q.empty() && Sumw - q.top() <= A[i].t && A[i].w < q.top())
					Sumw -= q.top(), q.pop(), q.push(A[i].w), Sumw += A[i].w;
				continue;
			}
			Sumw += A[i].w, q.push(A[i].w);
		}
		++ ans;
	}

	printf("%d\n", ans - 1);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/lunch__/article/details/83901348