C. Jury Marks

C. Jury Marks
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard 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.


题意:一个比赛中,n裁判依次给你打分,第i个裁判给了分,但你记性不好,没有记住所有得分,连初始分都忘记了,只记得m个加完分后的总分b[i](不是按顺序的),且B[i]互不相同。问:初始分有多少种可能的情况。

 解题思维:对评分进行前缀和,前缀和数组为a[i],第二行输入的数组为b[i],如有x满足条件,则a[i]-b[j]至少有k次。

注意前缀和去重,因为重复的前缀和会对其相同的前缀和在判断x是否出现k此的时候也有贡献,若x可能只出现了m次,若此时有几个个相同的前缀和,且该前缀和有b[j]-a[i]=x,则会对x出现的次数有错误的计算。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<int>vec;
 4 map<int,int>mp;
 5 
 6 int main()
 7 {
 8     int n,k,i,j,x,a[20005],ans;
 9     cin>>n>>k;
10     cin>>a[0];
11     for(i=1;i<n;i++)
12     {
13         cin>>x;
14         a[i]=a[i-1]+x;
15     }
16     sort(a,a+n);
17     int len=unique(a,a+n)-a; //前缀和去重
18     for(i=0;i<k;i++)
19     {
20         cin>>x;
21         for(j=0;j<len;j++)
22         {
23             vec.push_back(x-a[j]);
24         }
25     }
26     sort(vec.begin(),vec.end());//去掉会超时,可能与map的内部实现有关
27     for(i=0;i<vec.size();i++)
28     {
29         mp[vec[i]]++;
30        //cout<<vec[i]<<" ";
31     }
32     //cout<<endl;
33     ans=0;
34     for(i=0;i<vec.size();i++)
35     {
36         if(mp[vec[i]]>=k)
37         {
38             ans++;
39             mp[vec[i]]=0;
40         }
41     }
42     cout<<ans<<endl;
43     return 0;
44 }

还有一种思路是:先对前缀和a[i],b[i]分别排序,对于n个x:x=b[0]-a[i],(0<=i<n),是否对应k个b[j]-x是否有相应的a[i]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 set<int>se;
 4 int main()
 5 {
 6     int n,k,i,j,a[2005],b[2005],ans,x;
 7     cin>>n>>k;
 8     cin>>x;
 9     for(i=1;i<n;i++)
10     {
11         cin>>x;
12         a[i]=a[i-1]+x;
13     }
14     sort(a,a+n);
15     for(i=0;i<k;i++)
16     cin>>b[i];
17     sort(b,b+k);
18     ans=0;
19     for(i=0;i<n;i++)
20     {
21         int flag=1;x=b[0]-a[i];
22         for(j=0;j<k;j++)
23         {
24             int t=b[j]-x;
25             int p=lower_bound(a+i,a+n,t)-a;
26            // cout<<"a["<<p<<"]="<<a[p]<<",t="<<t<<endl;
27             if(t!=a[p]||p>n-1){flag=0;break;}
28         }
29         if(flag) se.insert(a[i]);//相等的a[i]是一回事所以用set去重
30     }
31     cout<<se.size()<<endl;
32     return 0;
33 }

 

posted @ 2017-07-17 18:34  晴雨天  阅读(361)  评论(0编辑  收藏  举报