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)的更多相关文章

  1. #C++初学记录(ACM试题2)

    Max Sum Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-seq ...

  2. #C++初学记录(ACM试题1)

    A - Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the ...

  3. #C++初学记录ACM补题(D. Candies!)前缀和运算。

    D - Candies!   Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...

  4. #C++初学记录(acm试题#预处理)

    C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...

  5. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  6. javaweb初学记录

    原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...

  7. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  8. #C++初学记录(奶酪#并查集)

    原题目:牛客网 题目描述 : 现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪中间有许多半径相同的球形空洞.我们可以在这块奶酪中建立空间坐标系, 在坐标系中,奶酪的下表面为 ...

  9. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

随机推荐

  1. 《区块链DAPP开发入门、代码实现、场景应用》笔记1——天外飞仙DAPP

    Solidity编程语言解决了编写智能合约的不友好的问题,但是当合约编译并部署之后,对与这些接口的访问,对于一般的使用者来说,门槛有点高, 对普通用户来说也是非常不友好,为了使广大用户理解并方便快捷的 ...

  2. 实战FFmpeg + OpenGLES--iOS平台上视频解码和播放

    一个星期的努力终于搞定了视频的播放,利用FFmpeg解码视频,将解码的数据通过OpenGLES渲染播放.搞清楚了自己想知道的和完成了自己的学习计划,有点小兴奋.明天就是“五一”,放假三天,更开心啦. ...

  3. 深入理解JVM-hotspot虚拟机对象探秘

    1.背景与大纲 在我们了解了java虚拟机的运行时数据区后,我们大概知道了虚拟机内存的概况,但是我们还是不清楚具体怎么存放的访问的: 接下来,我们将深入探讨HotSport虚拟机在java堆中对象的分 ...

  4. Hive使用过程中踩过的坑

    hive启动时错误1 Cannot execute statement:impossible to write to binary long since BINLOG_FORMAT = STATEME ...

  5. Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW(转)

    转载:https://www.cnblogs.com/yxwkf/p/4840675.html 一.Telnet 采用Telnet用来訪问远程计算机的TCP/IP协议以控制你的网络设备,相当于在离开某 ...

  6. jar - 操作jar包的工具

    jar - Manipulates Java Archive (JAR) files. jar命令是一种通用的存档和压缩工具,基于ZIP和ZLIB压缩格式. 常用格式: * 创建jar文件 jar c ...

  7. Pyspark中遇到的 java.io.IOException: Not a file 和 pyspark.sql.utils.AnalysisException: 'Table or view not found

    最近执行pyspark时,直接读取hive里面的数据,经常遇到几个问题: 1.  java.io.IOException: Not a file —— 然而事实上文件是存在的,是 hdfs 的默认路径 ...

  8. HTML&CSS基础-标签的属性

    HTML&CSS基础-标签的属性 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!-- html 根标签,一个页面中有且只有一个根标签,网页中的 ...

  9. CVE-2019-5475:Nexus2 yum插件RCE漏洞复现

    0x00 前言 如果有想一起做漏洞复现的小伙伴,欢迎加入我们,公众号内点击联系作者即可 提示:由于某些原因,公众号内部分工具即将移除,如果有需要的请尽快保存 0x01 漏洞概述 最近hackerone ...

  10. python测试开发django-rest-framework-62.基于类的视图(APIView和View)

    前言 django中编辑视图views.py有两种方式,一种是基于类的实现,另外一种是函数式的实现方式,两种方法都可以用. REST框架提供了一个APIView类,它是Django View类的子类. ...