Description
Weight Loss
The more you lose, the more fat you get!
Recently, the fail-to-lose-weight model has been playing a game to eliminate child-free games.
The game rule is simple. Just use an arrow to kill the child.
Arrows are consumable. We know that there are m different types of arrows to choose from, and each arrow will cause damage to rabbits, the corresponding damage values are di (1 <= I <= m), and each arrow requires a certain amount of QQ coins.
Assume that each arrow can only be used once, and each animation can only be shot once. Calculate the minimum QQ coins required to eliminate all rabbits on the map.
Input
There are multiple groups of input data, with each group having four rows of data;
The first line has two integers, n, m (1 <= n, m <= 100000), indicating the number of rabbits and the type of arrows respectively;
The second row has n positive integers, indicating the rabbit's blood volume Bi (1 <= I <= N );
The third row has M positive integers, indicating the Damage Value di (1 <= I <= m) that each arrow can cause );
The fourth row has M positive integers, indicating the qqcoin Pi (1 <= I <= m) required for each arrow ).
Note:
1. When the Damage Value of the arrow is greater than or equal to the blood volume of the rabbit, the rabbit can be killed;
2. The amount of blood Bi, the damage value di of the arrow, and the price pI of the arrow are all less than or equal to 100000.
Output
If you cannot kill all rabbits, output "no". Otherwise, output the minimum number of QQ coins and one row for each group.
Sample Input
3 3 1 2 3 2 3 4 1 3 3 3 4 1 2 2 3 3 3 1
Sample output
6 4
Train of Thought: Find the minimum cost among the arrows that can kill rabbits, use the priority queue, and review the code at will.
#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;}