2

I've just started to learn programming so I'm asking for your understanding ;) the checking program accepts only a few tests on the rest throws the error illegal syscall and I have no ideas how to "bite it".

task: (ATTENTION, the program should be memory-saving )write a program that will find in the square matrix a diagonal with the largest sum of elements diagonals (only right). like in the picture https://i.stack.imgur.com/Cex9o.jpg

MAX TIME:1s, MAX memory use 3MB; input: In the first row there is a natural number n (not greater than 1000) denoting the size of the matrix A. In each of the following n lines there is a sequence of n integers (from the range -10000..10000) - these are the elements of the next row of matrix A.

output: You should write two numbers:

*the number of the diagonal matrix A, with the largest sum of elements (if there are several such numbers, the smallest of them should be printed), *the value of this sum.

like this: https://i.stack.imgur.com/bM7AP.jpg

I suspect that the program exceeds the required 3MB memory, so it does not pass a few tests and throws an illegal syscall error. I have no ideas how to solve this task better so please help me ;)

#include <iostream>

using namespace std;

int main(){
    short length;
    short counter=0;
    short which=0;
    int largest_sum=-10000000;
    int sum=0;

    cin>>length;
    //matrix declaration
    short **matrix = new short*[length];

    for(short row=0; row<length; row++){
        matrix[row] = new short [length];
    }
    //filling the matrix
    for(short row=0; row<length; row++){
        for(int column=0; column<length; column++){
            cin>>matrix[row][column];
        }
    }
    //calculating the sum of diagonals and choosing the largest one (half of all diagonals)
    for(short row=length-1; row>=0; row--){
        short r=row;
        short c=0;
        while(r<length){
            sum+=matrix[r][c];
            r=r+1;
            c=c+1;
        }
        ++counter;

        if(sum>largest_sum){
            largest_sum=sum;
            which=counter;
        }
        sum=0;
    }
    //calculating the sum of diagonals and choosing the largest one (second half of all diagonals)
    for(short row=1; row<length; row++){
        short r=0;
        short c=row;
        while(c<length){
            sum+=matrix[r][c];
            r=r+1;
            c=c+1;
        }
        ++counter;
        if(sum>largest_sum){
            largest_sum=sum;
            which=counter;
        }
        sum=0;
    }
    //removing from memory
    for(short i=0; i<length; i++){
        delete [] matrix[i];
    }
    delete [] matrix;
    //score
    cout<<which<<" "<<largest_sum;

    return 0;
}

my code: https://ideone.com/6Qd1yF

MencX
  • 23
  • 2

1 Answers1

1

some hints to improve current solution: (memory consumption ~ 2 MB)

  • use std::vector or std::array (you do not need new and delete then)
  • create a seperate function where you give the coordinates of the first element and get the diagonal sum

alternative solution: (memory consumption ~10 kB)

  • if you look closeley you see that each entry only contributes to one of the diagonals
  • think of a function that gives you the number of the diagonal for each pair of coordinates
  • while reading the matrix, just add the value to the sum of its corresponding diagonal (you do not need to store the actual matrix)
  • in the end just pick the biggest sum
skeller
  • 1,151
  • 6
  • 6
  • I came up with something like that. This time there are no seg errors or illegal syscall errors, but the program exceeds the time limit in the last tests. Can I speed it up somehow or I need to use any matrix properties to make the algorithm in the time limit? – MencX May 12 '18 at 00:05
  • you can safe the loop (ln 12-15) by using a std::vector for your sums. – skeller May 12 '18 at 10:45
  • also you can move ln18 (declaration of number) to ln 15. what is your time limit and by how much do you exceed it? – skeller May 12 '18 at 10:51
  • but the biggest performance problem is your input. see: https://stackoverflow.com/questions/15036878/c-fastest-cin-for-reading-stdin or https://stackoverflow.com/questions/15163751/speed-up-integer-reading-from-file-in-c – skeller May 12 '18 at 10:55
  • thx bro, it's working with scanf/printf and passes all tests ;) – MencX May 12 '18 at 17:57