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

Envío 2262

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

  • Autor: Osvaldo
  • Fecha: 2020-12-07 19:46:38 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.007 s 2 KBi
#2
Correcto
0.007 s 13 KBi
#3
Correcto
0.005 s 1 KBi
#4
Correcto
0.006 s 8 KBi
#5
Correcto
0.006 s 11 KBi
#6
Correcto
0.006 s 7 KBi
#7
Correcto
0.011 s 2 KBi
#8
Correcto
0.06 s 10 KBi
#9
Correcto
0.067 s 2 KBi
#10
Correcto
0.085 s 1 KBi
#11
Correcto
0.077 s 2 KBi
#12
Correcto
0.052 s 2 KBi
#13
Correcto
0.071 s 2 KBi
#14
Correcto
0.074 s 2 KBi
#15
Correcto
0.076 s 2 KBi
#16
Correcto
0.051 s 1 KBi
#17
Correcto
0.062 s 1 KBi
#18
Correcto
0.064 s 2 KBi
Puntos totales: 100 / 100

Código

#include <bits/stdc++.h>
#define ii              pair<int,int>
#define F               first
#define S               second
#define pb              push_back
#define all(x)          (x).begin(),(x).end()
#define rall(x)         (x).rbegin(),(x).rend()
#define fore(i, a, b)   for(int i = a; i < b; i += 1)
#define forr(i, a)      for(int i = a; i >= 0; i--)
#define sz(s)           int(s.size())
#define cls(a,car)      memset(a,car,sizeof (a))
#define db(x)           cout << #x << " is " << x << '\n'
#define angle(x)        double(x * acos(-1) / 180.0)
using namespace std;
void debug(){cout << endl;}
template<typename T, typename... Args>
void debug(T a, Args... args){cout << a << " "; debug(args...);}
typedef long long   ll;
typedef vector<int> vi;
typedef vector<ii>  vii;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;
const double E = 1e-7;
const int oo = 1e9;
void solve(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> v(n, vector<int>(m)), acc(n, vector<int>(m));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> v[i][j];

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            //acc[i][j] = acc[i - 1][j] + ac[i][j - 1] + v[i][j] - acc[i - 1][j - 1];
            acc[i][j] = v[i][j];
            if(i)
                acc[i][j] += acc[i - 1][j];
            if(j)
                acc[i][j] += acc[i][j - 1];
            if(i and j)
                acc[i][j] -= acc[i - 1][j - 1];

        }
    }
    

    int best = INT_MIN;
    for(int x1 = 0; x1 < n; x1++)
        for(int y1 = 0; y1 < m; y1++)
            for(int x2 = x1; x2 < n; x2++)
                for(int y2 = y1; y2 < m; y2++){
                    int cur = acc[x2][y2];
                    if(y1) 
                        cur -= acc[x2][y1 - 1];
                    if(x1)
                        cur -= acc[x1 - 1][y2];
                    if(x1 and y1)
                        cur += acc[x1 - 1][y1 - 1];
                    best = max(best, cur);
                }
    cout << best << '\n';

}
int main(){
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
       ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #endif

    int t = 1;
    //cin >> t;
    for(int i = 1; i <= t; i++)
        solve();

    #ifdef LOCAL
        cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}