█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 2816

Problema 0xa6 - Submatriz de suma máxima en una matriz no muy grande

  • Autor: pradomaricon
  • Fecha: 2021-02-07 00:03:16 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.156 s 16 KBi
#2
Correcto
0.188 s 20 KBi
#3
Incorrecto
0.148 s 15 KBi
#4
Correcto
0.157 s 15 KBi
#5
Correcto
0.153 s 16 KBi
#6
Correcto
0.191 s 18 KBi
#7
Correcto
0.151 s 16 KBi
#8
Incorrecto
0.234 s 22 KBi
#9
Correcto
0.19 s 16 KBi
#10
Correcto
0.188 s 16 KBi
#11
Correcto
0.166 s 16 KBi
#12
Correcto
0.182 s 16 KBi
#13
Correcto
0.195 s 16 KBi
#14
Correcto
0.194 s 16 KBi
#15
Correcto
0.203 s 16 KBi
#16
Correcto
0.198 s 17 KBi
#17
Correcto
0.184 s 16 KBi
#18
Correcto
0.228 s 23 KBi
Puntos totales: 89 / 100

Código

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //input
        String[] dimensiones = leer.nextLine().trim().split(" ");

//        for (String item : dimensiones) {
//            System.out.println(item);
//        }

        int filas = Integer.parseInt(dimensiones[0]);
        int columnas = Integer.parseInt(dimensiones[1]);
        int[][] ejemplo1 = new int[filas][columnas];

        for (int i = 0; i < filas; i++) {
            String[] filaDada = leer.nextLine().trim().split(" ");
            int k = 0;
            for (int j = 0; j < columnas; j++) {

                ejemplo1[i][j] = Integer.parseInt(filaDada[k]);

                k++;
                //System.out.println(ejemplo1[i][j]);
            }
        }

        int toret = valorSubmatrizMaxima(ejemplo1, filas, columnas);

        System.out.println(toret);

    }

    public static Scanner leer = new Scanner(System.in);

    public static int valorSubmatrizMaxima(int[][] matriz, int filas, int columnas) {

        //crear matriz con suma de columnas acumulada
        int[][] matrizSumaColumnas = new int[filas][columnas];
        for (int i = 0; i < columnas; i++) {
            for (int j = 0; j < filas; j++) {
                if (j == 0) {
                    matrizSumaColumnas[j][i] = matriz[j][i];
                } else {
                    matrizSumaColumnas[j][i] = matriz[j][i] + matrizSumaColumnas[j - 1][i];
                }
            }
        }

        //algoritmo como el que teniamos en la busqueda del subarray maximo
        int maximoLocal = Integer.MIN_VALUE;;
        int minimo, subMatriz;
        for (int i = 0; i < filas; i++) {
            for (int j = i; j < filas; j++) {
                minimo = 0;
                subMatriz = 0;
                for (int k = 0; k < columnas; k++) {
                    if (i == 0) {
                        subMatriz += matrizSumaColumnas[j][k];
                    } else {
                        subMatriz += matrizSumaColumnas[j][k] - matrizSumaColumnas[i - 1][k];
                    }
                    if (subMatriz < minimo) {
                        minimo = subMatriz;
                    }
                    if ((subMatriz - minimo) > maximoLocal) {
                        maximoLocal = subMatriz - minimo;
                    }
                }
            }
        }

        return maximoLocal;
    }

}