Sequence
Time Limit: 6000MS |
|
Memory Limit: 65536K |
Total Submissions: 9285 |
|
Accepted: 3097 |
Description Given m sequences, each contains n non-negative integer. Now we may select one of the sequence to form a sequence with M integers. It's clear that we could get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What are we need is the smallest n sums. Could help us?
Input The first line is a integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains integers m, n (0 < M <=, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.
Output for each test case, print a line with the smallest n sums in increasing order, which was separated by a space.
Sample Input
1
2 3
1 2 3
2 2 3
Sample Output
3 3 4
Source POJ Monthly,guang Lin
topic Meaning:
There are m sets of sequences with n number per group. Takes a number from each group and sums it, outputting the smallest n sum.
Problem Solving Ideas:
Input a set of processing a group, first to find the first two sets of the number of n minimum and then enter a set of numbers, the number of the group with the previous n minimum and together, and then find the two sets of the number of n minimum and ... All inputs are processed until the M-group count is complete.
The process is as follows:
① the first set of numbers in the input a array, followed by descending order, the second set of input B array is also descending order
② will a[0]+b[i] This n number first queue, and then go through a array of the remaining n-1 number and B array of n number of the sum, each comparison will be smaller and queued.
After the ③ has been traversed, the elements in the queue are assigned to the A array, and the queue is emptied.
④ Enter the next set of numbers to the B array, repeating the ② until the M-number input processing is complete.
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue
> #include <algorithm> const int maxn=30030;
int A[MAXN],B[MAXN];
using namespace Std;
int main () {#ifdef Online_judge #else freopen ("F:/cb/read.txt", "R", stdin);
Freopen ("F:/cb/out.txt", "w", stdout);
#endif Ios::sync_with_stdio (FALSE);
Cin.tie (0);
int t;
cin>>t;
while (t--) {priority_queue<int>q;//maximum precedence int m,n;
cin>>m>>n;
for (int i=0; i<n; ++i) cin>>a[i];
Sort (a,a+n);//ascending order--m;
while (m--) {for (int i=0; i<n; ++i) cin>>b[i];
Sort (b,b+n);
for (int i=0; i<n; ++i)//Queue Q.push (A[0]+b[i]); for (int i=1; i<n; ++i)//compare smaller and queue for (int j=0; j<n; ++j) {int
TEMP=A[I]+B[J]; if (Temp<q.top ()) {Q.pop ();
Q.push (temp);
}} for (int i=0; i<n; ++i)//assigns the number of queues to a array, while the queue empties {a[i]=q.top ();
Q.pop ();
} sort (a,a+n);
} for (int i=0; i<n-1; ++i)//output result cout<<a[i]<< "";
cout<<a[n-1]<<endl;
} return 0;
}/* 1 2 3 1 2 3 2 2 3 * *
test Data:
2
2 3
1 2 3
2 2 3
ten
123 3 123-143
2-656
234 45 5-435-324 435-34-342-3-324-32-324----
23 432 324
234 324 4
234 234 546 65 88 66 53 65 435 (5 654) 3245 345 4 765
5 3 34 34 34 56 345 234 2 34
Operation Result:
3 3 4
131 132 132 133 134 135 140 140 141 141