【Codeforces69E】Subsegments

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CreationAugust/article/details/46654221

题目大意:求区间内只出现过一次的元素里的最大元素
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]);
    }
}
阅读更多

没有更多推荐了,返回首页