CodeForces 831C Jury Marks(stl)

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 kjudges 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).

Example
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 - 1010 or 15 points.

In the second example there is only one correct initial score equaling to 4 002 000.


【思路】首先对评委打分做一个前缀和数组,因为每个过程分可能出现在任意位置,所以我们应该把它放在任意位置算出每种可能的初始积分,然后把这些可能值放入vector容器中,对于每个过程分重复这个操作,最后借助map容器判断vector容器中有几个初始积分出现了至少n次(每种限制条件都满足),然后输出即可。

但是由于前缀和数组可能有重复元素 如:
4 2


1 -1 1 -1


2 3
错误输出:4
正确输出:1
对此我们要对前缀和做一个去重。

[注]:

unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include<iostream.h>,具体用法如下:
    int num[100];
unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。

代码 1:

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int sum[2010];
int b[2010];

map <int ,int> mp;
vector <int> vc;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i = 1; i <= n; i++)
    {
        int x;
        scanf("%d",&x);
        sum[i] = sum[i-1] + x;
    }
    for(int i = 1; i <= k; i++)
    {
        scanf("%d",&b[i]);
    }
    sort(sum+1,sum+1+n);
    int len = unique(sum+1,sum+1+n) - (sum+1);
    int cnt = 0;
    /*Time limited
    for(int i = 1; i <= len; i++)
    {
        for(int j = 1; j <= k; j++)
        {
             int tmp = b[j] - sum[i];
             mp[tmp] ++;
             if(mp[tmp] == k)
             {
                 cnt ++;
                 mp[tmp] = 0;
             }
        }
    }*/
    for(int i = 1; i <= len; i++)
    {
        for(int j = 1; j <= k; j++)
        {
             vc.push_back(b[j] - sum[i]);
        }
    }
    sort(vc.begin(),vc.end());//不加这个和上面引用的一样time limited,可能与set实现有关
    for(int i = 0; i < vc.size(); i++)
    {
         mp[vc[i]] ++;
         if(mp[vc[i]] == k)
         {
             cnt++;
             mp[vc[i]]= -1000;
         }
    }
    printf("%d\n",cnt);
    return 0;
}


另外 还可以对b[1]的位置枚举,然后二分查找其他b[i]是不是也有对应。

[注]:

binary_search(first,last,value,comp)
first表示容器的头,last表示容器的尾,value表示想要查找的元素,comp比较函数(可以省略)
顾名思义binary_search(二分查找),需要原始序列呈有序排列,所以我们必须保证容器内的数据都是呈升序或降序排列,但是这个函数的唯一缺点就在于:它的返回值是一个布尔类型,这个函数只能告诉你 你想要查找的元素是否在这个容器中,在则返回true 否则返回false。如果你想要更多的信息(例如:知道该元素在容器中的所在位置,那么就应该用lower_bound实现)

代码二:

            

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

const int maxn = 2005;
int sum[maxn];
int a[maxn],b[maxn];
int main(void)
{
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum[i+1] = sum[i]+a[i];
    }
    for(int j=0;j<k;j++)
        scanf("%d",&b[j]);
    sort(sum+1,sum+n+1);
    int m = unique(sum+1,sum+n+1)-sum-1;
    int ans = m;
    for(int i=1;i<=m;i++)
    {
        int s = b[0]-sum[i];
        for(int j=0;j<k;j++)
        {
            if(!binary_search(sum+1,sum+m+1,b[j]-s))
            {
                ans--;
                break;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

另外,想到排重,set 就是个好东西了。


代码 3:

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;

const int maxn=2005;

int sum[maxn],b[maxn];

set<int> s;
set<int>::iterator it;

int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i = 1; i <= n; i++)
    {
        int x;
        scanf("%d",&x);
        sum[i] = sum[i-1] + x;
        s.insert(sum[i]);
    }
    for(int i = 1; i <= k; i++)
    {
        scanf("%d",&b[i]);
    }
    int ans = s.size();//枚举b[1] - sum 最多有这些可能
    for(it = s.begin(); it != s.end(); it++)
    {
        int tmp = b[1] - *it;
        for(int j = 2; j <= k; j++)
        {
            if(!s.count(b[j] - tmp))
            {
                ans--;
                break;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值