思路:在可以杀死兔子的箭中找最小花费的,用到了优先队列,随便复习下写法
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; const int MAXN = 100005; struct Node { int D,P; }node[MAXN]; int arr[MAXN], n, m; bool cmp1(Node a, Node b) { return a.D < b.D; } struct cmp { bool operator() (int x, int y) { return x > y; } }; priority_queue<int, vector<int>, greater<int> > q; int main() { while (scanf("%d%d", &n, &m) != EOF) { while (!q.empty()) q.pop(); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = 0; i < m; i++) scanf("%d", &node[i].D); for (int i = 0; i < m; i++) scanf("%d", &node[i].P); if (n > m) { printf("No\n"); continue; } sort(arr, arr+n); sort(node, node+m, cmp1); int cnt = m-1; int flag = 1; long long ans = 0; for (int i = n-1; i >= 0; i--) { while (cnt >= 0 && node[cnt].D >= arr[i]) { q.push(node[cnt].P); cnt--; } if (q.empty()) { flag = 0; break; } ans += q.top(); q.pop(); } if (flag) cout << ans << endl; else cout << "No" << endl; } return 0; }