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
https://www.spoj.pl/problems/FASHION/
public class FashionShows { public static void main( String args[] ) throws Exception { int numCasos = sig(); int[] varones, mujeres; int numParejas; int cantVarones, hotnessMujer, producto; for( int i = 0; i < numCasos; i ++ ) { numParejas = sig(); // Almacena los datos de cada varon y mujer varones = leerDatos( numParejas ); mujeres = leerDatos( numParejas ); producto = 0; hotnessMujer = 10; /* * Algorimo para calcular la suma maxima de los productos * de los hotness de cada varon y mujer */ for( int j = 10; j >= 0; j -- ) { // Cantidad de varones con hotness igual a j cantVarones = varones[ j ]; if( cantVarones == 0 ) continue; while( cantVarones > 0 ) { // Si hay una cantidad suficiente de mujeres con hotness igual a 'hotnessMujer' // entonces anadimos el producto de los varones con hotnees igual a 'j', por // las mujeres con hotness igual a 'hotnessMujer' if( mujeres[ hotnessMujer ] >= cantVarones ) { producto = producto + cantVarones * j * hotnessMujer; mujeres[ hotnessMujer ] = mujeres[ hotnessMujer ] - cantVarones; cantVarones = 0; } // Si no hay una cantidad suficiente de mujeres igual a 'hotnessMujer' // entonces anadimos el producto de los varones con hotnees igual a 'j' por // las mujeres que estan disponibles con hotnees igual a 'hotnessMujer' // Para los varones restantes buscamos mujeres con un hotnees menor else { producto = producto + mujeres[ hotnessMujer ] * j * hotnessMujer; cantVarones = cantVarones - mujeres[ hotnessMujer ]; hotnessMujer --; } } } System.out.println( producto ); } } public static int[] leerDatos( int numDatos ) throws Exception { int[] datos = new int[ 11 ]; int val, car; for( int j = 1; j < numDatos; j ++ ) { val = 0; car = System.in.read(); while( car != 32 ) { val = 10 * val + ( car - 48 ); car = System.in.read(); } datos[ val ] ++; } val = 0; car = System.in.read(); while( car != 10 ) { val = 10 * val + ( car - 48 ); car = System.in.read(); } datos[ val ] ++; return datos; } public static int sig() throws Exception { int val = 0; int car = System.in.read(); val = car - 48; car = System.in.read(); while( car != 10 ) { val = 10 * val + ( car - 48 ); car = System.in.read(); } return val; } }
No hay comentarios:
Publicar un comentario