【Codeforces69E】Subsegments

题目大意:求区间内只出现过一次的元素里的最大元素
E. Subsegments
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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”.

Sample test(s)
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
由于每次查询的区间都是左端点右端点+1什么的,所以懒得写线段树直接set乱搞了一通。。。
稍微讨论一下即可。
我就是懒得写数据结构了你来咬我啊www

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#define MAXN 100010
using namespace std;
multiset<int> A;
set<int> B;
int a[MAXN];
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
    for (int i = 1;i <= k;i++)
    {
        int t = a[i];
        if (!A.count(t)) B.insert(t);
        else
            if (B.count(t)) B.erase(t);
        A.insert(t);
    }
    if (!B.size()) puts("Nothing");
    else printf("%d\n",*(--B.end()));
    for (int i = k + 1;i <= n;i++)
    {
        multiset<int>::iterator 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
            if (B.count(a[i])) B.erase(a[i]);
        if (!B.size()) puts("Nothing");
        else printf("%d\n",*(--B.end()));
        A.insert(a[i]);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值