1. 程式人生 > >Aizu ALDS1_1_D Maximum Profit

Aizu ALDS1_1_D Maximum Profit

題意:給你一串數,讓你找到這串數字中後面的數減前面的數差值最大的那個值。
做法:從頭到尾掃一遍,每次執行2個操作
先維護到目前為止的最優值MAX = max(MAX,a[i] - MIN);
然後再更新到目前為止的最小值MIN = min(MIN,a[i]);

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5
typedef long long LL;
const double pi = acos(-1.0
); const long long mod = 25 * 1E8; using namespace std; int a[200005]; int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(0);cin.tie(0); int n; cin >> n; for(int i = 0;i < n;i++) cin >> a[i]; int MAX = -INF,MIN = a[0
]; for(int i = 1;i < n;i++) { MAX = max(MAX,a[i] - MIN); MIN = min(MIN,a[i]); } cout << MAX << endl; return 0; }