POJ 1581 优先队列 priority_queue -- 比赛胜者求解
发布日期:2021-07-01 03:39:29 浏览次数:2 分类:技术文章

本文共 2718 字,大约阅读时间需要 9 分钟。

题目链接:

题目大意:

给定选手姓名,及答题提交次数(提交正确前,错误一次罚20分),每题的做题时间罚分(未正确作答的不罚分),最后求谁是胜出者(优先条件,答对题目多,次要条件,总罚分最低)。

输入格式:

Line 1 < nTeams >
Line 2 - n+1 < Name > < p1Sub > < p1Time > < p2Sub > < p2Time > … < p4Time >

Sample Input

4

Stars 2 20 5 0 4 190 3 220
Rockets 5 180 1 0 2 0 3 100
Penguins 1 15 3 120 1 300 4 0
Marsupials 9 0 3 100 2 220 3 80

解读一下第3位答题者:

Penguins
1 15
3 120
1 300
4 0
第1题:1次对,罚15分
第2题:3次对,罚120分,另前2次提交错误,罚40分
第3题:1次对,罚300分
第4题:4次错,不罚分
汇总,答对3题,罚分 = 15+120+40+300 = 475

Sample Output

Penguins 3 475

思路

  • 创建类,每个对象对输入的数据进行处理计算,计算其答对的题,和罚分
  • 把每个对象push进优先队列
  • 类创建 < 操作符,先按照答对题数多的优先出队,然后相等情况下,按照罚分少的优先出队
    在这里插入图片描述

Accepted 代码如下:

/** * @description: poj 1581 judge winner 判断胜者是谁 * @author: michael ming * @date: 2019/4/5 16:00 * @modified by:  */#include 
#include
#include
using namespace std;class Competitor{
private: string name; //姓名 int submitTime[4]; //提交次数 int penaltyPoint[4]; //罚分 void cal_solved_and_penalty() //计算求解题目数量,罚分 {
for(int i = 0; i < 4; ++i) {
if(penaltyPoint[i] != 0) {
solved++; total_penalty += 20*(submitTime[i]-1) + penaltyPoint[i]; } } }public: int total_penalty; //罚分 int solved; //求解题目数量 Competitor(string &str, int* info):total_penalty(0),solved(0) //构造函数,传入姓名和数据数组 {
name = str; for(int i = 0, j = 0; i < 4; ++i, ++j) //传进来的数据赋值给类成员 {
submitTime[i] = info[j++]; } for(int i = 0, j = 1; i < 4; ++i, ++j) {
penaltyPoint[i] = info[j++]; } cal_solved_and_penalty(); //计算求解题目数量,罚分 } string getName() const //获取私有成员值 {
return name; }};bool operator<(const Competitor &a, const Competitor &b) //操作符{
if(a.solved < b.solved) // "<"为从大到小排列,">"为从小到大到排列 return true; //解题数目多的,大先出队 else if(a.solved > b.solved) return false; else {
if(a.total_penalty > b.total_penalty) return true; //罚分少的,小的先出队 else return false; }}int main(){
int nums_of_player; //选手个数 cin >> nums_of_player; priority_queue
playerQueue; //选手队列 int info[8]; //4个题目答题数据 string name; //姓名 for(int i = 0; i < nums_of_player; ++i) {
cin >> name; //获取姓名 for(int j = 0; j < 8; ++j) //获取答题数据 cin >> info[j]; Competitor player(name,info); //根据输入的数据,建立选手类对象 playerQueue.push(player); //将对象压入优先队列(优先队列会按优先级排序) } cout << playerQueue.top().getName() << " " << playerQueue.top().solved << " " << playerQueue.top().total_penalty << endl; //打印队首的类对象 return 0;}

转载地址:https://michael.blog.csdn.net/article/details/89044234 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:算法--递归--走台阶问题(2种递归+递归改循环)
下一篇:POJ 2259 team queue团队排队问题

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年05月06日 18时13分55秒