int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
long long int n,k;
cin>>n;
long long int a[n],i,m=-1,j=0;
map<long long int,long long int> mp;
for(i=0;i<n;++i)
{ cin>>a[i];
}
cin>>k;
for(i=0;i<k;++i)
{
m=max(m,a[i]);
mp[a[i]]++;
}
cout<<m<<" ";
for(i=k;i<n;++i)
{
mp[a[j]]--;
if(mp[a[j]]==0)
mp.erase(mp[a[j]]);
mp[a[i]]++;
auto it=mp.end();
--it;
cout<<it->first<<" ";
j++;
}
return 0;
I don’t think so. You cannot blindly ease an element from the set. What if the element that you are erasing has an other occurrence in the current window? You wouldn’t know that if you use a set.
If you look at a multi-set in c++, the implementation is essentially a map :P. It stores a key, and the occurrence as a value. However there is something that you have to keep in mind while using a multiset.
s.erase(3) deletes all occurrences of 3. To delete a single instance:
auto it = s.find(3);
s.erase(it);
IMO, I always avoid multisets, because working with a multiset is not really the same as working with a set. So I generally use a map whenever needed.
There are some subtle differences. More on this here.