Minimum n sums (use of STL heap)
| Minimum n sum |
| Time Limit: 1000 MS |
Memory Limit: 32768 K |
| Total Submit: 109 (26 users) |
Total Accepted: 22 (19 users) |
Rating: |
Special Judge: No |
|
| Description |
|
Given the numbers of Series A and Series B, each of which contains n numbers, the sum of any number from A and B will be obtained, and there will be n ^ 2 results (including repeated ), evaluate the first n smallest sum of n ^ 2 results.
|
| Input |
|
There are multiple groups of test data.
For each group of test data, the first action is n, the second action is A, and the third action is B.
1 <= n <= 100000, 0 <= Ai, Bi <= 10 ^ 9. |
| Output |
|
For each group of test data, output a row containing the first n smallest sums, which are output in ascending order, separated by a space. |
| Sample Input |
51 3 4 2 07 3 5 2 111074 50 47 45 38 64 19 2 84 6991 46 44 7 67 1 40 60 78 41 |
| Sample Output |
2 3 3 4 43 9 20 26 39 42 43 45 46 46 |
| Source |
HCPC2014 training competition 3
|
#include
#include
#include
#include #include
using namespace std;int a[100005];int b[100005];int sum[100005];int main(){#ifdef xxz freopen("in.txt","r",stdin);#endif // xxz int n; while(cin>>n) { for(int i = 0; i < n; i++) cin>>a[i]; for(int i = 0; i < n; i++) cin>>b[i]; sort(a,a+n); sort(b,b+n); for(int i = 0; i < n; i++) sum[i] = a[0] + b[i]; make_heap(sum,sum+n); for(int i =1 ; i < n; i++) { for(int j = 0; j < n; j++) { int t = b[j] + a[i]; if(t > sum[0]) break; else { pop_heap(sum,sum+n); sum[n-1] = t; push_heap(sum,sum+n); } } } sort(sum,sum+n); for(int i = 0; i < n-1; i++) { cout<
<<" "; } cout<<
<<">