Codeforces 69E Subsegments (SET/线段树)

E. Subsegments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 nk + 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 - 1that 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

[题意]

 n个数,从1-n 每k个元素, 求k区间内最大出现一次的数, 没有则Nothing

[思路]

 set容器,有很好的操作性, 学习了, 哒哒哒哒哒哒哒哒哒

 用一个multiset 可以存相同的,set存单个, 用multiset做标记, set存答案, 每次移动一次, 查询第i-k个是否删除在答案中,

判断第a[i]个,

[代码实现]

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed);cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e7+5;
const int MAXN=1e5+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
	const int MT = 5e7;
	char buf[MT]; int c,sz;
	void begin(){
		c = 0;
		sz = fread(buf, 1, MT, stdin);//一次性输入
	}
	template<class T>
	inline bool read(T &t){
		while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
		if( c>=sz) return false;
		bool flag = 0; if( buf[c]== '-') flag = 1,c++;
		for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
		if(flag) t=-t;
		return true;
	}
}
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;//B.end 指的是最后个的下一个, 所以上移一次,  存的是指针

    for(int i=k+1;i<=n;i++)
    {
        auto pos=A.find(a[i-k]);
       // cout<<" + "<<*pos<<endl;
        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;
}



123

posted @ 2018-01-23 10:25  Sizaif  阅读(308)  评论(0编辑  收藏  举报