UVA 11078 Open Credit System

题目链接:https://vjudge.net/problem/UVA-11078
题目思路:令bi = ai-1 - ai,那么就能用连续的bi来表示所有的ai -aj,问题转化成了最大子段和,参考我的另外一篇博客 https://www.cnblogs.com/asakuras/p/8613658.html
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100005;
typedef long long ll;
ll arr[maxn];
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        memset(arr,0,sizeof(arr));
        int n;
        scanf("%d",&n);
        ll maxval;
        for(int i = 1; i <= n; i ++)
        {
            scanf("%lld",&arr[i]);
            arr[i - 1] = arr[i - 1] - arr[i];
        }
        maxval = arr[1];
        for(int i = 2; i <= n - 1; i ++)
        {
            arr[i] = max(arr[i], arr[i - 1] + arr[i]);
            maxval = max(maxval, arr[i]);
        }
        printf("%lld\n",maxval);
    }
    return 0;
}
posted @ 2018-09-14 15:41  asakuras  阅读(90)  评论(0编辑  收藏  举报