题意:给出一个有n个数的序列,求出Ai-Aj (i<j)的最大值
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19692
思路:用一个变量记录Ai最大值的状态,在用另一个变量记录结果的最大值的状态
注意点:暴力超时
以下为AC代码:
RunID
|
User
|
OJ
|
Prob ID
|
Result
|
Memory
(KB) |
Time
(ms) |
Language
|
Length
(Bytes) |
Submit Time
|
---|---|---|---|---|---|---|---|---|---|
2999627 | Accepted | 0 | 99 | 970 |
2014-11-19 18:39:00
|
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
inline int max ( int a, int b )
{
return a > b ? a : b;
}
int main()
{
ios::sync_with_stdio( false );
int num[100000];
int t;
cin >> t;
while ( t -- )
{
int n;
int ans;
int maxn;
cin >> n;
for ( int i = 0; i < n; i ++ )
{
cin >> num[i];
}
ans = num[0] - num[1];
maxn = num[0];
for ( int i = 1; i < n; i ++ )
{
ans = max ( ans, maxn - num[i] );
maxn = max ( maxn, num[i] );
}
cout << ans << endl;
}
return 0;
}