首页 > 其他 > 详细

UVa11078:Open Credit System

时间:2018-01-31 13:37:26      阅读:262      评论:0      收藏:0      [点我收藏+]

UVa11078:Open Credit System

题目大意

给定一个数组A,求Ai-Aj的最大值(i<j)

要求复杂度:Ο(n)

做法

对给定的j,为了Ai-Aj取得最大值,Ai应该取最大,因此可以用一个变量maxA维护最大值,边读边计算答案。

AC-Code(C++)

Time:50ms

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 30000 + 10;

int main(int argc, const char * argv[]) {
    
//    freopen("input.txt", "r", stdin);
    
    int n,T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int maxA;
        scanf("%d",&maxA);
        int ans = -INF;
        int temp;
        for(int i=1;i<n;i++){
            scanf("%d",&temp);
            ans = max(ans,maxA-temp);
            maxA = max(maxA,temp);
        }
        
        printf("%d\n",ans);
    }
    return 0;
}

UVa11078:Open Credit System

原文:https://www.cnblogs.com/irran/p/UVa11078.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!