题目大意:给出两组数,A,B,长度分别为m,n,可以对数组进行一种操作,把连续的数字段加在一起(这些数字从数组中去除),合成一个新数,放入数组,问A,B能够相等的最大长度,如果不能相等,输出-1.
题目链接:http://codeforces.com/contest/1036/problem/D
c++代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=1e6;
typedef long long LL;
LL a[maxn],b[maxn];
int main() {
int n,m;
LL suma=0,sumb=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
suma+=a[i];
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&b[i]);
sumb+=b[i];
}
if(suma!=sumb)printf("-1\n");
else {
int i=0,j=0,ans=0;
suma=0,sumb=0;
for(i=0;i<n;i++) {
suma+=a[i];
while(suma>sumb&&j<m) {
//cout << j <<endl;
sumb+=b[j];
j++;
//sumb+=b[j];
}
//cout << suma <<" " <<sumb << endl;
if(suma==sumb) {
ans++;
suma=sumb=0;
}
//cout << b[50] <<endl;
//cout << i << " " << j <<endl;
}
printf("%lld\n",ans);
}
return 0;
}