PAT甲1141 PAT Ranking of Institutions (25)

不要使用cout和cin,否则会导致最后测试点超时;
另外注意,每个学校的总分是指每个学校各等级考试“总分”乘权重后的和的“整数部分”。因此,不要四舍五入,在录入成绩时,先对各等级考试分别计算总分不要乘权重,最后再根据权重计算total weighted score。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <iostream>
using namespace std;

struct institution
{
    string name;
    int numtestee;
    int tscore;
    int scoreA;
    int scoreB;
    int scoreT;
    int rank;
}A[100010];

bool cmp1(institution a,institution b)
{
    if(a.tscore!=b.tscore)return a.tscore>b.tscore;
    else if(a.numtestee!=b.numtestee)return a.numtestee<b.numtestee;
    else return a.name<b.name;
}

int N;

void tolow(char a[])
{
    for(int i=0;i<strlen(a);i++)
    {
        if(a[i]>='A'&&a[i]<='Z')
        {
            a[i]=a[i]-'A'+'a';
        }
    }
}

map<string,int> mp;

int main()
{
    scanf("%d",&N);
    int count=0;
    char school[10];
    char name[10];
    for(int i=0;i<N;i++)
    {
        int score;
        scanf("%s %d %s",&name,&score,&school);
        tolow(school);
        if(mp.find(school)==mp.end())
        {
            mp[school]=count++;
        }
        A[mp[school]].name=school;
        A[mp[school]].numtestee++;
        if(name[0]=='A')A[mp[school]].scoreA+=score;
        if(name[0]=='B')A[mp[school]].scoreB+=score;
        if(name[0]=='T')A[mp[school]].scoreT+=score;
    }
    for(int i=0;i<count;i++)
    {
        A[i].tscore=A[i].scoreA+A[i].scoreB/1.5+A[i].scoreT*1.5;
    }
    sort(A,A+count,cmp1);
    A[0].rank=1;
    int pre=0;
    for(int i=1;i<count;i++)
    {
        if(A[i].tscore==A[pre].tscore)
        {
            A[i].rank=A[pre].rank;
        }
        else
        {
            A[i].rank=i+1;
            pre=i;
        }
    }
    printf("%d\n",count);
    if(N!=0)
    {
        for(int i=0;i<count;i++)
        {
            printf("%d %s %d %d\n",A[i].rank,A[i].name.c_str(),A[i].tscore,A[i].numtestee);
        }
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/80487403