第一行爲整數n, k。(1 <= n <= 5 * 10^6,1 <= k <= 1000) n代表有多少次操作,k代表窗口大小。 接下來的n行,每行代表一次操作。每行第一個整數爲操作數。 操作數1:用戶訪問 輸入格式: <1, id> 用戶ID爲[0, INT_MAX]閉區間內的整數。代表擁有此ID的用戶對網站進行了一次訪問,窗口進行相應移動。 操作數2:詢問衆數 輸入格式:<2> 輸出窗口內的衆數,如果有多個,輸出ID最小的那個。 p.s. 對於詢問衆數的操作,窗口保證不空 p.s.s. 對於詢問衆數的操作,窗口可能不滿
對於詢問衆數的操作,每行輸出一個整數。
10 5 1 2 1 1 1 2 1 1 1 2 1 1 2 1 3 1 3 2
1 1
set+map+卡常~
用map記錄現在該表號的出現次數,然後用set維護最大值。
居然被卡常了,然後又去學了輸出優化,有趣。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
using namespace std;
int n,x,k,pos,a[5000010],tot,sta[12],top;
struct node{
int x,num;
bool operator < (const node&u) const
{
return num==u.num ? x<u.x:num>u.num;
}
};
map<int,int> f;
multiset<node> s;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void ins(int u)
{
if(f[u]) s.erase(s.find((node){u,f[u]}));
f[u]++;
s.insert((node){u,f[u]});
}
void del(int u)
{
s.erase(s.find((node){u,f[u]}));f[u]--;
if(f[u]) s.insert((node){u,f[u]});
}
void write(int u)
{
if(!u)
{
puts("0");return;
}
top=0;
while(u>9) sta[++top]=u%10,u/=10;sta[++top]=u;
for(int i=top;i;i--) putchar(sta[i]+'0');puts("");
}
int main()
{
n=read();k=read();
for(int i=1;i<=n;i++)
{
pos=read();
if(pos==1)
{
a[++tot]=read();
if(tot>k) del(a[tot-k]);ins(a[tot]);
}
else write((*s.begin()).x);
}
return 0;
}