hdu 5131 (2014广州现场赛 E题)

时间:2022-08-02 17:30:40

题意:对给出的好汉按杀敌数从大到小排序,若相等,按字典序排。M个询问,询问名字输出对应的主排名和次排名。(排序之后)主排名是在该名字前比他杀敌数多的人的个数加1,次排名是该名字前和他杀敌数相等的人的个数加1,(也就是杀敌数相等,但是字典序比他小的人数加1)

Sample Input
5
WuSong 12
LuZhishen 12
SongJiang 13
LuJunyi 1
HuaRong 15
5 //m
WuSong
LuJunyi
LuZhishen
HuaRong
SongJiang
0

Sample Output
HuaRong 15
SongJiang 13
LuZhishen 12
WuSong 12
LuJunyi 1
3 2//主排名 次排名
5
3
1
2

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <string>
# include <map>
# include <cmath>
# include <queue>
# include <list>
# define LL long long
using namespace std ; struct ren
{
char name[] ;
int num;
}a[]; bool cmp(ren a,ren b)
{
if(a.num!=b.num)
return a.num > b.num;
return strcmp(a.name,b.name) <;
} map<string,int> mp1 ;
map<string,int> mp2 ; int main()
{
//freopen("in.txt","r",stdin) ;
int n , m ;
while(scanf("%d" , &n) != EOF)
{
if (n == )
break ;
mp1.clear() ;
mp2.clear() ;
int i , j ;
char ch[] ;
for (i = ; i < n ; i++)
scanf("%s %d" , a[i].name , &a[i].num) ;
sort(a , a+n , cmp) ;
int sum = ;
for (i = ; i < n ; i++)
{
printf("%s %d\n" , a[i].name , a[i].num) ;
} for (i = ; i < n ; i++)
{
sum=;
for(j = ; j < i ; j++)
{
if(a[j].num > a[i].num)
sum++;
}
mp1[a[i].name]=sum+;
}
for (i = ; i < n ; i++)
{
sum=;
for(j = ; j < i ; j++)
{
if(a[i].num==a[j].num)
sum++;
}
mp2[a[i].name]=sum+;
}
scanf("%d" , &m) ;
while(m--)
{
scanf("%s" , ch) ;
if (mp2[ch] == )
printf("%d\n" , mp1[ch]) ;
else
printf("%d %d\n" , mp1[ch] , mp2[ch]) ;
}
}
return ;
}