CF831C-Jury Marks(map+vector)

来源:互联网 发布:潜在客户软件 编辑:程序博客网 时间:2024/06/08 20:14

CF831C-Jury Marks

(http://codeforces.com/problemset/problem/831/C)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
看了大神代码才理解了,自己的理解写在注释里,代码和网上的差异不大。

#include <iostream>#include <algorithm>#include <vector>#include <map>using namespace std;const int maxn = 2e3 + 10;int a[maxn], b[maxn], sum[maxn];int main(){    int k, n;    while(cin >> k >> n)    {        vector<int> v;        map<int, int> m;        for(int i = 1; i <= k; i++)        {            cin >> a[i];            sum[i] = sum[i-1] + a[i];        }        sort(sum+1, sum+1+k);        int len = unique(sum+1, sum+1+k) - sum - 1;//去重,注意:数组必须有序        for(int i = 1; i <= n; i++)        {            cin >> b[i];            for(int j = 1; j <=len; j++)            {                int x = b[i] - sum[j];//对每一个b[i]都要能够从原始成绩+某个前缀和得到,且每个b[i]对应的每个原始成绩均不相等                v.push_back(x);//输入每一个可能值            }        }        int cnt = 0;        sort(v.begin(), v.end());//去掉会超时,可能与map的内部实现有关        for(int i = 0; i < v.size(); i++)        {            m[v[i]]++;        }        for(int i = 0; i < v.size(); i++)        {            if(m[v[i]]>=n)            {                cnt++;                m[v[i]] = -1;//防止重复加            }        }        cout << cnt << endl;    }    return 0;}