CF1036D.Vasya and Arrays
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vasya has two arrays A and B of lengths n and m, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1,10,100,1000,10000] Vasya can obtain array [1,1110,10000], and from array [1,2,3] Vasya can obtain array [6].
Two arrays A and B are considered equal if and only if they have the same length and for each valid i Ai=Bi.
Vasya wants to perform some of these operations on array A, some on array B, in such a way that arrays A and B become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays A and B equal.
瓦西里有两个长度分别为 n 和 m 的数组 A 和 B。
他可以任意多次(包括零次)执行如下操作:选取数组中某个连续子段,并将其替换为一个元素,该元素的值等于该子段中所有元素之和。例如,从数组 [1,10,100,1000,10000] 出发,瓦西里可以得到数组 [1,1110,10000];从数组 [1,2,3] 出发,他可以得到数组 [6]。
当且仅当两个数组长度相同,且对每个合法下标 i 均有 Ai=Bi 时,称数组 A 和 B 相等。
瓦西里希望对数组 A 执行若干次上述操作,对数组 B 也执行若干次上述操作,使得最终两个数组相等。此外,要求最终数组的长度尽可能大。
请帮助瓦西里确定所能达到的最大数组长度;若无法使数组 A 和 B 相等,则输出不可能。
输入格式
The first line contains a single integer n (1≤n≤3⋅105) — the length of the first array.
The second line contains n integers a1,a2,⋯,an (1≤ai≤109) — elements of the array A.
The third line contains a single integer m (1≤m≤3⋅105) — the length of the second array.
The fourth line contains m integers b1,b2,⋯,bm (1≤bi≤109) - elements of the array B.
第一行包含一个整数 n (1≤n≤3⋅105) —— 第一个数组的长度。
第二行包含 n 个整数 a1,a2,⋯,an (1≤ai≤109) —— 数组 A 的元素。
第三行包含一个整数 m (1≤m≤3⋅105) —— 第二个数组的长度。
第四行包含 m 个整数 b1,b2,⋯,bm (1≤bi≤109) —— 数组 B 的元素。
输出格式
Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays A and B in such a way that they became equal.
If there is no way to make array equal, print "-1".
输出一个整数——在对数组 A 和 B 执行若干操作后,使其相等的情况下,所得数组的最大长度。
若无法使两个数组相等,则输出 -1。
输入输出样例
输入#1
5 11 2 3 5 7 4 11 7 3 7
输出#1
3
输入#2
2 1 2 1 100
输出#2
-1
输入#3
3 1 2 3 3 1 2 3
输出#3
3
输入解题思路,AI测评打分。不知道怎么写?