[ABC273C] (K+1)-th Largest Number
合理使用 C++ STL,在比赛中有利于减少失误,加快打题速度。这道题使用 STL map 很好做。
根据题意,我们先把数列去重观察。设数列构成大小为 \(M\) 的集合 \(S=\{S_1,S_2...,S_M\}\)。
刚好有 \(K\) 个数比目标值大,其实就是说目标值在集合中是第 \(K+1\) 大的。
那么题目就变成了:输出 \(S\) 中第 \(K+1\) 大的数在数列中的个数。如果 \(K>M\),输出 0 即可。这波和标题配合得很好啊。
考虑使用 map,其中第一关键字储存数值,第二关键字储存个数。 map 自带排序,所以不必再劳神排序了。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
map<int,int> m;
signed main(){
scanf("%d",&n);
for(int i=1,a;i<=n;i++){
scanf("%d",&a);
m[a]++;
}
map<int,int>::reverse_iterator it;
for(it=m.rbegin();it!=m.rend();it++,cnt++)
printf("%d\n",(*it).second);
for(;cnt<n;cnt++)puts("0");
return 0;
}

浙公网安备 33010602011771号