1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 #include <string> 10 #include <iostream> 11 #include <ctype.h> 12 #include <string.h> 13 #include <set> 14 #include <stack> 15 #include<functional> 16 using namespace std; 17 #define size 100001 18 #define maxn 1<<30 19 int past[size];//past[i]表示在第i天之后(包括i)买入,在第i天后面的某天卖出所能赚到的最大钱数 20 int pre[size];//pre[i]表示在第i天之前(包括i)卖出,在第i天之前买入所能获得的最大利润 21 int a[size]; 22 /* 23 24 25 */ 26 int main(){ 27 int T; 28 cin >> T; 29 while (T--){ 30 int n; 31 cin >> n; 32 memset(past, 0, sizeof(past)); 33 memset(pre, 0, sizeof(pre)); 34 for (int i = 1; i <= n; i++) 35 scanf("%d", &a[i]); 36 int low_prise = a[1];//买入的价格 37 pre[1] = 0; 38 for (int i = 2; i <= n; i++) 39 { 40 low_prise = min(low_prise, a[i]); 41 pre[i] = max(pre[i-1],a[i] - low_prise);//如果等于0 表示今天买入今天卖出 42 } 43 int high_prise = a[n];//卖出的价格 44 past[n] = 0; 45 for (int i = n - 1; i >= 1; i--) 46 { 47 high_prise = max(high_prise, a[i]); 48 past[i] = max(past[i+1],high_prise - a[i]); 49 } 50 int max_profit = 0; 51 for (int i = 1; i <= n; i++) 52 max_profit = max(max_profit, past[i] + pre[i]); 53 cout << max_profit << endl; 54 } 55 system("pause"); 56 return 0; 57 }