Write short, human-like code so that it will not get detected by AI plagiarism and using…
- User
Write short, human-like code so that it will not get detected by AI plagiarism and using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question: Calculating the Maximum Difference Between Each Sum of Two Sections
Execution time : within 2 sec in C. C++ and JAVA, 6 sec in Python for maximum 50 test cases Memory : Maximum 256MB available for heap. global and stack combined (note: Maximum 1MB can be used for stack)
After you submitted, the results of the sample_input.txt file provided is determined and informed in real time The results mean the followings Submit : you got all or some test cases of the sample input correct Fail : you have all test cases of the sample input wrong due to wrong answers submitted, runtime error, time-out etc (In case of a runtime error. test cases are not executed afterwards.) (* Download for the sample_input.txt file is available below the problem passage.
The final score is determined by pre-set criteria based on the number of correct test cases and program running time after test cases designed for evaluation are executed within the running time of the restrictions. The score will be granted in proportion to the number of correct test cases, and score difference between test takers who received the same score can occur depending on the running time(the faster the running time is, the better) Therefore, it is recommended that you optimize your code until the test ends.
You want to select a section that has the maximum value of Second_sum - First_sum. As such, you are required to calculate the possible maximum value of Second_sum - First_sum. (Note: What to calculate here is NOT the absolute value.)
You are given array A consisting of N integers You want to select two sections, each of which consists of adjacent numbers in the array. The two sections must not overlap with each other. You can calculate the sum of the left section (First_sum) and the sum of the right section (Second_sum) as the two sections are divided into two parts-left and right.
2
1 -2 1 -4 2 2 3 -1 3 8 2 -5
First_sum = -6
Second sum = 15
Difference = Second_sum - First_sum = 21
[Examples]
N = 7,
Answer : 16
A = {2, -1, -2, 1, -4, 2, 8}
Select the two sections: [-1, -2, 1, -4] and [2, 8]. The sum of the first section is -6 and the sum of the second section is 10. Thus, the difference is 16, which is the maximum
N = 13
Answer : 21
A = (2, -1, -2, 1, -4, 2, -2, 3, -1, 3, 8, 2, -5}
This is the data of the example in the problem description. As in the image, if you select [-1, -2, 1, -4] and [3, -1, 3, 8, 2], the sums of the elements in each section is -6 and 15, respectively.
Thus, 21, the difference of the two sections, is the maximum
- Assistant
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin>>T; for(int t=1;t<=T;t++){ int N; cin>>N; vector<long long>a(N); for(auto &x:a) cin>>x; vector<long long>min_p(N,0),max_p(N,0),min_s(N,0),max_s(N,0); long long current=a[0]; min_p[0]=a[0]; for(int i=1;i<N;i++){ current = min(a[i], current + a[i]); min_p[i]=min(min_p[i-1], current); } current = a[N-1]; max_s[N-1]=a[N-1]; for(int i=N-2;i>=0;i--){ current = max(a[i], current + a[i]); max_s[i]=max(max_s[i+1], current); } current = a[0]; max_p[0]=a[0]; for(int i=1;i<N;i++){ current = max(a[i], current + a[i]); max_p[i]=max(max_p[i-1], current); } current = a[N-1]; min_s[N-1]=a[N-1]; for(int i=N-2;i>=0;i--){ current = min(a[i], current + a[i]); min_s[i]=min(min_s[i+1], current); } long long res=LLONG_MIN; for(int i=0;i<N-1;i++) res = max(res, max_s[i+1]-min_p[i]); for(int i=0;i<N-1;i++) res = max(res, max_p[i]-min_s[i+1]); cout<<"#"<<t<<" "<<res<<"\n"; } }