UVALive Problem 7073 Song Jiang's rank list(排序)——2014ACM/ICPC亚洲区广州站

来源:互联网 发布:sqlserver数据库置疑 编辑:程序博客网 时间:2024/05/16 08:56

此文章可以使用目录功能哟↑(点击上方[+])

 UVALive Problem 7073 Song Jiang's rank list

Accept: 0    Submit: 0
Time Limit: 3.000 seconds

 Problem Description

≪ ShuiHuZhuan! ≫, also ≪ W aterM argin ≫ was written by Shi Nai’an — an writer of Yuan and Ming dynasty. ≪ ShuiHuZhuan! ≫ 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

5
WuSong 12
LuZhishen 12
SongJiang 13
LuJunyi 1
HuaRong 15
5
WuSong
LuJunyi
LuZhishen
HuaRong
SongJiang
0

 Sample Output

HuaRong 15
SongJiang 13
LuZhishen 12
WuSong 12
LuJunyi 1
3 2
5
3
1
2

 Problem Idea

解题思路:

【题意】
每次战斗之后,宋江都要给n个好汉排位

排位规则如下:

①战斗中杀人数越多的名次越靠前

②杀人数相同的,名字字典序小的人名次靠前

要求输出排好名次之后每个好汉的名字及杀人数

接着,m次询问,问某某好汉排位之后是第几名

该名次分为major名次和minor名次

major为杀人数排名,杀人数一样的并列,如样例中,WuSong和LuZhishen杀人数均为12,并列第3,而第4因此空出

minor为杀人数相同的几个人的排名


【类型】
sort排序

【分析】
显然,此题的排序可以直接用algorithm里封装好的sort函数来进行排序

只需要写一下cmp,定一下排序的优先级就可以了

而后面的m次询问,只需要将排好序的名次表遍历一遍,就可以找到对应好汉的排名

【时间复杂度&&优化】
O(m×n)

题目链接→UVALive Problem 7073 Song Jiang's rank list

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 205;const int M = 55;const int inf = 1000000007;const int mod = 1000003;struct outlaws{    char name[M];    int k;}s[N];bool cmp(outlaws x,outlaws y){    if(x.k!=y.k)        return x.k>y.k;    return strcmp(x.name,y.name)<0;}char ch[M];int main(){    int n,i,j,m,major,minor;    while(scanf("%d",&n)&&n)    {        for(i=0;i<n;i++)            scanf("%s%d",s[i].name,&s[i].k);        sort(s,s+n,cmp);        for(i=0;i<n;i++)            printf("%s %d\n",s[i].name,s[i].k);        scanf("%d",&m);        for(i=0;i<m;i++)        {            scanf("%s",ch);            for(major=1,minor=j=0;j<n;j++)            {                if(!j||s[j].k!=s[j-1].k)                    major+=minor,minor=1;                else                    minor++;                if(!strcmp(s[j].name,ch))                {                    printf("%d",major);                    if(minor!=1)                        printf(" %d",minor);                    puts("");                    break;                }            }        }    }    return 0;}
菜鸟成长记

1 0
原创粉丝点击