hdu 5131 Song Jiang's rank list(模拟)

来源:互联网 发布:oracle sql优化 编辑:程序博客网 时间:2024/05/01 11:03

Song Jiang's rank list

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 662    Accepted Submission(s): 329


Problem Description
《Shui Hu Zhuan》,also 《Water Margin》was written by Shi Nai'an -- an writer of Yuan and Ming dynasty. 《Shui Hu Zhuan》is one of the Four Great Classical Novels of Chinese literature. It tells a story about 108 outlaws. They came from different backgrounds (including scholars, fishermen, imperial drill instructors etc.), and all of them eventually came to occupy Mout Liang(or Liangshan Marsh) and elected Song Jiang as their leader.

In order to encourage his military officers, Song Jiang always made a rank list after every battle. In the rank list, all 108 outlaws were ranked by the number of enemies he/she killed in the battle. The more enemies one killed, one's rank is higher. If two outlaws killed the same number of enemies, the one whose name is smaller in alphabet order had higher rank. Now please help Song Jiang to make the rank list and answer some queries based on the rank list.
 

Input
There are no more than 20 test cases.

For each test case:

The first line is an integer N (0<N<200), indicating that there are N outlaws.

Then N lines follow. Each line contains a string S and an integer K(0<K<300), meaning an outlaw's name and the number of enemies he/she had killed. A name consists only letters, and its length is between 1 and 50(inclusive). Every name is unique.

The next line is an integer M (0<M<200) ,indicating that there are M queries.

Then M queries follow. Each query is a line containing an outlaw's name.
The input ends with n = 0
 

Output
For each test case, print the rank list first. For this part in the output ,each line contains an outlaw's name and the number of enemies he killed.

Then, for each name in the query of the input, print the outlaw's rank. Each outlaw had a major rank and a minor rank. One's major rank is one plus the number of outlaws who killed more enemies than him/her did.One's minor rank is one plus the number of outlaws who killed the same number of enemies as he/she did but whose name is smaller in alphabet order than his/hers. For each query, if the minor rank is 1, then print the major rank only. Or else Print the major rank, blank , and then the minor rank. It's guaranteed that each query has an answer for it.
 

Sample Input
5WuSong 12LuZhishen 12SongJiang 13LuJunyi 1HuaRong 155WuSongLuJunyiLuZhishenHuaRongSongJiang0
 

Sample Output
HuaRong 15SongJiang 13LuZhishen 12WuSong 12LuJunyi 13 25312
 

Source

2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)  

题意: 给出人名和杀敌数,然后按照杀敌数排名,如果杀敌数一样,按照名字字典序排列。然后又n组询问
每一组询问给出一个人名,输出这个人的主要排名和次要排名,如果杀敌数一样那么主要排名就是一样的并
取排名靠前的名次,次要排名是,排名在这个人前边的人有几个和他杀敌数是一样的,比如有三个人杀敌数
一样,名字和杀敌数分别是:
zzz 33; aaa 33; ddd 33; 
排完序是: aaa ddd zzz
如果询问 aaa 那么输出 1 ,询问ddd 那么输出 1 2(他前边的aaa和他杀敌一样)询问zzz 那么输出
1 3(同理)
理解了题意剩下的就是模拟了。 

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;#define M 300int n,m;int num[M];struct node{char name[80];int kills,rank;}s[M];bool cmp(node a,node b){if(a.kills!=b.kills)return a.kills>b.kills;elsereturn strcmp(a.name,b.name)<0;}int main(){char ch[80];int i,j;while(scanf("%d",&n),n){for(i=0;i<n;i++){scanf("%s %d",s[i].name,&s[i].kills);}sort(s,s+n,cmp);for(i=0;i<n;i++){printf("%s %d\n",s[i].name,s[i].kills);s[i].rank=i+1;//先把每个人的排名初始化 }int c=0;//c记录杀敌数一样的个数 memset(num,0,sizeof(num));for(i=1;i<n;i++){if(s[i].kills==s[i-1].kills){c++; s[i].rank=s[i-1].rank;//如果杀敌数一样,主名次也一样 num[i]=c;//num[i]表示i前边有几个和他杀敌数一样的 }else{num[i-1]=c;c=0;}}scanf("%d",&m);while(m--){scanf("%s",ch);for(i=0;i<n;i++){if(strcmp(ch,s[i].name)==0){if(num[i]>0){printf("%d %d\n",s[i].rank,num[i]+1);}else printf("%d\n",s[i].rank);}}}}return 0;} 


0 0
原创粉丝点击