題意
- 給你個盒子,每個盒子有重量和可以承受的最大重量兩個屬性,有些盒子是必選的,你現在要在把所有的必選的盒子選定的基礎上,使選擇的盒子最多,最開始你有一輛能承重的車,如果不能選完必選的盒子就輸出。
首先對於兩個盒子和,如果放在上方,那麼承重爲;如果放在上方,那麼承重爲。如果放在上方更優,那麼,即。
我們可以貪心地按從小到大來從上到下確定盒子的順序,因爲從上到下每個物品上面的重量和是定值,選擇沒有後效性,用一個堆來維護已經選擇的盒子中不是必選的盒子中質量最大值,每次加入一個必選的盒子一直刪除堆中的元素,直到剩餘重量可以放在這個盒子上,或者堆爲空,如果加入一個不是必選的,能放就直接放 ,不能放的話比較它和堆頂元素誰更優即可。
對於車我們把它當作一個必選的盒子插在排完序的序列後面就可以了,複雜度。
#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;
}