Sequence
Time Limit: 6000MS |
|
Memory Limit: 65536K |
Total Submissions: 9131 |
|
Accepted: 3037 |
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 Linfirst of all, this is a system can be given to the priority_queue, but after all, now I am learning the handwriting heap, so I give myself trouble ~ idea: Because, to each row to take one, constitute a sum. Need to find n sum. We need a row to find, it is advisable to set the first two rows of the smallest n sum is the number of N and the second row of the first number of rows of the composition. Into the c[] array, and then one at a time to traverse the other number of the second line, to see if there is less than the largest number in the c[] array, and then replace, this is the first two rows of the smallest n sum has been found, save to the c[] array, and then find the first three rows, the same set let the third row of Then iterate through the other numbers in the third row to see if there is a maximum number in the array less than c[]. Replace. And so on Find the end of line m-1, when the array c[] is the array of the topic requirements. source of thought: Http://www.tuicool.com/articles/VjuYFnThe program presses a little bit and may be almost readable
1#include"bits/stdc++.h"2 #defineMem (A, B) memset (A,b,sizeof (a))3 #defineF (Z,x,y) for (z=x;z<=y;z++)4 using namespacestd;5typedefLong LongLL;6 Const intmax=2005;7 intCAs;8 intn,m;9 intS[max],sum[max];Ten structque{ One inth[max* -]; A intN; - Que () { -Mem (H,0); then=0; - } - voidHeapify (intx) { - intchild=x*2, key=H[x]; + while(child<=N) { - if(Child<n && h[child]1]) child++; + if(Key2;} A Else Break; at } -h[x]=key; - } - voidInsertintkey) { - intx=++N; - while(x>1){ in if(key>h[x/2]) h[x]=h[x/2],x/=2; - Else Break; to } +h[x]=key; - } the voiddel () { * if(n==1) n=0; $ Elseh[1]=h[n--],heapify (1);Panax Notoginseng } - }; the intMain () { +Freopen ("sequence.in","R", stdin); AFreopen ("Sequence.out","W", stdout); the inti,j,k; +scanf"%d",&CAs); - while(cas--){ $scanf"%d%d",&n,&m); $Mem (S,0), Mem (SUM,0); - Que Q; -F (I,1, M) { thescanf"%d", s+i); - }WuyiF (I,2, N) { theF (J,1, M) -scanf"%d", sum+j); WuF (J,1, M) -Q.insert (sum[1]+s[j]); AboutF (J,2, M) $F (k,1, M) { - intmx=sum[j]+S[k]; - if(mx<q.h[1]){ - Q.del (); A Q.insert (MX); + } the } -F (J,1, M) $s[j]=Q.h[j]; theMem (Q.h,0); theq.n=0; the } theSort (s+1, s+m+1); -F (I,1, M) { inprintf"%d", S[i]); the } theprintf"\ n"); About } the return 0; the}
POJ-2442 Sequence (handwritten heap optimization)