(CodeForces
来源:互联网 发布:调研数据分析报告模板 编辑:程序博客网 时间:2024/06/04 19:12
(CodeForces - 831C)Jury Marks
time limit per test:2 seconds
memory limit per test:256 megabytes
inputs:tandard input
outputs:tandard 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.
题目大意:有k个评委给一个人打分,第i个评委打的分数是a[i], 这个记得自己比赛过程中的n个分数,b[i],显然他想知道比赛开始前自己的分数(注意这个分数可以是负数)。
思路:先将评委评分前缀和排序去重,然后枚举所有可能的原始成绩,如果所有记录的分数都能由该原始成绩加某个前缀和得到,那么该原始成绩就符合条件,否则不符合。判断成绩是否符合条件可以用二分或者用set判断。
ps:我程序里用到了STL中的两个函数。
①int len=unique(sum,sum+k)-sum
返回的是将sum数组去重后的sum数组长度,尤其需要注意的是unique函数并没有将相同的元素删除,而是将他们隐藏在去重后的sum数组最后面,即如果访问sum[len]…sum[k-1]这些元素可以得到哪些重复的元素。
②binary_search(first,last,val)
这个函数可以在非递减序列[first,last)中二分查找val,如果找到返回1,否则返回0.
#include<cstdio>#include<algorithm>using namespace std;const int maxn=2005;int a[maxn],b[maxn],sum[maxn];int main(){ int k,n; while(~scanf("%d%d",&k,&n)) { for(int i=0;i<k;i++) { scanf("%d",a+i); if(i==0) sum[i]=a[i]; else sum[i]=sum[i-1]+a[i]; } sort(sum,sum+k); int len=unique(sum,sum+k)-sum; int ans=len; for(int i=0;i<n;i++) scanf("%d",b+i); for(int i=0;i<len;i++) { int tmp=b[0]-sum[i]; for(int j=1;j<n;j++) if(!binary_search(sum,sum+len,b[j]-tmp)) { ans--; break; } } printf("%d\n",ans); } return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- 题目14:会场安排问题
- 数据结构之链表(单链表、循环链表)
- ubuntu 16.04窗口总是出现在左上角问题
- Spark 操作数据库API
- Redis 缓存失效机制
- (CodeForces
- Nginx编译与安装
- JSP常用标签——JSTL标签和EL表达式
- Android studio中使用butterknife注解
- 移动支付--微信公众号支付开发
- 线程的五种状态详解
- Android中Kotlin语言构造函数的使用
- pnp4nagios画图出现 XML file XXXX not found 问题
- maven如何引入自定义jar或第三方jar