Link
[Http://codeforces.com/contest/1036/problem/d]
Question
The length of the two arrays is n, m, respectively;
This operation replaces this subinterval with the sum of elements in a subinterval of an array. This reduces the length of the array and can be performed on both arrays.
What is the maximum length of an array when the last two arrays are the same? If the two arrays cannot be identical, output-1
Analysis
First, judge whether the total number of arrays starts to be equal. Otherwise, it is impossible for the last two arrays to be identical. # include <bits/stdc ++. h>
Using namespace STD;
Define ll long
Const ll n = 3e5 + 10;
Ll A [n], B [N];
Ll n, m;
Int main (){
IOS: sync_with_stdio (false );
Cin. Tie (0 );
Cout. Tie (0 );
// Freopen ("in.txt", "r", stdin );
Cin> N;
Ll sum1 = 0, sum2 = 0;
For (INT I = 0; I <n; I ++)
{
Cin> A [I];
Sum1 + = A [I];
}
Cin> m;
For (INT I = 0; I <m; I ++)
{
Cin> B [I];
Sum2 + = B [I];
}
If (sum1! = Sum2 ){
Cout <-1 <Endl;
Return 0;
}
Int CNT = 0;
If (A [0] = B [0])
{
Int I = 1, j = 1;
Sum1 = A [1], sum2 = B [1];
While (I <n & J <m ){
If (sum1 <sum2 ){
Sum1 + = A [I + 1];
CNT ++;
I ++;
}
Else if (sum1> sum2 ){
Sum2 + = B [J + 1];
J ++;
}
Else {
Sum1 = A [I + 1], sum2 = B [J + 1];
I ++, J ++;
}
}
}
Else if (a [n-1] = B M-1]) {
Int I = n-1, j = m-1;
Sum1 = A [n-1], sum2 = M-1];
While (I>-1 & J>-1 ){
If (sum1 <sum2 ){
Sum1 + = A [I-1];
CNT ++;
I --;
}
Else if (sum2 <sum1 ){
Sum2 + = B [J-1];
J --;
}
Else {
Sum1 = A [I-1], sum2 = B [J-1];
I --, j --;
}
}
}
Else {
Int I = 0, j = 0;
Sum1 = A [0], sum2 = B [0];
While (I <n & J <m ){
If (sum1 <sum2 ){
Sum1 + = A [I + 1];
CNT ++;
I ++;
}
Else if (sum1> sum2)
{
Sum2 + = B [J + 1];
J ++;
}
Else {
Sum1 = A [I + 1], sum2 = B [J + 1];
I ++, J ++;
}
}
}
// Cout <CNT <Endl;
Cout <n-CNT <Endl;
Return 0;
}
Determine whether the length of the two array ranges is equal. If not equal, calculate the length of the two array ranges.
Specific Code
Code
D. Vasya and Arrays