D
D
dsrk_dev2014-10-13 10:41:39
Programming
dsrk_dev, 2014-10-13 10:41:39

How to solve a problem in Pascal?

This year, for the third time, a well-known company is holding an artificial intelligence programming competition for gaming strategies. This time, the participants were asked to write artificial intelligence to manage a team of hockey players.
After the strategy was programmed, Vasya sent it to the system. After conducting a series of test fights, she got into the sandbox and began to fight with the strategies of other participants. Each sandbox participant has a rating that shows the success of the submitted solution. After each system battle, it may change. All rating fluctuations can be seen on the participant's personal page in the form of a graph.
Analyzing data is a boring and tedious task, and besides, Vasya is busy writing the next version of his strategy. But he really wants to know the most successful and most disastrous period of performance of his artificial intelligence. Vasya considers a successful period such a period when the rating did not go down, but a failure, respectively, when the rating did not grow. Vasya considers the most successful period to be such a successful period in which the greatest increase in the rating occurred, and considers the most unfortunate period to be the period in which the largest drop occurred. Help Vasya find the rating changes for these periods using the initial data.
Input data format
The first line of the input file contains an integer N (1 ≤ N ≤ 105) — the amount of data. The second line contains N non-negative integers separated by a space, not exceeding 109 — the value of the rating after each game in chronological order.
Output data
output In the output file print two numbers — by how much the rating increased during the most successful period and by how much it fell during the most disastrous one.
Example
input.txt output.txt
3
1 2 4 3 0
5
0 3 5 2 3 5 3
How to solve this? does anyone have any ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Fedoryan, 2014-10-13
@dsrk_dev

Vasya considers a successful period such a period when the rating did not go down, but a failure, respectively, when the rating did not grow. Vasya considers the most successful period to be such a successful period in which the greatest increase in the rating occurred, and considers the most unfortunate period to be the period in which the largest drop occurred. Help Vasya find the rating changes for these periods using the initial data.

It follows from this that you need to go through the input array of numbers from beginning to end. If each subsequent number is greater than the previous one, then this is one of the successful periods. We remember the first number, from which the growth of indicators began, and the last, on which it began to decline. The difference between them will be an indicator of the growth of the rating.

A
Aitym Ramazanov, 2014-10-23
@iTeam91

Can you solve it without an array?

var
  n, a, oldA, nu, np, u, p, i: integer;
begin
  readln(n); read(oldA);
  nu:= 0; np:= 0; u:= 0; p:= 0;
  for i:= 2 to n do
  begin
    read(a);
    if a > oldA then
    begin
      u:= u+a-oldA; p:= 0;
      if nu < u then nu:= u;
    end;
    if a < oldA then
    begin
      p:= p+oldA-a; u:= 0;
      if np < p then np:= p;
    end;
    oldA:= a;
  end;
  writeln(nu, ' ', np);
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question