domingo, 16 de octubre de 2011

Girls and Boys

There are G girl students and B boy students in a class that is about to graduate. You
need to arrange them in a single row for the graduation. To give a better impression of
diversity, you want to avoid having too many girls or too many boys seating consecutively.
You decided to arrange the students in order to minimize the gender regularity. The
gender regularity of an arrangement is the maximum number of students of the same
gender (all girls or all boys) that appear consecutively.
Given G and B, calculate the minimum gender regularity among all possible arrangements.

Input


Each test case is described using a single line. The line contains two integers G and B
representing the number of girls and boys in the class, respectively (0 ≤ G, B ≤ 1000).
The end of input is indicated with a line containing the number −1 twice.

Output


For each test case, output a single line with a single integer representing the minimum
gender regularity that an arrangement of G girls and B boys can have.

Example
Input:
10 10
5 1
0 1000
-1 -1


Output:
1
3
1000

http://www.spoj.pl/problems/GIRLSNBS/



Sean:
     g: # girls
     b: # boys

Al leer los valores de g (# girls) y b (# boys) pueden ocurrir que nos encontremos en cualquiera de los siguientes casos:

Caso 1: g > b

     Para obtener la solucion minima, debemos de ubicar primero a  los boys:

     ___ b ___ b ___ b ___ ... ___ b ___
      |     |     |     |       |     |
      1     2     3     4       b    b+1 

     En la fila se generan b+1 grupos, en donde podremos ubicar a cada una de las  
     girls, para obtener una solución minima debemos distribuir a las girls de tal 
     manera que en cada grupo hay un numero similar de ellas.

     -> Min grupo g = ceil( g / ( b + 1 ) )


Caso 2: b > g

     Para obtener la solucion minima, debemos de ubicar primero a 
     las girls:

     ___ g ___ g ___ g ___ ... ___ g ___
      |     |     |     |       |     |
      1     2     3     4       g    g+1

     En la fila se generan g+1 grupos, en donde podremos ubicar a cada una de los
     boys para obtener una solución minima debemos distribuir a las boys de tal 
     manera que en cada grupo hay un numero similar de ellos.

     -> Min grupo b = ceil( b / ( g + 1 ) )


Caso 3: b = g

     Para obtener la solucion minima existe 2 posibles distribucion de los girls y 
     los boys

     g b g b ... g b g b

     b g b g ... b g b g

     -> Min grupo = 1


Observaciones: 

    * Es facil darse cuenta que:

        ceil( g / ( b + 1 ) ) = ( g + b ) / ( b + 1 )
        ceil( b / ( g + 1 ) ) = ( b + g ) / ( g + 1 )

        Lo anterior es para que tratemos solo con datos de tipo entero, lo cual 
        reducira el tiempo de las operaciones aritmeticas.

    * Caso especial

        g = b = 0 -> Min grupo = 0 

public class GirlsAndBoy
{
    public static void main( String args[] ) throws Exception
    {  
        int girls, boys;
        
        // Buffer de caracteres donde se guardara la salida del programa
        char[] salida = new char[ 50000  ];
    
        //  Tamanio de la salida ( cantidad de caracteres )
        int tamanio = 0, longitud;
        String resultado;
    
        // Leemos el numero de mujeres en el grupo
        girls = sig();

        while( girls != -1 )
        {
            // Leemos el numero de varones en el grupo
            boys = sig(); 
   
            // Obtenemos la representacion en cadena del elemento
            resultado = minRegularidad( girls, boys ) + "\n";
            longitud = resultado.length();
   
            // Copiamos los caracteres de la cadena al buffer de salida
            resultado.getChars( 0, longitud, salida, tamanio );
            tamanio = tamanio + longitud;
     
            girls = sig();
        }

        // Mostramos la salida
        System.out.print( new String( salida, 0, tamanio ) );
    }

    public static int minRegularidad( int g, int b )
    {   
        if ( g > b )
            return ( g + b ) / ( b + 1 );
  
        else if( b > g )
            return ( b + g ) / ( g + 1 );
  
        else if( g != 0 ) // Caso en que g == b && g != 0
            return 1;  
  
        return 0;  // Caso en que g == b == 0
    }
 
 
    public static int sig(  ) throws Exception
    {
        int val = 0;
        int car = System.in.read();
  
        // Si el numero comienza con '-' --> Entrada: -1 -1  
        if( car == '-' )
            return -1;
  
        while( car > 47 && car < 58 )
        {
            val = 10 * val + car - 48;
            car = System.in.read();
        }
   
        return val;
    }
}

No hay comentarios:

Publicar un comentario