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. (转) 对svn分支合并类型和深度的理解

    合并的工作是把主干或者分支上合并范围内的所有改动列出,并对比当前工作副本的内容,由合并者手工修改冲突,然后提交到服务器的相应目录里.如果当前工作副本是主干,则合并的范围是分支上的改动,如果工作副本是分 ...

  2. &lt;C#入门经典&gt;学习笔记1之初识C#

    序言 选择< C#入门经典第五版>作为自学书籍,以此记录学习过程中的笔记与心得. C#简单介绍 1. C#是一种块结构的语言 2. C#区分大写和小写 C#变量 C#的变量定义与C语言相似 ...

  3. C语言基础知识【运算符】

    C 运算符1.运算符是一种告诉编译器执行特定的数学或逻辑操作的符号.C 语言内置了丰富的运算符,并提供了以下类型的运算符:算术运算符关系运算符逻辑运算符位运算符赋值运算符杂项运算符2.杂项运算符 ↦ ...

  4. 研究怎么运用xcode处理常见的调试问题

    本文转载至 http://blog.csdn.net/zhuzhihai1988/article/details/7749022 所谓磨刀不误砍柴工,这里菜鸟我在研究怎么运用xcode处理常见的调试问 ...

  5. pthon 基础 9.8 增加数据

    #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 2:59 #@Auther :liuzhenchuan #@File   :增加 ...

  6. 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器

    在去年我们还未开始开发基于node.js的新版本EasyDarwin RTSP开源流媒体服务器的时候,我写了一篇博客<对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更 ...

  7. spring配置中的classpath

    1 classpath指WEB-INF下面的classes目录 2 配置成classpath*的话,spring会去所有的classpath中去找,包括lib下面的jar包 对于web app而言,c ...

  8. iOS 导航引发坐标高度问题

    iOS7 后导航结构发生变化,有新的控制属性诞生,一下为两个属性引发的控制器视图高度问题 translucent  = YES  导航透明    (默认) translucent  = NO   导航 ...

  9. Servlet学习笔记【1】--- 背景和基础知识(CGI、Web服务器发展史、Servlet简介、任务、继承结构)

    本文主要讲Servlet的基础知识和背景知识. 1 CGI简介 CGI(Common Gateway Interface 公共网关接口)是WWW技术中最重要的技术之一,有着不可替代的重要地位.CGI是 ...

  10. 洛谷 P2486 [SDOI2011]染色

    题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...