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. laravel学习之路1:认证相关

    Laravel中Auth::guard()表示什么意思? Auth::check() 是判断用户是否登录的方法,如果使用的默认用户系统,那这样使用没问题. 但是使用两组用户的话,如何使用各组用户的功能 ...

  2. 在Ubuntu下利用Eclipse开发FFmpeg配置小结

    首先需要编译FFmpeg得到头文件和lib文件,参见:在Ubuntu下编译FFmpeg 选择File-New-C Project 选择Executable下的Empty Project,右侧选择Lin ...

  3. SimpleAdapter的用法

    学习listView的时候,按照例子设定item的布局为系统提供的simple_list_item_single_choice.xml@frameworks/base/core/res/res/lay ...

  4. 002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用

    002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用 ViewPager ViewPager类直接继承了ViewGroup类,所有它是一个容 ...

  5. Android 下拉刷新上拉载入 多种应用场景 超级大放送(上)

    转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/47707017 关于Android下拉刷新上拉载入,网上的Demo太多太多了,这 ...

  6. vue-router实现页面的整体跳转

    直接看效果图: 代码地址:https://github.com/YalongYan/vue-router-jump

  7. iOS 启动页放大淡出效果

    //屏幕宽度#define kWIDTH [UIScreen mainScreen].bounds.size.width//屏幕高度 #define kHEIGHT [UIScreen mainScr ...

  8. C语言基础知识【变量】

    C 变量1.变量其实只不过是程序可操作的存储区的名称.C 中每个变量都有特定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上.变量的名称可以由字母.数字和下 ...

  9. 在Linux先显示文件

    cat:从第一行开始显示文件内容. tac:从最后一行开始显示内容. nl:显示的时候带行号. more:一页一页显示. less:同more,可以向上翻页. head:显示文件前几行. head - ...

  10. 九度OJ 1001:A+B for Matrices

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17682 解决:7079 题目描述: This time, you are supposed to find A+B where A and ...