Advertisement
Mihai_Preda

Untitled

Feb 18th, 2021
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <fstream>
  2. #define NMAX 501
  3.  
  4. using namespace std;
  5. int mat[NMAX + 1][NMAX + 1], sum[NMAX + 1][NMAX + 1];
  6.  
  7. int suma_sub(int i, int j, int iend, int jend){
  8.   return sum[iend][jend] - sum[i-1][jend] - sum[iend][j - 1] + sum[i - 1][j - 1];
  9. }
  10.  
  11. ifstream fin ("cruce.in");
  12. ofstream fout ("cruce.out");
  13.  
  14. int main(){
  15.  
  16.   int n, k, l1, c1, l2, c2, maxsum;
  17.   fin >> n >> k;
  18.  
  19.   for (l1 = 1; l1 <= n; l1++)
  20.     for (c1 = 1; c1 <= n; c1++){
  21.       fin >> mat[l1][c1];
  22.     }
  23.  
  24.   for (l1 = 1; l1 <= n; l1++)
  25.     for (c1 = 1; c1 <= n; c1++){
  26.       sum[l1][c1] = sum[l1 - 1][c1] + sum[l1][c1 - 1] - sum[l1 - 1][c1 - 1] + mat[l1][c1];
  27.     }
  28.   maxsum = -1500000005;
  29.   int i,j;
  30.   for (l1 = 1; l1 <= n-k+1; l1 ++)
  31.     for (c1 = 1; c1 <= n-k+1; c1 ++){
  32.       l2 = l1 + k - 1;
  33.       c2 = c1 + k - 1;
  34.       int val = suma_sub(l1, 1, l2, n) + suma_sub(1, c1, n, c2) - suma_sub(l1 , c1, l2, c2);
  35.       if (maxsum < val){
  36.         maxsum = val;
  37.         i = l1;
  38.         j = c1;
  39.       }
  40.     }
  41.   fout << maxsum << " " << i << " " << j;
  42.   return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement