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

Envío 5115

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

  • Autor: Ikerlb
  • Fecha: 2021-10-16 00:14:53 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.077 s 10 KBi
#2
Correcto
0.066 s 10 KBi
#3
Correcto
0.084 s 10 KBi
#4
Correcto
0.083 s 10 KBi
#5
Correcto
0.065 s 10 KBi
#6
Correcto
0.076 s 10 KBi
#7
Correcto
0.072 s 10 KBi
#8
Correcto
0.127 s 11 KBi
#9
Correcto
0.103 s 11 KBi
#10
Correcto
0.105 s 11 KBi
#11
Correcto
0.105 s 11 KBi
#12
Correcto
0.112 s 11 KBi
#13
Correcto
0.104 s 11 KBi
#14
Correcto
0.141 s 11 KBi
#15
Correcto
0.15 s 11 KBi
#16
Correcto
0.13 s 11 KBi
#17
Correcto
0.119 s 11 KBi
#18
Correcto
0.111 s 16 KBi
Puntos totales: 100 / 100

Código

import java.io.InputStreamReader;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.lang.Math;

public class Main{
    static class Reader{
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader(){
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException{
            din = new DataInputStream(
                new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException{
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') {
                    if (cnt != 0)
                        break;
                    else
                        continue;
                }
                buf[cnt++] = (byte)c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException{
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException{
            bytesRead = din.read(buffer, bufferPointer = 0,
                                 BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException{
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException{
            if (din == null)
                return;
            din.close();
        }
    }

    public static void updateMatrix(int[][] matrix){
        int N = matrix.length;
        int M = matrix[0].length;
        for(int r = 1; r < N; r++)
            matrix[r][0] += matrix[r - 1][0];
        for(int c = 1; c < M; c++)
            matrix[0][c] += matrix[0][c - 1];
        for(int r = 1; r < N; r++)
            for(int c = 1; c < M; c++){
                matrix[r][c] += matrix[r - 1][c];
                matrix[r][c] += matrix[r][c - 1];
                matrix[r][c] -= matrix[r - 1][c - 1];
            }
    }

    public static int querySubmatrixSum(int[][] matrix, int r1, int c1, int r2, int c2){
        if(r1 == c1 && c1 == 0)
            return matrix[r2][c2];
        if(r1 == 0)
            return matrix[r2][c2] - matrix[r2][c1 - 1];
        if(c1 == 0)
            return matrix[r2][c2] - matrix[r1 - 1][c2];
        return matrix[r2][c2] - matrix[r1 - 1][c2] - matrix[r2][c1 - 1] + matrix[r1 - 1][c1 - 1];
    }


    public static void main(String[] args) throws IOException {
        Reader fr = new Reader();
        int N = fr.nextInt();
        int M = fr.nextInt();
        int[][] matrix = new int[N][M];

        int res = (int)Math.pow(-10, 5) - 1;

        for(int r = 0; r < N; r++)
            for(int c = 0; c < M; c++)
                matrix[r][c] = fr.nextInt();
        updateMatrix(matrix);

        for(int r = 0; r < N; r++)
            for(int c = 0; c < M; c++)
                for(int rr = r; rr < N; rr++)
                    for(int cc = c; cc < M; cc++)
                        res = Math.max(res, querySubmatrixSum(matrix, r, c, rr, cc));

        System.out.println(res);
    }
}