【HDOJ】4544 湫湫系列故事——消灭兔子

时间:2022-01-01 13:17:36

贪心,普通贪心两层循环TLE了,然后用优先级队列维护内层。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std; #define MAXN 100005 typedef struct arrow_t {
int p, d;
bool operator < (const arrow_t &x) const {
return p > x.p;
}
} arrow_t; int B[MAXN];
arrow_t arrows[MAXN];
int n, m; bool comp(arrow_t a, arrow_t b) {
return a.d < b.d;
} int main() {
int i, j, k;
__int64 ans;
arrow_t arr;
bool flag; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<n; ++i)
scanf("%d", &B[i]);
for (i=; i<m; ++i)
scanf("%d", &arrows[i].d);
for (i=; i<m; ++i)
scanf("%d", &arrows[i].p); sort(B, B+n);
sort(arrows, arrows+m, comp);
ans = ;
priority_queue<arrow_t> Q;
k = m-;
flag = true;
for (i=n-; i>=; --i) {
while (k>= && arrows[k].d>=B[i]) {
Q.push(arrows[k]);
--k;
}
if (Q.empty()) {
flag = false;
break;
}
arr = Q.top();
Q.pop();
ans += arr.p;
} if (flag) {
printf("%I64d\n", ans);
} else {
printf("No\n");
}
} return ;
}