Algorithm
Problem link- https://www.spoj.com/problems/FASHION/
FASHION - Fashion Shows
A fashion show rates participants according to their level of hotness. Two different fashion shows were organized, one for men and the other for women. A date for the third is yet to be decided ;) .
Now the results of both fashion shows are out. The participants of both the fashion shows have decided to date each other, but as usual they have difficuly in choosing their partners. The Maximum Match dating serive (MMDS) comes to their rescue and matches them in such a way that that maximizes the hotness bonds for all couples.
If a man has been rated at hotness level x and a women at hotness level y, the value of their hotness bond is x*y.
Both fashion shows contain N participants each. MMDS has done its job and your job is to find the sum of hotness bonds for all the couples that MMDS has proposed.
Input
The first line of the input contains an integer t, the number of test cases. t test cases follow.
Each test case consists of 3 lines:
- The first line contains a single integer N (1 <= N <= 1000).
- The second line contains N integers separated by single spaces denoting the hotness levels of the men.
- The third line contains N integers separated by single spaces denoting the hotness levels of the women.
All hotness ratings are on a scale of 0 to 10.
Output
For each test case output a single line containing a single integer denoting the sum of the hotness bonds for all pairs that MMDS has proposed.
Example
Input: 2 2 1 1 3 2 3 2 3 2 1 3 2 Output: 5 15
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t,n,temp1[1000],temp2[1000];
long sum;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
sum=0;
scanf("%d",&n);
for(int j=0;j<n;j++)
{
scanf("%d",temp1+j);
}
for(int j=0;j<n;j++)
{
scanf("%d",temp2+j);
}
sort(temp1, temp1+n);
sort(temp2,temp2+n);
for(int k=0;k<n;k++)
{
sum=sum+temp1[k]*temp2[k];
}
cout<<sum<<endl;
}
}
Copy The Code &
Try With Live Editor
Input
2
1 1
3 2
3
2 3 2
1 3 2
Output
15
#2 Code Example with Java Programming
Code -
Java Programming
import java.util.*;
import java.lang.*;
class Main
{
public static void mergeArrays(int[] left, int[] right, int[] input){
int i = 0, j = 0, k = 0;
while(i < left.length && j < right.length){
if(left[i]>right[j]){
input[k] = right[j];
j++;
}
else{
input[k] = left[i];
i++;
}
k++;
}
if(i > left.length-1){
while(j<=right.length-1){
input[k]=right[j];
j++;
k++;
}
}
else if(j > right.length-1){
while(i<=left.length-1){
input[k]=left[i];
i++;
k++;
}
}
}
public static void mergeSort(int[] input){
if(input.length<2)
return;
int mid = input.length/2;
int[] left = new int[mid];
int[] right = new int[input.length-mid];
for(int i = 0; i<=mid-1; i++){
left[i] = input[i];
}
for(int i = mid; i<=input.length-1; i++){
right[i-mid] = input[i];
}
mergeSort(left);
mergeSort(right);
mergeArrays(left, right, input);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t > 0){
int numOfParticipants = s.nextInt();
long sum = 0;
int[] men = new int[numOfParticipants];
int[] women = new int[numOfParticipants];
for(int i = 0; i < numOfParticipants; i++){
men[i] = s.nextInt();
}
for(int i = 0; i < numOfParticipants; i++){
women[i] = s.nextInt();
}
mergeSort(men);
mergeSort(women);
for(int i = 0; i < numOfParticipants; i++){
sum = sum+(men[i]*women[i]);
}
System.out.println(sum);
t--;
}
}
}
Copy The Code &
Try With Live Editor
Input
2
1 1
3 2
3
2 3 2
1 3 2
Output
15
Demonstration
SPOJ Solution-FASHION Fashion Shows-- Solution in C, C++, Java, Python,FASHION,SPOJ Solution