当前位置: 代码迷 >> 综合 >> Maximum Profit
  详细解决方案

Maximum Profit

热度:12   发布时间:2024-01-26 07:59:30.0

刷题day1

    • 题目
    • 代码

题目

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.

Write a program which reads values of a currency RtRt at a certain time tt (t=0,1,2,…n?1t=0,1,2,…n?1), and reports the maximum value of Rj?RiRj?Ri where j>ij>i .

Input
The first line contains an integer nn. In the following nn lines, RtRt (t=0,1,2,…n?1t=0,1,2,…n?1) are given in order.

Output
Print the maximum value in a line.

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

Sample Input 1

6
5
3
1
3
4
3

Sample Output 1

3

代码

#include <iostream>
#include <algorithm>
using namespace std;
static const int MAX=200000;int main()
{int R[MAX],n;cin>>n;for(int i=0;i<n;i++)cin>>R[i];int maxv=-2000000000;int minv=R[0];for(int i=1;i<n;i++)  //如果每次都在这一阶段直接读取R[i],那么数组可以省去 {maxv=max(maxv,R[i]-minv); //更新最大值 minv=min(minv,R[i]);   //暂存现阶段的最小值 }cout<<maxv<<endl;return 0;
}
  相关解决方案