1890. Balls Again
Constraints
Time Limit: 3 secs, Memory Limit: 32 MB
Description
Yes, balls again! Given n different color balls with points marked on them and m different columniform bottles. Each bottle has limited capacity to hold in balls. Now the job is to put the balls into the bottles. As his name, Max wants to know the maximum number of balls that can be put into bottles and meanwhile maximize the sum of points of the balls inside bottles.
Input
Input contains multiple test data sets.
For each data set, first comes two integers n, m (1 <= n, m<= 200) in one line. Then n lines follow with one integer p (1 <= p <= 10^6), the point of corresponding ball, per each line. Then follows m lines. Each has two integers c and q ( 0 <= c <= 200, 1 <= q <= 10^6), the capacity of corresponding bottle (that is the maximum number of balls that bottle can hold) and the upper limit of point in the bottle (this means that all balls in this bottle should not have point larger than q). Input is terminated by EOF.
Output
For each test data set, one output data set should be generated as follow:
Generates two integers in one line: B, the maximum number of balls filled into bottles. And S, the maximum sum of points of that B balls which are inside bottles.
Sample Input
2 1 2 3 1 2 2 2 4 5 2 4 2 5
Sample Output
1 22 9
// Problem#: 1890 // Submission#: 3258640 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <iostream> #include <algorithm> using namespace std; struct bottle { int maxNum; int maxVal; }; bottle b[205]; int ball[205]; bool cmp(const bottle & b1, const bottle & b2) { return b1.maxVal > b2.maxVal; } int main() { std::ios::sync_with_stdio(false); while (1) { int N, M; cin >> N >> M; if (cin.eof()) break; for (int i = 0; i < N; i++) cin >> ball[i]; for (int i = 0; i < M; i++) cin >> b[i].maxNum >> b[i].maxVal; sort(ball, ball + N); sort(b, b + M, cmp); int counter = 0, sum = 0; for (int i = N - 1, j = 0; i >= 0 && j < M; ) { if (ball[i] <= b[j].maxVal && b[j].maxNum > 0) { b[j].maxNum--; counter++; sum += ball[i]; i--; } else if (b[j].maxNum == 0) { j++; } else if (ball[i] > b[j].maxVal) { i--; } } cout << counter << " " << sum << endl; } //getchar(); //getchar(); return 0; }