A matrix of K * k is given, and each row takes a number to form the sum of K numbers. There are K ^ K possibilities in total, and the first K is the smallest.
I made a mistake at the beginning, because as long as I ordered each line, the smallest must be the sum of the first column. Then I thought about it and gradually promoted it, change the number of rows to the number of columns in the lower column each time. Of course, the spacing is the minimum. Obviously, this is not the case. In this way, each number is used only once, and the meaning of the question is obviously repeated multiple times.
Then we can see it as two rows, that is, processing two rows at a time, leaving the minimum K rows to continue processing with the row read next time. This method is quite powerful, but there is a difficulty: how can we quickly get the first K in the two rows? I tried to calculate it all over again, k ^ K .. Timeout .. After thinking about it for a long time, I didn't come up with a more effective method. Later I saw a very embarrassing method, and it was easy to prove that
The first row of a [0] to a [K-1] respectively and the second row of B [0] constitute the earliest K number, at present, I only know that a [0] + B [0] is the smallest, and the rest are not sure, but I can be sure that the second is small, it must be a [0] + B [1] or one of the above number of K-1, that is to say, I put these numbers into the priority queue, and then each pop out the smallest number, output, then push the next of the smallest number into the queue, then the next Pop must be the second smallest... In this way, repeat K times to obtain the first K small numbers.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;struct node{ int s,b; bool operator < (const node& rhs) const{ return s>rhs.s; }};priority_queue<node> q;const int N=800;int n;int A[N];int B[N];void solve(){ while (!q.empty()) q.pop(); for (int i=0;i<n;i++){ q.push((node){A[i]+B[0],0}); } int cnt=0; while (cnt<n){ node tmp=q.top(); q.pop(); A[cnt++]=tmp.s; q.push((node){tmp.s-B[tmp.b]+B[tmp.b+1],tmp.b+1}); }}int main(){ while (scanf("%d",&n)!=EOF) { while (!q.empty()) q.pop(); for (int i=0;i<n;i++){ scanf("%d",&A[i]); } for (int i=1;i<n;i++){ for (int j=0;j<n;j++){ scanf("%d",&B[j]); } sort(B,B+n); solve(); } printf("%d",A[0]); for (int i=1;i<n;i++){ printf(" %d",A[i]); } puts(""); } return 0;}