Aoj ALDS1_1_D Maximum Profit

本文介绍了一种O(n)算法,用于解决外汇交易中的最大利润问题。通过一次遍历历史汇率记录,找到任意两个交易日间可能获得的最大差价。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:戳我

Description

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读取货币Rt的值(t=0,1,2,…n -1),并报告Rj−Ri的最大值(j>I)。

Input

The first line contains an integer n. In the following n lines, Rt (t=0,1,2,…n−1t=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≤10^9
Sample Input
6
5
3
1
3
4
3
3
4
3
2
Sample Output
3
-1
Analyze

给定一组数据,求它们之间的最大差值。

这里给出一种 O(n) 的算法。

根据题目条件, Rj−Ri where j>i ,以第一组数据为例:

5 3 1 3 4 3 这组数据,就有
3 - 5 = -2

1 - 3 = -2
1 - 5 = -4

3 - 1 = 2
3 - 3 = 0
3 - 5 = -2

4 - 3 = 1
4 - 1 = 3
4 - 3 = 1
4 - 5 = -1

3 - 4 = -1
3 - 3 = 0
3 - 1 = 2
3 - 3 = 0
3 - 5 = -2

所以最大差值就是 3, 
对于 A - S = N 这个算式来说,A不变的情况下,S越小,差值N就越大,所以上述步骤可简化为

3 - 5 = -2

1 - min(3,5) = -2

3 - min(1,3,5) = 2

4 - min(3,1,3,5) = 3

3 - min(4,3,1,3,5) = 2
Code
#include <iostream>
#include <limits.h>
#include <cstdio>
using namespace std;

int r[200010];

int main()
{
    int n;

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

        int Max = INT_MIN;
        int Min = r[0];
        for(int i=1; i<n; i++)
        {
            Max = max(Max, r[i] - Min);
            Min = min(Min, r[i]);

        }

        cout << Max << endl;
    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值