1. 程式人生 > >1141 PAT Ranking of Institutions

1141 PAT Ranking of Institutions

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資料結構了,但是不熟悉,所以就先用最笨的方法。