A closest product

3.9

7 votes
Graphs, Algorithms, Shortest Path Algorithms
Problem

You are given two arrays A and B, both of size N, and an integer T. You must re-arrange the elements in both the arrays (ie, apply any permutation in A, and then apply any permutation in B), in such a way that their inner product S is as close as possible from T.

 

The inner product is defined as S =  A[1]*B[1] + A[2]*B[2] + … + A[N]*B[N]

 

This is an approximation problem, so you are not asked to find the exact answer. Instead, for each case your scoring will be proportional to

|S-T|/N

and you are asked to minimize the sum of the scores over all testcases.

Constraints:

There are 4 testcases in this problem:

1: N = 10

2: N = 50

3: N = 1.000

4: N = 100.000

A and B are randomly generated in all tests

All the elements in the array are between 1 and 10^6

Input:

First line of the input has integers N and T.

The following two lines contain, each, N integers, indicating one of the arrays.

Output:

Print 2 lines containing N integers each, indicating the reordering of the respective arrays.

 

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

-

Editor Image

?