CodeJam05
Me esta yendo de la patada. Ya se me acabo el tiempo y no he resuelto el segundo problema.
Eso me pasa por tratar de concursar en una maquina con win98 y procesador PII a 450 Mhz. Truena a cada rato y la lap anda indispuesta.
Desafortunadamente solo pude mandar el primer problema (10 mins). Ya que para el segundo solo daban 37 minutos y el windows tronó dos veces sin darme oportunidad de guardar el código. De todas maneras,aqui van mis respuestas.
Primer problema
Problem Statement
When a web client (like a browser) requests a particular web page, it typically replaces certain characters with escape sequences. For instance, spaces are replaced with "%20". Your task is to reverse this, replacing every escape sequence in the input with the character it represents. Each escape sequence is formatted as "%XX" where XX is the ASCII value of the escaped character in hexadecimal.
Definition
Class:
URLParser
Method:
parse
Parameters:
String
Returns:
String
Method signature:
String parse(String url)
(be sure your method is public)
Constraints
-
url will contain between 1 and 50 characters, inclusive.
-
Each '%' in the input will be followed by a hexadecimal value between 20 (hex) and 7E (letters will be uppercase).
-
Each character in the input will have ASCII value between 32 and 126, inclusive.
Examples
0)
"http://www.%20%40%20%40%20.com/%25"
Returns: "http://www. @ @ .com/%"
"%20" is the escape sequence for ' ', while "%40" stands for '@' and "%25" stands for '%'.
1)
"%20%21%22%23%24%25%26%27%28"
Returns: " !\"#$%&'("
2)
"%48%65%6C%6C%6F%20%57%6F%72%6C%64%21"
Returns: "Hello World!"
3)
"%2525"
Returns: "%25"
public class URLParser
{
public String parse(String url)
{
String retorna="";
for(int i=0;i
if(url.charAt(i)=='%')
retorna+=valor(url.charAt(++i),url.charAt(++i));
else retorna+=url.charAt(i);
}
return retorna;
}
char valor(char a, char b)
{
return (char)(hexchar(a)*16+hexchar(b));
}
char hexchar(char a)
{
return (char)(a>='0'&&a<='9'?a-'0':a-'A'+10);
}
}
Segundo problema
Problem Statement
You are given a String[], grid, representing a city where each character in grid is a single city block. Each block will contain a digit representing the relative population on that block. For example, a block containing the digit '6' will contain three times as many people as a block containing the digit '2'. You are also given a String[], stations, containing the locations of all the fire stations within the city. Each element of stations is formatted as "r c" (quotes for clarity only), where r and c represent the row and column, respectively, of the block on which a fire station is located. Character j of element i of grid represents the block at row i, column j. All indices are 0-based.
The city has received enough funds to build one additional fire station, and the mayor has decided that it is most important to minimize the average distance between a person and the closest fire station to that person. The metric used to determine the distance between two locations in the city is the Manhattan distance between the two blocks on which the locations are situated. The Manhattan distance between two blocks (r1, c1) and (r2, c2) is |r1-r2|+|c1-c2| (the vertical bars represent absolute value). Determine the block on which the new station should be built and return its row and column formatted as "r c" (quotes for clarity only). The return String should contain no extra leading zeros. If multiple blocks are equally optimal, return the one with the lowest row, and if multiple optimal blocks have the same lowest row, return the one among them with the lowest column. If adding an additional fire station would not reduce the average distance between a person and the closest fire station to that person, return the empty String ("").
Definition
Class:
NewStation
Method:
location
Parameters:
String[], String[]
Returns:
String
Method signature:
String location(String[] grid, String[] stations)
(be sure your method is public)
Constraints
-
grid will contain between 1 and 20 elements, inclusive.
-
Each element of grid will contain between 1 and 20 characters, inclusive.
-
Each element of grid will contain exactly the same number of characters.
-
Each element of grid will contain only digits ('0'-'9').
-
At least one character in grid will be non-zero.
-
stations will contain between 1 and 10 elements, inclusive.
-
Each element of stations will be formatted as "r c" (quotes for clarity only), where r and c are each integers between 0 and 19, inclusive, with no leading zeros.
-
Each element of stations will represent a location within the boundaries of grid.
Examples
0)
{"111",
"111",
"111"}
{"1 1"}
Returns: "0 1"
There's an existing station at (1, 1) and each block contains exactly the same number of people. Placing a new station at either (0, 1), (1, 0), (1, 2), or (2, 1) would minimize the average distance. (0, 1) is chosen since it has the lowest row. Adding the new station reduces the average distance from approximately 1.33 to 1.0. The distance from each block to the nearest station becomes:
101
101
212
1)
{"111",
"111",
"111"}
{"0 0", "0 1", "0 2",
"1 0", "1 1", "1 2",
"2 0", "2 1", "2 2"}
Returns: ""
There's a fire station on every block, so adding a new station would not reduce the average distance.
2)
{"2312",
"0233"}
{"1 3"}
Returns: "0 1"
Placing a fire station at (0, 1) would make the average distance 0.625.
3)
{"2312",
"0233"}
{"1 1", "1 1"}
Returns: "0 3"
Respuesta
public class NewStation
{
int[][] popu;
int[][] dist;
int[][] temp;
public String location(String[] grid, String[] stations)
{
//Construir matrices
popu= new int[grid.length][grid[0].length()];
dist= new int[grid.length][grid[0].length()];
temp= new int[grid.length][grid[0].length()];
for(int i=0;i
popu[i][j]=(grid[i].charAt(j)-'0');
dist[i][j]=mD(i,j,stations);
}
float avg=(Average(popu,dist));
String[] stations2=new String[stations.length+1];
int r=-1; int c=-1;
for(int k=0;k
for(int a=0;a
stations2[stations.length]=String.valueOf(a)+" "+String.valueOf(b);
for(int i=0;i
temp[i][j]=mD(i,j,stations2);
}
float avg2=Average(popu,temp);
if(avg2
avg=avg2;
r=a;
c=b;
}
}
return r==-1?"":String.valueOf(r)+" "+String.valueOf(c);
}
int MD(int r1,int c1, int r2, int c2)
{
return (Math.max(r1,r2)-Math.min(r1,r2))+(Math.max(c1,c2)-Math.min(c1,c2));
}
int mD(int r1, int c1, String[] stations)
{
String aaa[];
int min=1000000;
for(int i=0;i
aaa=stations[i].split(" ");
min=Math.min(min,MD(r1,c1,Integer.parseInt(aaa[0]),Integer.parseInt(aaa[1])));
}
return min;
}
float Average( int[][] pop, int[][] dis)
{
float total=0,muls=0;
for(int i=0;i
total+=pop[i][j];
muls+=pop[i][j]*dis[i][j];
}
return muls/total;
}
}

Nada más la Puntita by Dan Alonso is licensed under a Creative Commons Reconocimiento-Compartir bajo la misma licencia 2.5 México License.
Permissions beyond the scope of this license may be available at http://dan-alonso.org/trabajos-derivados.
Todos los personajes, hechos y lugares son ficticios.
Cualquier parecido con la realidad es mera coincidencia. Las opiniones, entrevistas y comentarios aquí expresadas son responsabilidad de su respectivo autor y no representan la opinión de dan-alonso.org ni de sus socios o afiliados.
Comments
El for del primer problema no está mocho?
I like ur code.
Posted by: Yanina | 23 de Agosto 2005 a las 04:49 PM
Pues el "i<" lo interpreta como html... si ves el código fuente podras verlo entero
Posted by: Dan | 23 de Agosto 2005 a las 04:57 PM
Hay que codificar tu html
Posted by: Luis Medina | 25 de Agosto 2005 a las 12:21 PM