POJ 1581 优先队列 priority_queue -- 比赛胜者求解
題目鏈接:http://poj.org/problem?id=1581
題目大意:
給定選手姓名,及答題提交次數(shù)(提交正確前,錯(cuò)誤一次罰20分),每題的做題時(shí)間罰分(未正確作答的不罰分),最后求誰是勝出者(優(yōu)先條件,答對(duì)題目多,次要條件,總罰分最低)。
輸入格式:
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次對(duì),罰15分
第2題:3次對(duì),罰120分,另前2次提交錯(cuò)誤,罰40分
第3題:1次對(duì),罰300分
第4題:4次錯(cuò),不罰分
匯總,答對(duì)3題,罰分 = 15+120+40+300 = 475
Sample Output
Penguins 3 475
思路
- 創(chuàng)建類,每個(gè)對(duì)象對(duì)輸入的數(shù)據(jù)進(jìn)行處理計(jì)算,計(jì)算其答對(duì)的題,和罰分
- 把每個(gè)對(duì)象push進(jìn)優(yōu)先隊(duì)列
- 類創(chuàng)建 < 操作符,先按照答對(duì)題數(shù)多的優(yōu)先出隊(duì),然后相等情況下,按照罰分少的優(yōu)先出隊(duì)
Accepted 代碼如下:
/*** @description: poj 1581 judge winner 判斷勝者是誰* @author: michael ming* @date: 2019/4/5 16:00* @modified by: */ #include <string> #include <iostream> #include <queue> using namespace std; class Competitor { private:string name; //姓名int submitTime[4]; //提交次數(shù)int penaltyPoint[4]; //罰分void cal_solved_and_penalty() //計(jì)算求解題目數(shù)量,罰分{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; //求解題目數(shù)量Competitor(string &str, int* info):total_penalty(0),solved(0) //構(gòu)造函數(shù),傳入姓名和數(shù)據(jù)數(shù)組{name = str;for(int i = 0, j = 0; i < 4; ++i, ++j) //傳進(jìn)來的數(shù)據(jù)賦值給類成員{submitTime[i] = info[j++];}for(int i = 0, j = 1; i < 4; ++i, ++j){penaltyPoint[i] = info[j++];}cal_solved_and_penalty(); //計(jì)算求解題目數(shù)量,罰分}string getName() const //獲取私有成員值{return name;} }; bool operator<(const Competitor &a, const Competitor &b) //操作符 {if(a.solved < b.solved) // "<"為從大到小排列,">"為從小到大到排列return true; //解題數(shù)目多的,大先出隊(duì)else if(a.solved > b.solved)return false;else{if(a.total_penalty > b.total_penalty)return true; //罰分少的,小的先出隊(duì)elsereturn false;} } int main() {int nums_of_player; //選手個(gè)數(shù)cin >> nums_of_player;priority_queue<Competitor> playerQueue; //選手隊(duì)列int info[8]; //4個(gè)題目答題數(shù)據(jù)string name; //姓名for(int i = 0; i < nums_of_player; ++i){cin >> name; //獲取姓名for(int j = 0; j < 8; ++j) //獲取答題數(shù)據(jù)cin >> info[j];Competitor player(name,info); //根據(jù)輸入的數(shù)據(jù),建立選手類對(duì)象playerQueue.push(player); //將對(duì)象壓入優(yōu)先隊(duì)列(優(yōu)先隊(duì)列會(huì)按優(yōu)先級(jí)排序)}cout << playerQueue.top().getName() << " " << playerQueue.top().solved << " "<< playerQueue.top().total_penalty << endl; //打印隊(duì)首的類對(duì)象return 0; }總結(jié)
以上是生活随笔為你收集整理的POJ 1581 优先队列 priority_queue -- 比赛胜者求解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql format row_MyS
- 下一篇: php判断全是中文正则,php判断是否为