首页 > 代码库 > HDU - 5586 Sum(区间增量最大)

HDU - 5586 Sum(区间增量最大)

题意:将数组A的部分区间值按照函数f(Ai)=(1890*Ai+143)mod10007修改值,区间长度可以为0,问该操作后数组A的最大值。

分析:先求出每个元素的增量,进而求出增量和。通过b[r]-b[l-1]求区间增量和,枚举r,而b[l-1]则是b[r]前所有元素的最小值,注意mi初始化为0,因为当前有可能的最优值为区间0~r。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int b[MAXN];
int main(){
    int n;
    while(scanf("%d", &n) == 1){
        memset(a, 0, sizeof a);
        memset(b, 0, sizeof b);
        int ans = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
            ans += a[i];
            b[i] = ((a[i] * 1890 + 143) % 10007) - a[i];//增量
        }
        for(int i = 1; i < n; ++i){
            b[i] += b[i - 1];//增量和
        }
        int ma = 0;
        int mi = 0;
        for(int i = 0; i < n; ++i){
            ma = Max(ma, b[i] - mi);
            mi = Min(mi, b[i]);
        }
        if(ma > 0){
            printf("%d\n", ans + ma);
        }
        else{
            printf("%d\n", ans);
        }
    }
    return 0;
}

 

 

 

HDU - 5586 Sum(区间增量最大)