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

本文主要是介绍UVALive Problem 7073 Song Jiang's rank list(排序)——2014ACM/ICPC亚洲区广州站,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

 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;elseminor++;if(!strcmp(s[j].name,ch)){printf("%d",major);if(minor!=1)printf(" %d",minor);puts("");break;}}}}return 0;
}
菜鸟成长记

这篇关于UVALive Problem 7073 Song Jiang's rank list(排序)——2014ACM/ICPC亚洲区广州站的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/226422

相关文章

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.