Soluciones y Explicación de los problemas ACM ... - ICPC Bolivia
Soluciones y Explicación de los problemas ACM ... - ICPC Bolivia
Soluciones y Explicación de los problemas ACM ... - ICPC Bolivia
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Explicaciones a <strong>los</strong> <strong>problemas</strong> South American Regionals 2008 25<br />
Programa Java que soluciona el problema<br />
import java.util.Scanner;<br />
import static java.lang.Math.max;<br />
class candy {<br />
public static void main(String[] args) {<br />
Scanner in = new Scanner(System.in);<br />
int M, N;<br />
while(true){<br />
M = in.nextInt();<br />
N = in.nextInt();<br />
if(M == 0 && N == 0) break;<br />
int[][] D = new int[M][N + 2];<br />
for (int i = 0; i < M; i++) {<br />
for (int j = 0; j < N; j++) {<br />
D[i][j + 2] = in.nextInt();<br />
}<br />
}<br />
for (int i = 0; i < M; i++) {<br />
for (int j = 2; j < (N + 2); j++) {<br />
D[i][j] = max(D[i][j - 1], D[i][j] + D[i][j - 2]);<br />
}<br />
}<br />
int[] sol = new int[M + 2];<br />
for (int i = 0; i < M; i++) {<br />
sol[i + 2] = D[i][N + 1];<br />
}<br />
for (int i = 2; i < (M + 2); i++) {<br />
sol[i] = max(sol[i - 1], sol[i] + sol[i - 2]);<br />
}<br />
System.out.println(sol[M + 1]);<br />
}<br />
}<br />
}<br />
Programa C que soluciona el problema<br />
#inclu<strong>de</strong> <br />
#<strong>de</strong>fine max(_x,_y) ((_x) > (_y) ? (_x) : (_y))<br />
int main(void)<br />
{<br />
int N,M,i,j,a,b,c,A,B,C;