C. Jury Marks
2 seconds
256 megabytes
standard input
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.
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.
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).
4 1
-5 5 0 20
10
3
2 2
-2000 -2000
3998000 4000000
1
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的更多相关文章
- Codeforces831C Jury Marks
C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- C. Jury Marks 思维
C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...
- C. Jury Marks 思维题
http://codeforces.com/contest/831/problem/C 做的时候想不到,来了个暴力. 对于每个b[i],枚举每一个a[i],就有1个可能的情况. 然后用vector存起 ...
- CF831C Jury Marks
思路: 关键在于“插入”一个得分之后,其他所有得分也随之确定了. 实现: #include <iostream> #include <cstdio> #include < ...
- 【Codeforces Round #424 (Div. 2) C】Jury Marks
[Link]:http://codeforces.com/contest/831/problem/C [Description] 有一个人参加一个比赛; 他一开始有一个初始分数x; 有k个评委要依次对 ...
- #424 Div2 Problem C Jury Marks (二分 && 暴力 && std::unique && 思维)
题目链接 :http://codeforces.com/contest/831/problem/C 题意 :选手有一个初始积分,接下来有k个裁判为他加分或减分(时间顺序给出),然后告诉你n(1< ...
- 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 ...
- 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 ...
- CF-831C
C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- python selenium2示例 - 日志管理
logger继承图 前言 在自动化测试实践过程中,必不可少的就是进行日志管理,方便调试和生产问题追踪,python提供了logging模块来进行日志的管理.下面我们就logging模块的学习和使用进行 ...
- linux 面试题
[题目]一个数组,输出重复次数最多的前你n位,倒序输出 [答案] catwords.txt | sort | uniq -c | sort -k1,1nr | head -n [解析] sor ...
- 时下世界上最先进的液晶面板技术---ips
IPS是英文In-Plane Switching的缩写,英文含义为平面转换屏幕技术,由日立公司于2001推出的液晶面板技术,俗称“Super TFT”,是目前世界上最先进的液晶面板技术,目前已经广泛使 ...
- linux 下配置jdk
去java官方地址下载相应的源码包我下载的是1.8.0放在usr/local目录下 export JAVA_HOME=/usr/local/jdk1.8.0export PATH=$JAVA_HOME ...
- vue项目在APP禁止页面缩放
veu-cli自动生成的项目中,index.html中meta 标签内容如下,放在手机上浏览 是可以放大缩小的<meta name="viewport" content=&q ...
- Asp.Net mvc4 项目 在vs中调试正常 在IIS发布后连接oracle数据库时提示数据库连接关闭
解决办法: 1.打开iis,找到发布的程序 2.右键单击“TAKANAPP” 从右键菜单选择“管理应用程序”--“高级设置....” 在打开的高级设置 面板 查看对应的应用程序池 名称 3.设置应用 ...
- Failed to decode response: zlib_decode(): data error Retrying with degraded;
composer update的时候出现: Failed to decode response: zlib_decode(): data error Retrying with degraded: 执 ...
- php自定义的格式化时间示例代码
时间刚好是5分钟前,则对应的时间戳就会被格式化为5分钟前,自定义的格式化时间方法如下,感兴趣的朋友可以参考下 如:时间刚好是5分钟前,则对应的时间戳就会被格式化为5分钟前,不多说了,直接贴上代码: 复 ...
- sonarQube使用maven进行检查
1.在maven的中找到setting配置文件.在setting.xml中增加sonarqube配置.如下: <profiles> <profile> <id>so ...
- LeetCode:用HashMap解决问题
LeetCode:用HashMap解决问题 Find Anagram Mappings class Solution { public int[] anagramMappings(int[] A, i ...