Concise one-Kazakhstan problem is: give two length n N of the array A and B, and then a[i]+b[i] a [i] + b [i] have N2 N 2 combinations, the smallest first n n combinations
Like n=4 N = 4
a:1,2,3,4 a:1, 2, 3, 4
b:5,6,7,8 B:5, 6, 7, 8
The first n n smallest is 6,7,7,8 6, 7, 7, 8.
Arrays are sorted first ha
Let's start with an initial answer: what is called the initial answer.
For example, each of the number a[i] a [i] in a a [i] all plus the smallest b[1] b [1] in B array, is almost the smallest n n number
This n number is 6,7,8,9 6, 7, 8, 9, right?
But there's a problem here.
The smallest number is 6, but the second small number has two combinations: a[2]+b[1] A [2] + b [1] and a[1]+b[2] a [1] + b [2], then this a[1]+b[2] a [1] + b [2] should be used as The third small is right, and this third small but did not calculate, how to do nan.
We'll get our initial answers first in the priority queue, small in the team head, the team head is the smallest combination, and the smallest to be used for optimization, such as the current smallest is a[1]+b[1] a [1] + b [1], then b[1] b [1] with b[2] b [2] for Off, is also a relatively small combination, then put this combination into the queue, compared with other, anyway the smallest each time is the team first ~
/* To two of the length n of the array A and B, so a[i]+b[i] There are n^2 combinations, the
smallest of the first n combinations
* * * *
#include "iostream"
#include "algorithm"
#include "queue"
using namespace std;
const int maxn=1e6+5;
int A[MAXN],B[MAXN],C[MAXN];
int N;
struct AAA
{
int v,id;
AAA () {}
aaa (int v,int ID): V (v), ID (ID) {}
bool operator< (const AAA &a) const
{return
A.V <v;
}
;
int main ()
{while
(cin>>n)
{
priority_queue<aaa>que;
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++) cin>>b[i];
Sort (a+1,a+1+n);
Sort (b+1,b+1+n);
for (int i=1;i<=n;i++) Que.push (AAA (b[1]+a[i],1));
for (int i=1;i<=n;i++)
{
AAA t=que.top ();
Que.pop ();
C[I]=T.V;
T.V=T.V-B[T.ID]+B[T.ID+1];
t.id++;
Que.push (t);
}
for (int i=1;i<=n;i++) cout<<c[i]<< "";
cout<<endl;
}
}
And the problem is to repeat it a few times and it's over.
#include "iostream" #include "Cstdio" #include "algorithm" #include "queue" using namespace std;
const int maxn=750+5;
Long Long A[MAXN],B[MAXN];
Long Long N;
struct AAA {long long v,id; AAA () {} AAA (Long long V,long long ID): V (v), ID (ID) {} bool operator< (const AAA &a) Const {retur
N a.v<v;
}
};
int F () {priority_queue<aaa>que;
for (int i=1;i<=n;i++) {Que.push (AAA (b[1]+a[i],1));
for (int i=1;i<=n;i++) {AAA t=que.top ();
Que.pop ();
A[I]=T.V;
T.V=T.V-B[T.ID]+B[T.ID+1];
t.id++;
Que.push (t);
int main () {while (cin>>n) {int t=n-1;
for (int i=1;i<=n;i++) cin>>a[i];
Sort (a+1,a+1+n);
while (t--) {for (int i=1;i<=n;i++) cin>>b[i];
Sort (b+1,b+1+n);
f (); for (int i=1;i<n;i++) cout<<a[i]<< "";
cout<<a[n]<<endl;
}
}