QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#256266#4591. Maxdifficent Groupjiayi_koWA 24ms3760kbC++142.4kb2023-11-18 18:14:272023-11-18 18:14:28

Judging History

你现在查看的是最新测评结果

  • [2023-11-18 18:14:28]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:3760kb
  • [2023-11-18 18:14:27]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> a(100005);
int main(){
    int n;
    ll sum=0, total=0, ans=0, lala=0;
    int l, r, ml, mr;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
        lala+=a[i];
    }
    l=r=0;
    ml=mr=-1;
    // largest positive maximum subarray
    for(int i=0; i<n; i++){
        if(sum + a[i] >= 0){
            sum+=a[i];
            r=i;
        }
        else{
            l=i+1;
            r=i+1;
            sum=0;
        }
        if(sum > total){
            total=sum;
            ml=l;
            mr=r;
        }
    }
    // cout<<"positive "<<ml<<" "<<mr<<": "<<total<<"\n";
    // hanya ada 1 grup
    if(ml == 0 && mr == (n-1)){
        ans=max(ans, max({abs((total-a[0])-a[0]), abs((total-a[n-1])-a[n-1]), abs(a[0]-(total-a[0])), abs(a[n-1]-(total-a[n-1]))}));
    }
    else if(ml != -1){
        // cari minimum kiri dan kanan
        ll sumleft=0, best=1e18;
        ll sumright=0;
        ml--;
        while(ml >= 0){
            sumleft+=a[ml];
            ml--;
            best=min(best, sumleft);
        }
        mr++;
        while(mr < n){
            sumright+=a[mr];
            mr++;
            best=min(best, sumright);
        }
        ans=max(ans, abs(total-best));
    }

    // largest negative
    sum=0;
    total=lala;
    ml=0;
    mr=n-1;
    r=l=0;
    for(int i=0; i<n; i++){
        if(sum + a[i] <= 0){
            sum+=a[i];
            r=i;
        }
        else{
            l=r=i+1;
            sum=0;
        }
        if(sum < total){
            total=sum;
            ml=l;
            mr=r;
        }
    }
    // cout<<"negative "<<ml<<" "<<mr<<": "<<total<<"\n";
    // hanya ada 1 grup
    if(ml == 0 && mr == (n-1)){
        ans=max(ans, max({abs((total-a[0])-a[0]), abs((total-a[n-1])-a[n-1]), abs(a[0]-(total-a[0])), abs(a[n-1]-(total-a[n-1]))}));
    }
    else if(ml != -1){
        // cari minimum kiri dan kanan
        ll sumleft=0, best=0;
        ll sumright=0;
        ml--;
        while(ml >= 0){
            sumleft+=a[ml];
            ml--;
            best=max(best, sumleft);
        }
        mr++;
        while(mr < n){
            sumright+=a[mr];
            mr++;
            best=max(best, sumright);
        }
        ans=max(ans, abs(total-best));
    }
    cout<<ans<<"\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3680kb

input:

4
100 -30 -20 50

output:

150

result:

ok single line: '150'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

5
12 7 4 32 9

output:

46

result:

ok single line: '46'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3636kb

input:

6
-5 10 -5 45 -20 15

output:

70

result:

ok single line: '70'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3664kb

input:

14
1 5 3 4 2 -4 -2 -6 -5 6 4 2 3 9

output:

41

result:

ok single line: '41'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3704kb

input:

2
0 -900000

output:

900000

result:

ok single line: '900000'

Test #6:

score: 0
Accepted
time: 6ms
memory: 3676kb

input:

59394
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 7ms
memory: 3760kb

input:

63744
-1 0 1 1 0 0 1 -1 -1 -1 -1 -1 1 1 1 -1 0 0 1 1 1 -1 1 -1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 0 1 -1 1 1 1 -1 1 -1 0 0 -1 -1 1 1 0 0 0 1 0 -1 -1 0 0 0 1 -1 0 1 0 0 1 -1 -1 0 0 -1 -1 1 0 1 0 1 0 -1 0 -1 1 1 -1 1 1 -1 1 -1 1 0 0 -1 1 1 0 0 -1 0 0 1 0 0 1 -1 1 0 1 0 -1 0 0 1 0 0 -1 0 0 0 -1 0 0 1 -1 -...

output:

475

result:

ok single line: '475'

Test #8:

score: 0
Accepted
time: 6ms
memory: 3632kb

input:

