1141 PAT Ranking of Institutions

1141. PAT Ranking of Institutions (25)

时间限制
500 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

where "ID" is a string of 6 characters with the first one representing the test level: "B" stands for the basic level, "A" the advanced level and "T" the top level; "Score" is an integer in [0, 100]; and "School" is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that "ID" is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where "Rank" is the rank (start from 1) of the institution; "School" is the institution code (all in lower case); "TWS" is the total weighted score which is defined to be the integer part of "ScoreB/1.5 + ScoreA + ScoreT*1.5", where "ScoreX" is the total score of the testees belong to this institution on level X; and "Ns" is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:
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
Sample Output:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
考试时候做这个题,感觉是结构体排序,但是特别繁琐,自己想着想着就乱了,感觉自己的逻辑思维能力还是太差了,今天晚上重新做了一下这个题,就得了20分,一直是超时,我是用了两个结构体,也想过去优化,但是感觉如果优化需要改动的地方太多了,先上代码吧,最后5组数据tle了
#include<bits/stdc++.h> 
#include<string.h>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
struct node1
{
	string s;
	double score;
	string s1;
}q[100005];
struct node2
{
	string s3;
	double score;
	int score1;
	int num;
}t;
vector<node2>ans;
int cmp(node2 a,node2 b)
{
	if(a.score1==b.score1)
		if(a.num==b.num)
			return a.s3<b.s3;
		else
			return a.num<b.num;
	return a.score>b.score;
}
int main()
{
	freopen("C:/input.txt","r",stdin);
	int n;
	scanf("%d",&n);
	int m=n;
	int k=0;
	while(m--)
	{
		cin >> q[k].s ;
		scanf("%lf",&q[k].score);
		cin >> q[k].s1;
		for(int i=0;i<q[k].s1.size();i++)
		{
			q[k].s1[i]=tolower(q[k].s1[i]);
		}
		if(q[k].s[0]=='B')
		{
			q[k].score/=1.5;
		}
		else if(q[k].s[0]=='T')
		{
			q[k].score*=1.5;
		}
		k++;
	}
	for(int i=0;i<n;i++)
	{
		if(ans.size())
		{
			int r=0;
			for(int j=0;j<ans.size();j++)
			{
				if(q[i].s1==ans[j].s3)
				{
					ans[j].score+=q[i].score;
					ans[j].num++;
				}
				else
				{
					r++;
				}
			}
			if(r==ans.size())
			{
				t.score=q[i].score;
				t.s3=q[i].s1;
				t.num=1;
				ans.push_back(t);
			}
		}
		else
		{
			t.score=q[i].score;
			t.s3=q[i].s1;
			t.num=1;
			ans.push_back(t);
		}
	}
	for(int i=0;i<ans.size();i++)
	{
		ans[i].score1=(int)ans[i].score;
	}
	sort(ans.begin(),ans.end(),cmp);
	int rank;
	int d,p;
	int l=-1;
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++)
	{
		if(l!=ans[i].score1)
			rank=i+1, l=ans[i].score1;
		printf("%d ",rank);
		cout << ans[i].s3 ;	
		printf(" %d %d\n",ans[i].score1,ans[i].num);
	}
	return 0;
}
在60行的那里,每次都需要重新查询,而数据又是1e5,所以这算是一个优化的点,以后想到办法再去优化修改,想到用Map和set数据结构了,但是不熟悉,所以就先用最笨的方法。

接下来再贴一段代码,是用了map的,他是在查找的时候大大节少了时间,我也给代码写上了相应的注释,当初这个代码也是20分,我们一开始用的是cin 和 cout,大家都知道比较浪费时间,我们把cout和cin能改的地方改成了scanf和printf,然后又过了两组数据,最后一组数据不是超时,而是错误。。实在是不知道为什么,有ac的大神可以指导一下么。

#include <bits/stdc++.h>
using namespace std;

class school
{
public:
	string n; //名字
	int r; //人数 
	double f; //分数 
	int k;
	school (string a = "", int b = 1, double c = 0, int d = 1): n(a), r(b), f(c), k(d) {}
};
bool sort_cmp(const school& a, const school& b)
{
	if (a.f != b.f)
		return a.f > b.f;
	if (a.r != b.r)
		return a.r < b.r;
	return a.n < b.n;
}
int main()
{

	//freopen("C:/input.txt", "r", stdin);


	map<string, school> mp;
	int n;
	scanf("%d",&n);
	for (int i = 0; i < n; i++)
	{
		string s1, s2; //难度 学校 
		double x; //分数 
		cin >> s1;
		scanf("%lf",&x);
		cin >> s2;
		if (s1[0] == 'B')
			x /= 1.5;
		else if (s1[0] == 'T')
			x *= 1.5;
		int l = s2.length();
		for (int j = 0; j < l; j++)
			s2[j] = tolower(s2[j]);    //转换成小写字母
		if (mp.count(s2))    //返回被查找元素的个数,不存在相同元素,所以只有0和1
		{
			mp[s2].f += x;
			mp[s2].r++;
		}
		else
		{
			school t(s2, 1, x);
			mp[s2] = t;
		}
	}
	vector<school> vec;
	for (map<string, school>::iterator it = mp.begin(); it != mp.end(); it++)
		vec.push_back(it->second);      //迭代器,把结构体拷入到vec数组
	sort(vec.begin(), vec.end(), sort_cmp);    //排序
	int s = vec.size();
	printf("%d\n",s);
	int last = -1;
	for (int i = 0; i < s; i++)
		if ((int)vec[i].f == last)
			vec[i].k = vec[i - 1].k;
		else
		{
			vec[i].k = i + 1;
			last = (int)vec[i].f;
		}
		for (int i = 0; i < s; i++)
		{
			printf("%d ",vec[i].k);
			cout << vec[i].n;
			printf(" %d %d\n",(int)vec[i].f,vec[i].r);
		}
		return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值