A
A
Alexander Nekrasov2020-08-16 21:17:41
Pascal
Alexander Nekrasov, 2020-08-16 21:17:41

How to fix a pascal program so that all tests pass?

On the site task number 112371. Saddle points that I can't solve.

My code:

var
    N, M, i, j, abuse: integer;
    matrix: array [1..100, 1..100] of integer;
    min, max: int64;


function max_in_column(j, N: integer): integer;
var
    i: integer;

begin
    for i := 1 to N do
    begin
        if matrix[i, j] >= max then
            max := matrix[i, j];
    end;
    max_in_column := max;
end;

function min_in_string(i, M: integer): integer;
var
    j: integer;

begin
    for j := 1 to M do
        if matrix[i, j] <= min then
            min := matrix[i, j];
    min_in_string := min;
end;

procedure sedlov_dot(j, i, N, M: integer);

begin
    min := 92233720;
    if matrix[i, j] = min_in_string(i, M) then
    begin
        Writeln(i, ' ', j);
        abuse := 1;
    end;
end;

begin
    Read(N, M);
    for i := 1 to N do
        for j := 1 to M do
            Read(matrix[i, j]); 
    abuse := 0;
    for j := 1 to M do
    begin
        max := -92233720;
        for i := 1 to N do
            if matrix[i, j] = max_in_column(j, M) then
                sedlov_dot(j, i, N, M);
    end;
    if abuse = 0 then
        Write(abuse);
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-08-16
@wataru

You have a solution for a cube, although there should be a solution for a square. You are most likely getting a time limit.
Get 2 arrays - min_row and max_column and fill them in one pass using the functions you already have. And only then go over the entire matrix and compare the current element with the already known maximum / minimum.
At the end, also output via writeln. Initialize the maximum/minimum to the largest possible values ​​or the first elements of the row/column. What if all the numbers in the matrix are 92233720+1?
Ps In this context, the line - row, point - point. Saddle point - saddle_point.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question