Sequence
Time Limit: 6000MS |
|
Memory Limit: 65536K |
Total Submissions: 7762 |
|
Accepted: 2565 |
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 is separated by a space.
Sample Input
12 31 2 32 2 3
Sample Output
3 3 4
Source
POJ Monthly,guang Lin
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <queue> #include <vector> #include < Stack>using namespace Std;int tt,m,n;int a[2010],b[2010],heap[2010];int main () {scanf ("%d", &TT); while (tt--) {scanf ("%d%d", &m,&n); for (int i=0;i<n;i++) scanf ("%d", &a[i]); Sort (a,a+n); m--; while (m--) {for (int i=0;i<n;i++) scanf ("%d", &b[i]); Sort (b,b+n); for (int i=0;i<n;i++) heap[i]=a[i]+b[0]; Make_heap (Heap,heap+n); for (int i=1;i<n;i++) {bool ok=0; for (int j=0;j<n;j++) {int temp=a[j]+b[i]; if (Temp
(Application of heap) POJ 2442