[ACM] HDU 5131 Song Jiang's rank list (simulation ),
Song Jiang's rank list
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission (s): 36 Accepted Submission (s): 18
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.
InputThere 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 (random SIVE ). 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
OutputFor 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
Source2014ACM/ICPC Asia Guangzhou site-reproduction competition (thanks to Huawei engineering and Peking University)
Solution:
Question: give N heroes, each of which has its own name and number of enemies to kill. Now, rank these heroes by the number of enemies to kill. The more enemies to kill, the higher the ranking, the higher the ranking if the number of enemies to be killed is the same. Given the names of n heroes and the number of enemies to kill, first output the names of the heroes and the number of enemies to kill by rank. Then, there are q inquiries. Each query includes a hero name, then, output the Primary and Secondary ranking of the hero. The primary ranking indicates the number of heroes that are more than the number of enemies killed by the hero + 1, the ranking indicates that the number of enemies killed by the hero is the same as that of the hero, but the ranking is higher than that of the top hero. If the ranking is 1, only the main ranking is output; otherwise, both are output.
Sort the struct, use map to record the hero name and the number of enemies to be killed, and then search the struct array linearly.
Code:
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <stdlib.h>#include <cmath>#include <iomanip>#include <vector>#include <set>#include <map>#include <stack>#include <queue>#include <cctype>using namespace std;#define ll long longconst int maxn=202;struct P{ string name; int k;}p[maxn];bool cmp(P a,P b){ if(a.k>b.k) return true; else if(a.k==b.k) { if(a.name<b.name) return true; return false; } return false;}int n;map<string,int>mp;void find(string str){ int major=1; int minor=1; for(int i=1;i<=n;i++) { if(p[i].name==str) { break; } if(p[i].k>mp[str]) major++; if(p[i].k==mp[str]) minor++; } cout<<major; if(minor!=1) cout<<" "<<minor<<endl; else cout<<endl;}int main(){ while(cin>>n&&n) { mp.clear(); for(int i=1;i<=n;i++) cin>>p[i].name>>p[i].k; sort(p+1,p+1+n,cmp); for(int i=1;i<=n;i++) { cout<<p[i].name<<" "<<p[i].k<<endl; mp[p[i].name]=p[i].k; } int q; string str; cin>>q; while(q--) { cin>>str; find(str); } } return 0;}