zoukankan      html  css  js  c++  java
  • PAT 1085 PAT单位排行(25)(映射、集合训练)

    1085 PAT单位排行(25 分)

    每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。

    输入格式:

    输入第一行给出一个正整数 N(≤10​5​​),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:

    准考证号 得分 学校
    

    其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。

    输出格式:

    首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

    排名 学校 加权总分 考生人数
    

    其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5整数部分考生人数是该属于单位的考生的总人数。

    学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。

    输入样例:

    10
    A57908 85 Au
    B57908 54 LanX
    A37487 60 au
    T28374 67 CMU
    T32486 24 hypu
    A66734 92 cmu
    B76378 71 AU
    A47780 45 lanx
    A72809 100 pku
    A03274 45 hypu
    

    输出样例:

    5
    1 cmu 192 2
    1 au 192 3
    3 pku 100 1
    4 hypu 81 2
    4 lanx 81 2

    我的思路 :利用map(可直接根据校名索引)录入数据,但由于map只能根据第一个参数(key)进行排序,所以只能将数据转入set集合中(按自定义的结构体比较方式排序),最后排名,可设置一个前驱分数,当前后总分不同时,更新排名。 

      注意:1、总分要取整;

                 2、最后的输出语句我换成了printf,增加效率,否则测试点5运行超时

                 3、注意sum是求和之后再取整(测试点5答案错误)

    目录

    优化后的代码:

    优化前的代码: 


    优化后的代码:

    #include<iostream>
    #include<string>
    #include<map>
    #include<set>
    #include<algorithm>
    using namespace std;
    struct school {
    	string id;					//校名
    	set<string> student;		//学生集合
    	double  sum;				//学校总分,注意最后取整
    	bool operator <( const school& b) const {      //重载<,定义学校间的比较方式
    		if ((int)sum != (int)b.sum)						//比较学校总分
    			return (int)sum > (int)b.sum;
    		else if (student.size() != b.student.size())	//比较学生人数
    			return student.size()< b.student.size();
    		else											//比较校名
    			return id < b.id;
    	}
    };
    int main(){		//权值表
    	map<char, double> trans = { make_pair('B', 2.0 / 3),make_pair('A', 1) ,make_pair('T',1.5) };	
    	map<string, school> myschool;
    	set<school> result;  //map只能根据第一个参数排序,所以后续要将map中的结构体转到集合set中
    	int N;
    	cin >> N;
    	string student_id, school_name;
    	int mark;	
    	for (int i = 0; i < N; i++) {
    		cin >> student_id >> mark >> school_name;
    		transform(school_name.begin(), school_name.end(), school_name.begin(), ::tolower);//校名转小写
    		myschool[school_name].id = school_name;
    		myschool[school_name].student.insert(student_id.substr(1, string::npos));
    		myschool[school_name].sum += trans[student_id[0]]*mark;
    	}
    	for (auto it = myschool.begin(); it != myschool.end(); it++)
    		result.insert(it->second);  //将map的结构体存到set集合中,方便排序
    	int ranking = 0 /*排名*/ , count = 0   /*计数器*/; 
    	int pre=-1;				//前驱分数
    	cout << result.size() << endl;
    	for (auto it = result.begin(); it != result.end(); it++) {
    		count++;
    		if ((int)it->sum != pre)	//与前驱分数不同时,更新ranking
    			ranking = count;
    		pre = (int)it->sum;
    		cout << ranking << " " << it->id << " " << (int)it->sum << ' ' << it->student.size() << endl;
    	}
    	return 0;
    }

    优化前的代码: 

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<set>
    using namespace std;
    struct school {
    	string id;   //再次存入id方便后来从map转入set
    	map<char, int> mark;    //这里用map记录各级比赛的成绩
    	int num = 0, sum = 0;   //num记录该校人数,sum为总分
    };
    struct cmp {         //自定义set的比较方式
    	bool operator()(const school&a, const school &b) {
    		if (a.sum == b.sum) {
    			if (a.num == b.num)
    				return a.id < b.id;
    			return a.num < b.num;
    		}
    		return a.sum > b.sum;
    	}
    };
    int main() {
    	map<string, school> k;
    	int n, t, count = 1;
    	cin >> n;
    	string id, name;
    	while (n--) {             //录入数据
    		cin >> id >> t >> name;
    		transform(name.begin(), name.end(), name.begin(), ::tolower);    //将学校的字符化为小写
    		k[name].mark[id[0]] += t;         //记住这里的+=,因为可能存在同一学校的不同学生参加了同级比赛。
    		k[name].id = name;
    		k[name].num++;        //该校人数计数器
    	}
    	set<school, cmp> rk;
    	for (auto it = k.begin(); it != k.end(); it++) {    //在计算总分后将结构体插到set里面
    		it->second.sum = (double)it->second.mark['B'] / 1.5 + it->second.mark['A'] + (double)it->second.mark['T'] * 1.5;
    		rk.insert(it->second);
    	}
    	cout << rk.size() << endl;
    	school pre;    //这里用前驱进行比较
    	auto it = rk.begin();
    	cout << count << " " << it->id << " " << it->sum << " " << it->num << endl;
    	pre = *it;
    	it++;
    	int m = 2;
    	for (; it != rk.end(); m++, it++) {
    		if (it->sum != pre.sum)   //当前后的得分不同时,更新count
    			count = m;
    		printf("%d %s %d %d
    ", count, it->id.c_str(), it->sum, it->num);
    		pre = *it;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Making a CocoaPod
    关于Http
    The podfile
    iOS 8个实用小技巧(总有你不知道的和你会用到的)
    关于深拷贝浅拷贝
    适配ios10(iTunes找不到构建版本)
    iOS 10 推送的简单使用
    __block 和 __weak的区别
    Masonry使用注意事项
    iOS数字键盘自定义按键
  • 原文地址:https://www.cnblogs.com/F-itachi/p/9974408.html
Copyright © 2011-2022 走看看