QTSEQ - Dãy số QT

Tác giả: hieult

Ngôn ngữ: C++

#include<cstdio>
#include<cmath>
#include<math.h>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define ep 0.00001
#define maxn 1000011
#define oo 1111111111
#define mod 1000000007
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define rep(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
//#include <conio.h>
//#define g 9.81

const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0;
char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; }
void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); }
int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; }
template<class T> bool gi(T &v) {
	v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc();
    while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true;
}

double const PI=4*atan(1.0);

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;

void OPEN(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}

int n;
long long x, a[1000011], MAX[maxn] = {0}, MIN[maxn] = {0}, res = -1, num = 0, tong = 0, num_max[maxn], num_min[maxn];

int main(){
   // OPEN();
    gi(n);
    rep(i, n){
        gi(a[i]);
        if(!i){
            MAX[i] = a[i];
            MIN[i] = a[i];
            num_max[i] = 1;
            num_min[i] = 1;
        }
        else{
            a[i] += a[i - 1];
            MAX[i] = max(a[i], MAX[i - 1]);
            MIN[i] = min(a[i], MIN[i - 1]);
            if(a[i] > MAX[i - 1]) num_max[i] = 1;
            else if(a[i] == MAX[i - 1]) num_max[i] = num_max[i - 1] + 1;
            else num_max[i] = num_max[i - 1];
            if(a[i] < MIN[i - 1]) num_min[i] = 1;
            else if(a[i] == MIN[i - 1]) num_min[i] = num_min[i - 1] + 1;
            else num_min[i] = num_min[i - 1];
        }
    }
    
   // rep(i, n) printf("%lld %lld\n", num_max[i], num_min[i]);
    res = - (1ll << 62);
    FORD(i, n - 1, 1){
        if(abs(MIN[i - 1] - a[n - 1] + a[i - 1]) > res){
            res = abs(MIN[i - 1] - a[n - 1] + a[i - 1]);
            num = num_min[i - 1];
        }
        else if(abs(MIN[i - 1] - a[n - 1] + a[i - 1]) == res) num += num_min[i - 1];
        if(MAX[i - 1] == MIN[i - 1]) continue;
        if(abs(MAX[i - 1] - a[n - 1] + a[i - 1]) > res){
            res = abs(MAX[i - 1] - a[n - 1] + a[i - 1]);
            num = num_max[i - 1];
        }
        else if(abs(MAX[i - 1] - a[n - 1] + a[i - 1]) == res) num += num_max[i - 1];
    }
    cout << res << " " << num;
}

Download