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. PHP浮点数运算精度造成的,订单金额支付经常少1分的问题

    最近碰见一个奇怪的问题,商城通过微信支付的订单经常少一分钱,经过排查是PHP浮点运算精度问题造成的 由PHP浮点数运算精度造成的,鸟哥的Bolg有详细的说明.http://www.laruence.c ...

  2. iOS图片无损拉伸

    一张图片如果放大的话一般情况下会失真,如果该图片是规则的,比如这个聊天气泡,可以用如下代码来设置 UIImage *rightImg = [UIImage imageNamed:@"Sen ...

  3. 闪屏Flash动画

    这个也比较简单,之前也做过不少 今天这个就为了方便日后使用,希望大家都可以借鉴借鉴啊! @ViewInject(R.id.linMain) private LinearLayout linMain; ...

  4. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  5. Collecting Bugs (概率dp)

    Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...

  6. elasticsearch从入门到出门-02-简单的CRUD

    操作背景: 电商网站上面的一个商品的增删改查: es 能接受的都是JSON格式的数据 Es 提供了一套简单的集群信息健康监控的api GET /_cat/health?v   epoch      t ...

  7. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  8. cocos2d-x CCControl控件

    感谢点评与关注.欢迎转载与分享.勤奋努力,持之以恒! CCControlSlider 滑动条 void HelloWorld::myInit10() { CCSize size = CCDirecto ...

  9. 关于 IN UPDATE TASK

    [转 http://blog.sina.com.cn/s/blog_6f74e6d50100sq57.html]更新程序必须用一个特殊的FM(update module)来实现. 1.Exportin ...

  10. 【深度学习】ubuntu16.04下安装opencv3.4.0

    1.首先安装一些编译工具 # 安装编译工具 sudo apt-get install build-essential # 安装依赖包 sudo apt-get install cmake git li ...