QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#46844 | #4591. Maxdifficent Group | abcde | WA | 25ms | 5132kb | C++ | 2.6kb | 2022-09-01 23:57:02 | 2022-09-01 23:57:03 |
Judging History
answer
#include<bits/stdc++.h>
#define fr(i,j,n) for(int i=j;i<n;i++)
#define pb push_back
#define rf(i,j,p) for(int i=p-1;i>=j;i--)
#define vi vector<int>
#define vll vector<long long>
#define ll long long
#define hmm "\n"
#define sp " "
#define srt(v) sort(v.begin(), v.EEnd());
#define ww int tc ; cin >> tc ; while(tc--)
#define down cout<<hmm;
#define faaast ios_base::sync_with_stdio(0);cin.tie(nullptr);
#include <iostream>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll max_so_far, max_EEnding_here ;
ll start, EEnd , s;
bool allneg(ll a[], int n)
{
fr(i,0,n)
{
if(a[i]>0)return 0;
}
return 1;
}
bool allpos(ll a[], int n)
{
fr(i,0,n)
{
if(a[i]<0)return 0;
}
return 1;
}
void maxSubArraySum(ll a[], int size)
{
start =0, EEnd = 0, s=0;
max_so_far = LLONG_MIN, max_EEnding_here = 0;
for (int i=0; i< size; i++ )
{
max_EEnding_here += a[i];
if (max_so_far < max_EEnding_here)
{
max_so_far = max_EEnding_here;
start = s;
EEnd = i;
}
if (max_EEnding_here < 0)
{
max_EEnding_here = 0;
s = i + 1;
}
}
//max so far , EEnd ;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n ; cin >> n ;
ll arr[n]; ll cum[n];
fr(i,0,n)cin >> arr[i], cum[i]=arr[i];
maxSubArraySum( arr, n);
//cout<<start<<sp<<EEnd<<hmm;
if((start == 0 && EEnd == n-1)||allneg(arr, n)|| allpos(arr,n)){
fr(i,1,n)cum[i]+=cum[i-1];
ll mx = 0 ;
mx = abs(cum[n-1]-(ll)2*arr[0]);
mx = max(mx , abs(cum[n-1]-2*arr[n-1]));
fr(i,1,n-1){
mx = max(mx ,abs(cum[i-1]-arr[i]));
mx = max(mx, abs(cum[n-1]-cum[i]-arr[i]));
}
cout<<mx<<hmm;
return 0;
}
ll mx = 0, sum=0 ;
for(int i = start -1 ; i >=0 ; i--){
sum += arr[i];
mx = max(mx, abs(max_so_far-sum));
}
sum = 0 ;
fr(i,EEnd+1, n)
{
sum += arr[i];
mx = max(mx, abs(max_so_far-sum));
}
cout<<mx<<hmm;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3712kb
input:
4 100 -30 -20 50
output:
150
result:
ok single line: '150'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
5 12 7 4 32 9
output:
46
result:
ok single line: '46'
Test #3:
score: 0
Accepted
time: 3ms
memory: 3596kb
input:
6 -5 10 -5 45 -20 15
output:
70
result:
ok single line: '70'
Test #4:
score: 0
Accepted
time: 2ms
memory: 3612kb
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: 3540kb
input:
2 0 -900000
output:
900000
result:
ok single line: '900000'
Test #6:
score: 0
Accepted
time: 10ms
memory: 4536kb
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: 13ms
memory: 4696kb
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: 8ms
memory: 5016kb
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: 11ms
memory: 3972kb
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: 13ms
memory: 4228kb
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: 11ms
memory: 4220kb
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: 6ms
memory: 4280kb
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: 14ms
memory: 4332kb
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: -100
Wrong Answer
time: 25ms
memory: 5132kb
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:
35965129456
result:
wrong answer 1st lines differ - expected: '45189595250', found: '35965129456'