Still STL, map,vector,unique.
Title: A person has an initial score, telling you the score of N judges (order), each time the judges finish scoring, it is the initial score plus prefix sum, telling you k heard results (random order), asking what the initial score may be.
Idea: Preprocess the prefix sum first, enumerate the possible initial scores (k[i]-prefix sum) corresponding to each score, and record the number of occurrences of the score. If== K (satisfying the situation that K numbers all occur), it will meet the requirements, cnt+. For this problem, you have to use map, because the prefix and the number may be negative. If you open an array to hash, then add 4e6, which is dangerous. Because it might blow up by 8e6. So use map to store the key corresponding to each number, and then look it up again.
Note that if there are prefixes and the same to be removed, otherwise a ki corresponds to the same initial score will be accumulated many times, and the final answer will be larger than the real answer.
Behind the AC code, I attach my weird TLE code.
/*If I get TLE , it is good.If I get AC,it's NICE !*/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=1e7+10;
const ll mod=1e9+7;
using namespace std;
typedef long long ll;
vector <int> temp;
map <int,int > mp;
int sum[3000];
int main(void)
{
int n,k;
cin >> n>> k;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
sum[i]=t+sum[i-1];
}
//for(int i=1;i<=n;i++)
// printf("%d\n",sum[i]);
sort(sum+1,sum+1+n);
int len=unique(sum+1,sum+1+n)-(sum+1);
//for(int i=1;i<=len;i++)
// printf("%d\n",sum[i]);
mp.clear();
//int mmax=-INF;
//int mmin=INF;
for(int i=1;i<=k;i++)
{
int t;
scanf("%d",&t);
for(int j=1;j<=len;j++)
{
temp.push_back(t-sum[j]);
}
}
sort(temp.begin(),temp.end()); // With sort AC and without TLE, the test data is T earlier than the following TLE code, so I suspect that the efficiency of map insertion may be related to the monotony of the original vector. The more monotonous, the faster.
int cnt=0;
//int index=mmax;
for(int i=0;i<temp.size();i++)
{
mp[temp[i]]++;
}
for(int i=0;i<temp.size();i++)
{
if( mp[temp[i]] ==k )
mp[temp[i]]=0,cnt++;
}
printf("%d\n",cnt);
}
/ / The reason for this TLE, I guess, is that max,min is required every time.
TLE Code
/*If I get TLE , it is good.If I get AC,it's NICE !*/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=1e7+10;
const ll mod=1e9+7;
using namespace std;
typedef long long ll;
vector <int> temp;
map <int,int > mp;
int sum[3000];
int main(void)
{
int n,k;
cin >> n>> k;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
sum[i]=t+sum[i-1];
}
//for(int i=1;i<=n;i++)
// printf("%d\n",sum[i]);
sort(sum+1,sum+1+n);
int len=unique(sum+1,sum+1+n)-(sum+1);
//for(int i=1;i<=len;i++)
// printf("%d\n",sum[i]);
mp.clear();
int mmax=-INF;
int mmin=INF;
for(int i=1;i<=k;i++)
{
int t;
scanf("%d",&t);
for(int i=1;i<=len;i++)
{
mp[t-sum[i]]++;
mmax=max(mmax,t-sum[i]);
mmin=min(mmin,t-sum[i]);
}
}
int cnt=0;
int index=mmax;
for(int i=mmin;i<=index;i++)
{
//printf("%d:%d\n",i,mp[i]);
if(mp[i]==k)
cnt++;
}
printf("%d\n",cnt);
}
Finally, the encapsulation function unique in STL is explained. This function returns the last address of the sequence without duplication. It does not delete the duplicate number, but puts it behind the last address.
Being able to write unique is also a key to this problem.
What I have learned from this problem:
1. Learned the usage and principle of unique in STL.
2. When inserting data into map, first use vector to store and then sort, which can greatly save time.
3.STL is really important