L2-021 点赞狂魔 (25 分) set去重,结构体排序
L2-021 点赞狂魔 (25 分)
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int p;
class people
{
public :
string name;
int cnt;
int k;
people(){
}
people(string a,int b,int c)
{
name=a;
cnt=b;
k=c;
}
} a[105];
bool cmp(people a,people b)
{
if(a.cnt!=b.cnt)return a.cnt>b.cnt;
else{
return a.k<b.k;
}
}
int main()
{
int n;cin>>n;
while(n--)
{
string str;int k,t,kk;
cin>>str;
s.clear();
cin>>k;kk=k;
while(k--)
{
cin>>t;
s.insert(t);
}
a[p++]=people(str,s.size(),kk);
}
sort(a,a+p,cmp);
for(int i=0;i<3;++i)
{
if(i!=2)
{
if(i<=p-1)cout<<a[i].name<<" ";
else cout<<"- ";
}
else
{
if(i<=p-1)cout<<a[i].name;
else cout<<"-";
}
}
return 0;
}
更新
#include <bits/stdc++.h>
using namespace std;
#define map unordered_map
map<int,int> mp;
struct node{
int cnt;
double avg;
};
map<string,node> cnt;
class cmp{
public :
bool operator ()(const pair<string,node> a,const pair<string,node> b){
if(a.second.cnt!=b.second.cnt)return a.second.cnt>b.second.cnt;
return a.second.avg<b.second.avg;
}
};
int main()
{
int n;cin>>n;
for(int i=0;i<n;++i)
{
string s;int k,x;
cin>>s>>k;
mp.clear();
for(int j=0;j<k;++j)
{
cin>>x;
mp[x]++;
}
cnt[s].cnt=mp.size();
cnt[s].avg=k*1.0/mp.size();
}
vector<pair<string,node> > v(cnt.begin(),cnt.end());
sort(v.begin(),v.end(),cmp());
if(v.size()>=3){
for(int i=0;i<3;++i){
if(i!=2)cout<<v[i].first<<" ";
else cout<<v[i].first;
}
}else {
for(int i=0;i<3;++i){
if(i<v.size())cout<<v[i].first<<" ";
else {
if(i!=2)cout<<"- ";
else cout<<"-";
}
}
}
return 0;
}