PAT A1141 PAT Ranking of Institutions(25 分)

总结:

1.结构体必须初始化,和变量一样未初始化的数据是不安全的

2.使用map但是用不着其排序功能的时候使用unordered_map(包含头文件#include<unordered_map>)降低其时间复杂度

代码:

#include<iostream>
#include<string>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    string ID;
    int score;
    string school;
    double totalscore;
    int number;
    node(){
        number = 0;
       totalscore = 0.0;
    }
};
bool cmp(const node&a, const node&b)
{
    if (a.totalscore != b.totalscore)return a.totalscore > b.totalscore;
    else{
        if (a.number != b.number)return a.number < b.number;
        else return a.school < b.school;
    }
}
int main()
{
    vector<node> po;
    unordered_map<string, node> p;
    int m;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        node s;
        cin >> s.ID >> s.score >> s.school;
        for (int x = 0; x < s.school.length(); x++)s.school[x] = tolower(s.school[x]);
        p[s.school].number+=1; p[s.school].ID = s.ID; p[s.school].school = s.school;
        if (s.ID[0] == 'A')p[s.school].totalscore += s.score;
        else if (s.ID[0] == 'T')p[s.school].totalscore += s.score*1.5;
        else if (s.ID[0] == 'B')p[s.school].totalscore += s.score / 1.5;
    }
    int k = 0;
    for (auto it = p.begin(); it != p.end(); it++, k++)
    {
        node su;
        su.school = it->second.school;
        su.totalscore = (int)it->second.totalscore;
        su.number = it->second.number;
        po.push_back(su);
    }
    sort(po.begin(), po.end(), cmp);
    cout << k << endl; int bh = 1; int g = 1;
    for (auto it = po.begin(); it != po.end(); it++,g++)
    {
        auto it2 = it;
        if (it != po.begin())it2--;
        if (it != po.begin() && it->totalscore != it2->totalscore)bh = g;
        cout <<bh<<" "<< it->school << " " << it->totalscore << " " << it->number<< endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81407352