C. Jury Marks
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, …, bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn’t announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
Input
The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, …, ak ( - 2 000 ≤ ai ≤ 2 000) — jury’s marks in chronological order.
The third line contains n distinct integers b1, b2, …, bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Output
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print “0” (without quotes).
Examples
input
4 1
-5 5 0 20
10
output
3
input
2 2
-2000 -2000
3998000 4000000
output
1
Note
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
题意 : 按先后给出 k 个裁判打的分数a[i],和不是先后顺序给出的 n 个Polycarp 记得在某次裁判打完后分数b[j],求可能的初始分有多少种可能
思路 : 求出在第 i 个裁判打分后的得分,然后 n 个 Polycarp 记得分数,一定是在某次裁判打完分后的得分 + 初始分,枚举初始分,假设 b[1],是在第 1…….n次打分后的得分
两种AC代码:
map :
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX = 2e3 + 10;
LL a[MAX],b[MAX];
map <LL,LL> m;
int main()
{
int n,k,cut = 0;
scanf("%d %d",&k,&n);
for(int i = 1; i <= k; i++)
scanf("%lld",&a[i]),a[i] += a[i - 1],m[a[i]] = 1;
for(int i = 1; i <= n; i++)
scanf("%lld",&b[i]);
sort(a + 1,a + 1 + k);
k = unique(a + 1,a + 1 + k) - a - 1; // 去重,并非真正意义上的删除,而是把相邻的重复元素放到数组后面,使用前需要排序
for(int i = 1; i <= k; i++){
int x = b[1] - a[i],ok = 1;
for(int j = 1; j <= n; j++)
if(!m[b[j] - x]){ // 查找是否满足
ok = 0; break;
}
if(ok) cut++;
}
printf("%d\n",cut);
return 0;
}
STL :
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
const int MAX = 2e3 + 10;
LL a[MAX],b[MAX];
int main()
{
int n,k,cut = 0;
scanf("%d %d",&k,&n);
for(int i = 1; i <= k; i++)
scanf("%lld",&a[i]),a[i] += a[i - 1];
for(int i = 1; i <= n; i++)
scanf("%lld",&b[i]);
sort(a + 1,a + 1 + k);
k = unique(a + 1,a + 1 + k) - a - 1; // 去重,并非真正意义上的删除,而是把相邻的重复元素放到数组后面,使用前需要排序
for(int i = 1; i <= k; i++){
int x = b[1] - a[i],ok = 1;
for(int j = 1; j <= n; j++)
if(!binary_search(a + 1,a + 1 + k,b[j] - x)){ // 二分查找是否满足
ok = 0; break;
}
if(ok) cut++;
}
printf("%d\n",cut);
return 0;
}