- This problem gives a list of PAT testees’ information including their ID,Score and school.We have to collect the institutions where they from,and output the ranklist of institutions in nondecreasing order of their ranks.
- My Idea
Define a struct insti to store the school name,its testees ’ scores(B,A,T),and the number of its testees.Initialize an array I of insti type to store these institutions.For convenience,I used a map to help us find the index of school in array I quickly.Then I write a comparision function to make it easier to get the ranklist of institutions. - Something need to be NOTED
1.The string of school name has to be in lower case
2.The total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5
Here’s my code.
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include<queue>
#include <unordered_map>
#include <set>
using namespace std;
const int maxn = 100100;
typedef struct insti {
string school;
int sB, sA, sT;
int Ns;
};
insti I[maxn];
map<string, int> index;
bool cmp(insti A, insti B)
{
int s1 = int(A.sB / 1.5 + A.sA + A.sT*1.5);
int s2 = int(B.sB / 1.5 + B.sA + B.sT*1.5);
if (s1 != s2) return s1 > s2;
else if (A.Ns != B.Ns) return A.Ns < B.Ns;
else return A.school < B.school;
}
int main() {
int N;
cin >> N;
string ID, inst;
int score;
for (int i = 0; i < N; i++)
{
cin >> ID >> score >> inst;
transform(inst.begin(), inst.end(), inst.begin(), ::tolower);
if (!index.count(inst))
{
int ind=index.size();
index[inst] = ind;
I[ind].school = inst;
I[ind].sA = 0; I[ind].sB = 0; I[ind].sT = 0;
if (ID[0] == 'B') I[ind].sB = score;
else if (ID[0] == 'A') I[ind].sA = score;
else I[ind].sT = score;
I[ind].Ns = 1;
}
else {
++I[index[inst]].Ns;
if (ID[0] == 'B')I[index[inst]].sB += score;
else if (ID[0] == 'A') I[index[inst]].sA += score;
else I[index[inst]].sT += score;
}
}
sort(I, I + index.size(), cmp);
cout << index.size() << endl;
int rank = 1;
int cnt = 1; //平局的学校数
cout << 1 << " " << I[0].school
<< " " << int(I[0].sB / 1.5 + I[0].sA + I[0].sT*1.5)
<< " " << I[0].Ns << endl;
for (int i = 1; i < index.size(); i++) {
if (int(I[i].sB / 1.5 + I[i].sA + I[i].sT*1.5) == int(I[i - 1].sB / 1.5 + I[i - 1].sA + I[i - 1].sT*1.5))
{
++cnt;
cout << rank << " " << I[i].school
<< " " << int(I[i].sB / 1.5 + I[i].sA + I[i].sT*1.5)
<<" "<< I[i].Ns << endl;
}
else {
rank += cnt;
cnt = 1;
cout << rank << " " << I[i].school
<< " " << int(I[i].sB / 1.5 + I[i].sA +I[i].sT*1.5)
<<" "<< I[i].Ns << endl;
}
}
return 0;
}