Maximum Profit Aizu

Maximum Profit

You can obtain profits from foreign exchange margin transactions.
你可以从外汇保证金交易中获得利润。
For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen.
例如,如果你以每美元100日元的汇率购买1000美元,然后以每美元108日元的汇率出售,你可以得到(108-100)×1000=8000日元。

Write a program which reads values of a currency Rt at a certain time t (t=0,1,2,…n−1), and reports the maximum value of Rj−Ri where j>i .
编写一个程序,在特定时间t(t=0,1,2,…n−1)读取货币rt的值,并报告rj−ri的最大值,其中j>i。

Input

The first line contains an integer n. In the following n lines, Rt (t=0,1,2,…n−1) are given in order.
第一行包含一个整数n。在下面的n行中,rt(t=0,1,2,…n−1)按顺序给出。

Output

Print the maximum value in a line.
打印一行中的最大值。

Constraints

2≤n≤200,000
1≤Rt≤109

Sample Input 1

6
5
3
1
3
4
3

Sample Output 1

3

Sample Input 2

3
4
3
2

Sample Output 2

-1

code

/*
                                ^....0
                               ^ .1 ^1^
                               ..     01
                              1.^     1.0
                             ^ 1  ^    ^0.1
                             1 ^        ^..^
                             0.           ^ 0^
                             .0            1 .^
                             .1             ^0 .........001^
                             .1               1. .111100....01^
                             00             ^   11^        ^1. .1^
                             1.^                              ^0  0^
                               .^                                 ^0..1
                               .1                                   1..^
                             1 .0                                     ^  ^
                            ^ 00.                                     ^^0.^
                             1 ^ 0                                     ^^110.^
                           0.   0 ^                                    ^^^10.01
                   ^^     010^   1 1                                   ^^^1110.1
                   0001  10 0   ^ 1.1                                   ^^^1111110
                   0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                   10  10^ 0^                                             ^^111^^^0.1^       1....^
                    11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                    1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                   10   00 11                                               ^^^^^   1 0           1.
                   0^  ^0  ^0                                                ^^^^    0            0.
                   0^  1.0  .^                                               ^^^^    1 1          .0
                   ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                   1 ^      11                             1.                ^^^     ^ ^        ..^
                  ^..^      ^1                             ^.^               ^^^       .0       ^.0
                  0..^      ^0                              01               ^^^       ..      0..^
                 1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                ^  1.        00                              0.             ^^^        ^.0 ^.1
                . 0^.        ^.^                             ^.^            ^^^         ..0.0
               1 .^^.         .^                  1001        ^^            ^^^         . 1^
               . ^ ^.         11                0.    1         ^           ^^          0.
                0  ^.          0              ^0       1                   ^^^          0.
              0.^  1.          0^             0       .1                   ^^^          ..
              .1   1.          00            .        .1                  ^^^           ..
             1      1.         ^.           0         .^                  ^^            ..
             0.     1.          .^          .         0                                  .
             .1     1.          01          .        .                                 ^ 0
            ^.^     00          ^0          1.       ^                                 1 1
            .0      00           .            ^^^^^^                                   .
            .^      00           01                                                    ..
           1.       00           10                                                   1 ^
          ^.1       00           ^.                                            ^^^    .1
          ..        00            .1                                        1..01    ..
         1.1         00           1.                                       ..^      10
        ^ 1^         00           ^.1                                      0 1      1
        .1           00            00                                       ^  1   ^
         .           00            ^.^                                        10^  ^^
       1.1           00             00                                              10^
       ..^           1.             ^.                                               1.
      0 1            ^.              00                 00                            .^
        ^            ^.              ^ 1                00   ^0000^     0               01
     1 0             ^.               00.0^              ^1.0.1   1.00.1              11
     . 1              0               1^^0.01                      ^^^                01
      .^              ^                1   1^^                                       ^.^
    1 1                                                                              0.
    ..                                                                              1 ^
     1                                                                               1
   ^ ^                                                                             .0
   1                                                                             ^ 1
   ..                                                          1.1            ^0.0
  ^ 0                                                           1..01^^100000..0^
  1 1                                                            ^ 1 ^^1111^ ^^
  0 ^                                                             ^ 1      1000^
  .1                                                               ^.^     .   00
  ..                                                                1.1    0.   0
  1.                                                                  .    1.   .^
  1.                                                                 1    1.   ^0
 ^ .                                                                 ^.1 00    01
 ^.0                                                                  001.     .^
 */
// Virtual_Judge —— Maximum Profit Aizu - ALDS1_1_D.cpp created by VB_KoKing on 2019,04,24,19.
/* Procedural objectives:

 Procedural thinking:
 在下标自增的过程中,将现阶段rt最小值保存下来。
 Functions required by the program:

 Variables required by the program:

*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first hurricane that passed through your ear is you,
The first star you see is also you.
The world I see is all your shadow."

FIGHTING FOR OUR FUTURE!!!
*/
#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
    int n;
    cin>>n;

    int rt[200000];
    memset(rt,0, sizeof(rt));

    for (int i = 0; i < n; ++i) {
        cin >> rt[i];
    }

    int maxv=-2000000000,minv=rt[0];
    for (int i = 1; i < n; i++) {       
        maxv=max(maxv,rt[i]-minv);      //更新最大值
        minv=min(minv,rt[i]);           //暂存现阶段的最小值
    }
    
    cout<<maxv<<endl;

    return 0;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12338411.html