87255
4 -9 3 -3 -2 -2 3 10 -7 -10 -5 5 -9 -7 1 -1 10 0 -1 1 10 -9 -8 8 5 -8 2 -3 -7 7 -5 -3 3 -2 5 -1 -5 6 -2 8 3 0 -6 5 -10 5 0 -8 -3 -1 -7 -3 0 7 -4 5 -1 -5 -9 -2 8 7 6 -2 6 9 -2 6 10 3 10 -10 4 5 -9 0 -10 -9 -10 6 9 -5 -3 -7 -2 -4 6 4 7 -4 4 2 -8 -2 -4 3 2 -9 -4 9 3 8 -5 -1 8 -10 8 -3 -6 7 -1 8 -...

output:

5780

result:

ok single line: '5780'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3672kb

input:

22859
123270 568778 416102 510776 597790 125263 860375 89550 16897 909046 124583 592535 32050 175429 993750 899188 863104 608753 724971 728319 82786 733737 923128 266621 57189 518044 556356 756193 408537 94718 998619 206034 125763 649241 204930 474150 57466 358193 153011 416494 753281 480726 927554 ...

output:

11402652338

result:

ok single line: '11402652338'

Test #10:

score: 0
Accepted
time: 8ms
memory: 3680kb

input:

33834
-514303 -172893 -206523 -8184 -343624 -208669 -690452 -17023 -342571 -830835 -999032 -533750 -822347 -62447 -85968 -172653 -485779 -40414 -37219 -564266 -427415 -565431 -197676 -911379 -238653 -602443 -410483 -122721 -921143 -764784 -355380 -23053 -510189 -547702 -165836 -672416 -12018 -966618...

output:

16851470332

result:

ok single line: '16851470332'

Test #11:

score: 0
Accepted
time: 7ms
memory: 3580kb

input:

38275
-999930 -999875 -999861 -999804 -999784 -999669 -999500 -999492 -999437 -999435 -999368 -999324 -999180 -999143 -999117 -999090 -999085 -999026 -998991 -998956 -998853 -998850 -998832 -998603 -998594 -998572 -998559 -998550 -998506 -998390 -998277 -998258 -998220 -998135 -998087 -997967 -99785...

output:

19123721387

result:

ok single line: '19123721387'

Test #12:

score: 0
Accepted
time: 8ms
memory: 3672kb

input:

37238
999909 999833 999825 999777 999746 999734 999700 999678 999636 999622 999616 999213 999117 999095 998985 998963 998939 998861 998771 998769 998739 998677 998636 998634 998531 998275 998224 998146 998046 997992 997955 997788 997735 997647 997629 997628 997490 997456 997230 997193 997118 996935 ...

output:

18615206654

result:

ok single line: '18615206654'

Test #13:

score: 0
Accepted
time: 6ms
memory: 3564kb

input:

43149
-140201 -501803 656742 -329686 -795682 -699585 891458 639773 -265403 -322470 587438 -291944 981542 -514447 -146704 158657 308640 -720314 -593447 93738 -837311 -528874 -298652 643385 -665241 124247 -263996 -251752 -777850 814558 134614 498228 23865 628783 856530 -100188 350276 685071 -635465 54...

output:

179476167

result:

ok single line: '179476167'

Test #14:

score: 0
Accepted
time: 21ms
memory: 3668kb

input:

100000
-157175 -347083 -932037 -545196 -659616 600072 -286127 -201837 -997799 -12057 -921128 -6769 -754129 -681225 -136294 -187918 -42011 -568716 -816662 -239977 -417282 -319395 -751876 -674830 -570340 -724210 -422603 -961907 -699371 -249593 -881886 -260367 -873171 -830607 -733352 -533789 -980129 -7...

output:

45189595250

result:

ok single line: '45189595250'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

15
1 5 3 4 2 -4 -2 -6 -5 6 4 2 -1 3 9

output:

40

result:

ok single line: '40'

Test #16:

score: -100
Wrong Answer
time: 24ms
memory: 3676kb

input:

100000
-307691 -36043 -946036 -686424 -566905 -420198 -113847 -609649 764682 -193881 -899305 -860342 -940133 -759469 -230786 -994676 -867417 -152003 -282385 -447316 -276684 17745 766295 983080 -982977 -468861 -461817 -667312 -489521 -783625 578675 -335354 -112405 -319581 -731817 928193 -390912 17800...

output:

30111222485

result:

wrong answer 1st lines differ - expected: '30112062436', found: '30111222485'