D. Vasya and Arrays

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.