set结构体中<号的重载(参考某位博主的方法完成)
//
// main.cpp
// test6
//
// Created by Jacky Roth on 2019/2/28.
// Copyright © 2019 Jacky Roth. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int maxn=50010;
int num[maxn];
struct node {
int id,times=0;
friend bool operator<(const node& a,const node& b){
if(a.times!=b.times){
return a.times>b.times;
}else{
return a.id<b.id;
}
}
};
set<node>res;
int main(int argc, const char * argv[]) {
memset(num, 0, sizeof(num));
int N,k,data;
scanf("%d%d",&N,&k);
for (int i=0; i<N; i++) {
scanf("%d",&data);
if(i){
int count=0;
printf("%d:",data);
for (auto it =res.begin(); it!=res.end()&&count<k; it++,count++) {
printf(" %d",it->id);
}
printf("\n");
}
auto it=res.find(node{data,num[data]});
if (it!=res.end()) {
res.erase(it);
}
res.insert(node{data,++num[data]});
}
return 0;
}