单科成绩优秀奖问题

某学校的期末考试共有n个学生参加,考试科目共有m科,学校将会给一部分学生颁发单科成绩优秀奖,获奖学生需要满足的条件是某一科的成绩是所有学生中最高的或是最高的之一
请问学校应该给多少名学生颁发单科成绩优秀奖

/* 输入第一行包含两个正整数n和m,分别代表学生人数和考试科目数量。
   (n,m <= 500)
   接下来有n行,每行有m个正整数,每个正整数在1-100之间,中间用空格隔开,
   表示每个学生的m科考试成绩 

   思路:将获取m科成绩放在m个红黑树map中,按成绩从大到小排列,人的序号作为值,
        循环从m个map中获取最高成绩(一科可能有多个最高成绩),将人的序号放在一个hash set中,
        返回这个hash set中元素数量即可
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_set>
using namespace std;

int getAwardsNum(vector<multimap<int, int, greater<int>>>& scoreArr) {
    
    
	unordered_set<int> numOfPeople;
	for (multimap<int, int, greater<int>>& e : scoreArr) {
    
    
		auto ite = e.begin();
		auto preScore = 0;
		do {
    
    
			numOfPeople.insert(ite->second);
			preScore = ite->first;
			ite++;
		} while (ite != e.end() && ite->first == preScore);
	}
	return numOfPeople.size();
}

int main() {
    
    
	int n = 0;
	int m = 0;
	cin >> n >> m;
	vector<multimap<int, int, greater<int>>> scoreArr(m, multimap<int, int, greater<int>>());
	int score = 0;
	int time = n * m;
	for (int i = 0, j = 0; i < time; i++) {
    
    
		cin >> score;
		if (i != 0 && i % m == 0) {
    
    
			j++;
		}
		scoreArr.at(i % m).insert(pair<int, int>(score, j));
	}
	cout << getAwardsNum(scoreArr) << endl;
	system("pause");
	return 0;
}

如有侵权,请联系删除,如有错误,欢迎大家指正,谢谢

猜你喜欢

转载自blog.csdn.net/xiao_ma_nong_last/article/details/105594100