hdoj 5131 Song Jiang's rank list 【模拟】

根据《水浒传》的故事背景,本篇介绍了如何通过编程来模拟梁山好汉的战后排名系统。该系统依据每位好汉击杀敌人的数量进行排序,并在数目相同时采用姓名字典序来确定先后。此外,还提供了查询特定好汉排名的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Song Jiang's rank list

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


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
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
 
不想做模拟啊,虽说1A,但是我宁愿去做DP。

大致题意:给你N个人的名字和他们对应的武力值,让你按照武力值升序排列,若武力值相同则按名字字典序排列,然后输出排序后的排名。最后输入一个M,表示M次查询。
查询
1,他在所有人中的排名。(武力值相同的人是并列排名)
2,他在与他武力值相同的人中的排名。(若他是第一不输出)

给个例子:
A  1
B  2
C  2
D  3
E  3
F  3

新序列        所有人中排名               等武力值人中排名

D   3                    1                 ( 排名第一不输出,同下)
E   3                    1                                    2
F   3                    1                                    3
B   2                    4   
C   2                   4                                     2
A   1                    6



AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 210
using namespace std;
struct rec
{
    char str[1010];
    int val;
    int p1;//在新序列中所占位置
    int p2;//在等值序列中所占位置
};
rec num[MAXN];
bool cmp(rec a, rec b)
{
    if(a.val != b.val)
        return a.val > b.val;
    else
        return strcmp(a.str, b.str) < 0;
}
int N;
int main()
{
    int M;char ss[1010];
    while(scanf("%d", &N), N)
    {
        for(int i = 1; i <= N; i++)
        {
            scanf("%s%d", num[i].str, &num[i].val);
            num[i].p2 = 1;
        }
        sort(num+1, num+N+1, cmp);
        num[1].p1 = num[1].p2 = 1;
        printf("%s %d\n", num[1].str, num[1].val);
        for(int i = 2; i <= N; i++)
        {
            printf("%s %d\n", num[i].str, num[i].val);
            bool flag = false;;
            for(int j = i-1; j >= 1; j--)
            {
                if(num[i].val == num[j].val)//并列的
                {
                    flag = true;
                    num[i].p2++;
                }
                else//找到不同的 一位 num[j]
                    break;
            }
            if(flag)//前面有并列的
                num[i].p1 = num[i-1].p1;//就是前一位的排名
            else//没有并列的
                num[i].p1 = num[i-1].p1 + num[i-1].p2;//前一位的排名 + 在等值序列中的排名
        }
        scanf("%d", &M);
        while(M--)
        {
            scanf("%s", ss);
            for(int i = 1; i <= N; i++)
            {
                if(strcmp(num[i].str, ss) == 0)
                {
                    printf("%d", num[i].p1);
                    if(num[i].p2 != 1)//第一名不输出
                        printf(" %d", num[i].p2);
                    break;
                }
            }
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值