hdu 5131 (2014广州现场赛 E题)
题意:对给出的好汉按杀敌数从大到小排序,若相等,按字典序排。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
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <map> 7 # include <cmath> 8 # include <queue> 9 # include <list> 10 # define LL long long 11 using namespace std ; 12 13 struct ren 14 { 15 char name[100] ; 16 int num; 17 }a[300]; 18 19 bool cmp(ren a,ren b) 20 { 21 if(a.num!=b.num) 22 return a.num > b.num; 23 return strcmp(a.name,b.name) <0; 24 } 25 26 map<string,int> mp1 ; 27 map<string,int> mp2 ; 28 29 int main() 30 { 31 //freopen("in.txt","r",stdin) ; 32 int n , m ; 33 while(scanf("%d" , &n) != EOF) 34 { 35 if (n == 0) 36 break ; 37 mp1.clear() ; 38 mp2.clear() ; 39 int i , j ; 40 char ch[200] ; 41 for (i = 0 ; i < n ; i++) 42 scanf("%s %d" , a[i].name , &a[i].num) ; 43 sort(a , a+n , cmp) ; 44 int sum = 0 ; 45 for (i = 0 ; i < n ; i++) 46 { 47 printf("%s %d\n" , a[i].name , a[i].num) ; 48 } 49 50 for (i = 0 ; i < n ; i++) 51 { 52 sum=0; 53 for(j = 0 ; j < i ; j++) 54 { 55 if(a[j].num > a[i].num) 56 sum++; 57 } 58 mp1[a[i].name]=sum+1; 59 } 60 for (i = 0 ; i < n ; i++) 61 { 62 sum=0; 63 for(j = 0 ; j < i ; j++) 64 { 65 if(a[i].num==a[j].num) 66 sum++; 67 } 68 mp2[a[i].name]=sum+1; 69 } 70 scanf("%d" , &m) ; 71 while(m--) 72 { 73 scanf("%s" , ch) ; 74 if (mp2[ch] == 1) 75 printf("%d\n" , mp1[ch]) ; 76 else 77 printf("%d %d\n" , mp1[ch] , mp2[ch]) ; 78 } 79 } 80 return 0; 81 }