#C++初学记录(set进阶#acm cf 190802 B. Subsegments)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了#C++初学记录(set进阶#acm cf 190802 B. Subsegments),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2828字,纯文字阅读大概需要5分钟。
内容图文
![#C++初学记录(set进阶#acm cf 190802 B. Subsegments)](/upload/InfoBanner/zyjiaocheng/739/98d3628786a046a09ddf28a9ebe86499.jpg)
B. Subsegments#set进阶
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
Input
The first line contains two positive integers n and k (1?≤?n?≤?105,?1?≤?k?≤?n) — the number of array elements and the length of the segment.
Then follow n lines: the i-th one contains a single number ai (?-?109?≤?ai?≤?109).
Output
Print n–k?+?1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai?+?1 … ai?+?k?-?1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
Examples
input
5 3
1
2
2
3
3
output
1
3
2
input
6 4
3
3
3
4
4
2
output
4
Nothing
3
正确代码
#include <iostream>
#include <bits/stdc++.h>
#define MAXN 100010
using namespace std;
multiset<int> A;
set<int>B;
int a[MAXN];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=k;i++)
{
int t=a[i];
if(!A.count(t))
B.insert(t);
else
B.erase(t);
A.insert(t);
}
if(!B.size()) cout<<"Nothing"<<endl;
else cout<<*--B.end()<<endl;
for(int i=k+1;i<=n;i++)
{
auto pos=A.find(a[i-k]);
A.erase(pos);
if(!A.count(a[i-k])&&B.count(a[i-k])) B.erase(a[i-k]);
else if(A.count(a[i-k])==1&&!B.count(a[i-k])) B.insert(a[i-k]);
if(!A.count(a[i]))
B.insert(a[i]);
else
B.erase(a[i]);
A.insert(a[i]);
if(!B.size()) cout<<"Nothing"<<endl;
else cout<<*--B.end()<<endl;
}
return 0;
}
题意理解
求区间内只出现过一次的元素里的最大元素 ,使用一个multiset 可以存相同的变量,使用set存单个变量, 用multiset做标记进行判断是否为单个元素,然后用set存答案, 每次移动一次,存入multiset中判断是否重复,不重复则存入set中。
set和multiset相关
set的一些基本常用用法
begin() ,返回set容器的第一个元素
end() ,返回set容器的最后一个元素
clear() ,删除set容器中的所有的元素
empty() ,判断set容器是否为空
max_size() ,返回set容器可能包含的元素最大个数
size() ,返回当前set容器中的元素个数
rbegin ,返回的值和end()相同
rend() ,返回的值和rbegin()相同
此外,还有一些操作也是set有必要学习的但不常见:
1) count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了,而multiset可以返回大于1的数
2)equal_range() ,返回一对定位器,分别表示第一个大于或等于给定关键值的元素和 第一个大于给定关键值的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于end()的值。
3)erase(iterator) ,删除定位器iterator指向的值
4)erase(first,second),删除定位器first和second之间的值
5)erase(key_value),删除键值key_value的值
内容总结
以上是互联网集市为您收集整理的#C++初学记录(set进阶#acm cf 190802 B. Subsegments)全部内容,希望文章能够帮你解决#C++初学记录(set进阶#acm cf 190802 B. Subsegments)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。