CodeForces

来源:互联网 发布:ibatis sql注入 编辑:程序博客网 时间:2024/05/21 01:47

Jury Marks

Time limit : 2000 ms Memory limit : 262144 kB

description:

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个评委对他的表现依次给出一个评价分(可正可负)。每个评委给出一个评价分后,节目会报一下这个选手的当前得分。累加完全部的评价分就是选手的最终得分。可能评价分一直悬浮在屏幕上吧,这K个评价分是一直知道的。
现在Polycarp同学在看节目的时候,迷迷糊糊的记下了几个中间宣布的过程分,他看着那K个评价分,试图推理出这个选手的初始得分有几种可能。

解题思路:
1.利用评价分累加的顺序性,对评价分求一个前缀和。初始分加上第i个前缀和就对应累加上第i个评价分后报出的中间成绩。
2.用unique函数化简,去掉相邻的相同的前缀和,并得到不同的前缀和的个数。

int num = unique(grade, (grade + k))-grade;

3.随意取一个记下的数,暴力枚举它可能出现的位置,这样就能包含全部可能的情况了。
4.对每一个情况求出初始分以后,用前面求出的前缀和就能计算出每一个中间分。之后在其中寻找每一个记下的分数,只要有一个找不到这个初始分就是不对的,全都能找到就是可能的初始分。
核心代码:

        for (i = 0; i < num; i++) {            result++;                    //先假设这是可能的            init = rem[0] - grade[i];    //求出初始分数            for (int j = 0; j < k; j++) {                test[j] = init + grade[j];            }                            //得到中间分数组            sort(test, test + k);            for (int x = 0; x < n; x++) {                int y = find(test, test + k, rem[x])-test;                                      if (y == k) {          //有一个记下的分数没找到                    result--;                    break;                }            }        }

由于find的函数的需要,是要对求出的中间分数组进行预排序的。


源代码

#include<iostream>#include<stdio.h>#include<cstring>#include<vector>#include<algorithm>#include<map>#include<sstream>using namespace std;int *grade;int *rem,*test;int result;int main() {    int k, n,i,init;    while (~scanf("%d%d", &k,&n)) {        result = 0;        grade = new int[k];        test = new int[k];        for (i = 0; i < k; i++)            scanf("%d", &grade[i]);        rem = new int[n];        for (i = 0; i < n; i++)            scanf("%d", &rem[i]);        sort(rem, rem + n);        for (i = 1; i < k; i++)            grade[i] = grade[i - 1] + grade[i];        int num = unique(grade, (grade + k))-grade;        for (i = 0; i < num; i++) {            result++;            init = rem[0] - grade[i];            for (int j = 0; j < k; j++) {                test[j] = init + grade[j];            }            sort(test, test + k);            for (int x = 0; x < n; x++) {                int y = find(test, test + k, rem[x])-test;                if (y == k) {                    result--;                    break;                }            }        }        printf("%d\n", result);    }    return 0;}
原创粉丝点击