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出现的次数有错误的计算。

 #include<bits/stdc++.h>
using namespace std;
vector<int>vec;
map<int,int>mp; int main()
{
int n,k,i,j,x,a[],ans;
cin>>n>>k;
cin>>a[];
for(i=;i<n;i++)
{
cin>>x;
a[i]=a[i-]+x;
}
sort(a,a+n);
int len=unique(a,a+n)-a; //前缀和去重
for(i=;i<k;i++)
{
cin>>x;
for(j=;j<len;j++)
{
vec.push_back(x-a[j]);
}
}
sort(vec.begin(),vec.end());//去掉会超时,可能与map的内部实现有关
for(i=;i<vec.size();i++)
{
mp[vec[i]]++;
//cout<<vec[i]<<" ";
}
//cout<<endl;
ans=;
for(i=;i<vec.size();i++)
{
if(mp[vec[i]]>=k)
{
ans++;
mp[vec[i]]=;
}
}
cout<<ans<<endl;
return ;
}

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

 #include<bits/stdc++.h>
using namespace std;
set<int>se;
int main()
{
int n,k,i,j,a[],b[],ans,x;
cin>>n>>k;
cin>>x;
for(i=;i<n;i++)
{
cin>>x;
a[i]=a[i-]+x;
}
sort(a,a+n);
for(i=;i<k;i++)
cin>>b[i];
sort(b,b+k);
ans=;
for(i=;i<n;i++)
{
int flag=;x=b[]-a[i];
for(j=;j<k;j++)
{
int t=b[j]-x;
int p=lower_bound(a+i,a+n,t)-a;
// cout<<"a["<<p<<"]="<<a[p]<<",t="<<t<<endl;
if(t!=a[p]||p>n-){flag=;break;}
}
if(flag) se.insert(a[i]);//相等的a[i]是一回事所以用set去重
}
cout<<se.size()<<endl;
return ;
}

C. Jury Marks的更多相关文章

  1. Codeforces831C Jury Marks

    C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. C. Jury Marks 思维

    C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...

  3. C. Jury Marks 思维题

    http://codeforces.com/contest/831/problem/C 做的时候想不到,来了个暴力. 对于每个b[i],枚举每一个a[i],就有1个可能的情况. 然后用vector存起 ...

  4. CF831C Jury Marks

    思路: 关键在于“插入”一个得分之后,其他所有得分也随之确定了. 实现: #include <iostream> #include <cstdio> #include < ...

  5. 【Codeforces Round #424 (Div. 2) C】Jury Marks

    [Link]:http://codeforces.com/contest/831/problem/C [Description] 有一个人参加一个比赛; 他一开始有一个初始分数x; 有k个评委要依次对 ...

  6. #424 Div2 Problem C Jury Marks (二分 && 暴力 && std::unique && 思维)

    题目链接 :http://codeforces.com/contest/831/problem/C 题意 :选手有一个初始积分,接下来有k个裁判为他加分或减分(时间顺序给出),然后告诉你n(1< ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

    http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组

    A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. CF-831C

    C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. linux vmware提示:此虚拟机似乎正在使用中,取得该虚拟机的所有权失败错误

    用vm的时候,没有挂起和关闭虚拟机,直接关实体机.然后不幸的就异常了. 然后今天用的时候提示 此虚拟机似乎正在使用中. 如果此虚拟机已在使用中,请按“取消”按钮,以免损坏它.如果此虚拟机未使用,请按“ ...

  2. FMM和BMM的python代码实现

    FMM和BMM的python代码实现 FMM和BMM的编程实现,其实两个算法思路都挺简单,一个是从前取最大词长度的小分句,查找字典是否有该词,若无则分句去掉最后面一个字,再次查找,直至分句变成单词或者 ...

  3. Hadoop学习笔记(二)——zookeeper使用和分析

    分布式架构是中心化的设计.就是一个主控机连接多个处理节点,因此保证主控机高可用性十分关键.分布式锁是解决该问题的较好方案,多主控机抢一把锁.Zookeeper就是一套分布式锁管理系统,用于高可靠的维护 ...

  4. 【Atheros】Ath9k速率调整算法源码走读

    上一篇文章介绍了驱动中minstrel_ht速率调整算法,atheros中提供了可选的的两种速率调整算法,分别是ath9k和minstrel,这两个算法分别位于: drivers\net\wirele ...

  5. 从sql走向linq的我撞死在起点上

    [本文纯个人理解,错误轻喷,非常希望能有大神指点] A left (outer) join B on A.bid=B.id 上面这句话叫做左连接,原因是left(左)join(加入,连入)被译为左连接 ...

  6. Cocos2d-x中使用第三方so库

    项目中假设使用到第三方的SDK,大多数是以.so动态共享库的文件打包给我们使用.怎样使用他们,见以下分析. 1.获得库文件 假如我们得到的库文件是libxxx.so(注:关于.so文件的命名方式,可百 ...

  7. spring注解集合

    spring篇 @Autowired Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. Spring 通过一个 BeanPos ...

  8. 如何基于EasyDSS体系的全套SDK完成各种场景下的视频应用需求

    需求背景 回顾EasyDSS的发展过程,基本上保持的是先局部后系统.先组件后平台的发展方式,一步一步夯实每一个细节功能点,从最基础.最兼容的音视频数据的拉流获取,到高效的.全兼容的数据推流,再到流媒体 ...

  9. What I learned from competing against a ConvNet on ImageNet

    http://karpathy.github.io/2014/09/02/what-i-learned-from-competing-against-a-convnet-on-imagenet/

  10. 2017-2018-1 20179209《Linux内核原理与分析》第八周作业

    Linux内核如何装载和启动一个可执行程 一.实验 1.1理解编译链接的过程和ELF可执行文件格式. 1.1.1编译链接过程 能用图说明的问题,就少用文字描述: 1.1.2ELF可执行文件 ELF可执 ...