Editorial for Dãy số QT


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của ladpro98

program QTSEQ;
uses    math;
const   fi='';
        maxn=trunc(1e6)+6;
        oo=2*trunc(1e9);
var     a,cmin,cmax:array[0..maxn] of longint;
        maxPs,minPs,ps:array[0..maxn] of int64;
        n,i:longint;
        S,res,cnt:int64;
begin
        readln(n);
        maxPs[0]:=-oo;minPs[0]:=oo;
        for i:=1 to n do begin
                read(a[i]);
                ps[i] := ps[i-1] + a[i];
                maxPs[i] := maxPs[i-1]; cmax[i] := cmax[i-1];
                minPs[i] := minPs[i-1]; cmin[i] := cmin[i-1];
                if (minPs[i]=ps[i]) then inc(cmin[i]);
                if (maxPs[i]=ps[i]) then inc(cmax[i]);
                if (maxPs[i]<ps[i]) then begin
                        maxPs[i] := ps[i];
                        cmax[i] := 1;
                end;
                if (minPs[i]>ps[i]) then begin
                        minPs[i] := ps[i];
                        cmin[i] := 1;
                end;
        end;
        for i:=n downto 2 do begin
                inc(S,a[i]);
                if (res=abs(S-minPs[i-1])) then inc(cnt,cmin[i-1]);
                if ((res=abs(S-maxPs[i-1])) and (maxPs[i-1]<>minPs[i-1])) then inc(cnt,cmax[i-1]);
                if (res<abs(S-minPs[i-1])) then begin
                        res := abs(S-minPs[i-1]);
                        cnt := cmin[i-1];
                end;
                if (res<abs(S-maxPs[i-1])) then begin
                        res := abs(S-maxPs[i-1]);
                        cnt := cmax[i-1];
                end;
        end;
        write(res, #32 , cnt);
end.

Code mẫu của RR

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <complex>

#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(i,a) for(int i = 0; i < a; ++i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
using namespace std;

const int MN = 1000111;

int n, a[MN];
long long l[MN], r[MN];

//Buffer reading
int INP,AM;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (feof(stdin)) memset(BUF, 0, sizeof BUF); else fread(BUF,1,BUFSIZE,stdin); \
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long long oo = 1000111000111000111LL;

int main() {
    GN(n);
    FOR(i,1,n) GN(a[i]);
    FOR(i,1,n) l[i] = l[i-1] + a[i];
    FORD(i,n,1) r[i] = r[i+1] + a[i];

    long long res = -1, cnt = 0, ln = -oo, nn = oo, c1 = 0, c2 = 0, now;
    FOR(i,1,n-1) {
        if (l[i] < nn) {
            nn = l[i];
            c1 = 1;
        }
        else if (l[i] == nn) {
            ++c1;
        }

        if (l[i] > ln) {
            ln = l[i];
            c2 = 1;
        }
        else if (l[i] == ln) {
            ++c2;
        }

        now = ln - r[i+1];
        if (now > res) {
            res = now;
            cnt = c2;
        }
        else if (now == res) {
            cnt += c2;
        }

        now = r[i+1] - nn;
        if (now > res) {
            res = now;
            cnt = c1;
        }
        else if (now == res) cnt += c1;
    }
    if (!res) cnt /= 2;
    cout << res << ' ' << cnt << endl;
    return 0;
}

Code mẫu của hieult

#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;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.