PAT 1012. The Best Rank (25) Data Structures, Sorting

There was no A question before. Looking back, it was very simple. The only pitfall is that when the scores are the same, the RANK is the same, and the title is not mentioned at all.
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<map>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;


struct ZZ
{
    string id;
    int mark;
    bool operator < (const ZZ &A) const
    {
        return mark>A.mark;
    }
};

struct
{
    int emark;
    int cmark;
    int amark;
    int mmark;
}node[2005];

void gogo(int x)
{
    int a,b,c,d;
    a=node[x].amark;
    b=node[x].cmark;
    c=node[x].emark;
    d=node[x].mmark;
    int maxx=min(a,b);
    maxx=min(maxx,min(c,d));
    cout<<maxx<<' ';
    if(maxx==a) cout<<'A';
    else if(maxx==b) cout<<'C';
    else if(maxx==d) cout<<'M';
    else cout<<'E';
    cout<<endl;
    return ;
}

map<string,int> M;

vector<ZZ> average;
vector<ZZ> english;
vector<ZZ> math;
vector<ZZ> cc;

intmain()
{
     int n,m;
     cin>>n>>m;
     for(int i=0;i<n;i++)
     {
         string id;
         int x,y,z;
         cin>>id>>x>>y>>z;
         M[id]=i;
         ZZ tmp;
         tmp.id=id;
         tmp.mark=x;
         cc.push_back(tmp);
         tmp.mark=y;
         math.push_back(tmp);
         tmp.mark=z;
         english.push_back(tmp);
         tmp.mark=(x+y+z)/3;
         average.push_back(tmp);
     }
     sort(cc.begin(),cc.end());
     sort(average.begin(),average.end());
     sort(english.begin(),english.end());
     sort(math.begin(),math.end());
     int last1,last2,last3,last4;
     last1=last2=last3=last4=0;
     node[M[average[0].id]].amark=1;
     node[M[english[0].id]].emark=1;
     node[M[cc[0].id]].cmark=1;
     node[M[math[0].id]].mmark=1;
     for(int i=0;i<n;i++)
     {
        if(math[i].mark!=math[i-1].mark) {node[M[math[i].id]].mmark=i+1;last1=i;}
        else {node[M[math[i].id]].mmark=last1+1;}
         //node[M[average[i].id]].amark=i+1;
        // node[M[english[i].id]].emark=i+1;
        // node[M[cc[i].id]].cmark=i+1;
        if(average[i].mark!=average[i-1].mark) {node[M[average[i].id]].amark=i+1;last2=i;}
        else {node[M[average[i].id]].amark=last2+1;}
        if(english[i].mark!=english[i-1].mark) {node[M[english[i].id]].emark=i+1;last3=i;}
        else {node[M[english[i].id]].emark=last3+1;}
        if(cc[i].mark!=cc[i-1].mark) {node[M[cc[i].id]].cmark=i+1;last4=i;}
        else {node[M[cc[i].id]].cmark=last4+1;}



     }
     for(int i=0;i<m;i++)
     {
         string tmpp;
         cin>>tmpp;
         if(M.find(tmpp)==M.end()) cout<<"N/A"<<endl;
         else
         {
             int p=M[tmpp];
             gogo (p);
         }


     }

    return 0;
}

Check out another code

#include <cstdio>
#include <algorithm>
using namespace std;
struct node {
    int id, best;
    int score[4], rank[4];
}stu[2005];
int exist[1000000], flag = -1;
bool cmp1(node a, node b) {return a.score[flag] > b.score[flag];}
int main() {
    int n, m, id;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
        stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
    }
    for(flag = 0; flag <= 3; flag++) {
        sort(stu, stu + n, cmp1);
        stu[0].rank[flag] = 1;
        for(int i = 1; i < n; i++) {
            stu[i].rank[flag] = i + 1;
            if(stu[i].score[flag] == stu[i-1].score[flag])
                stu[i].rank[flag] = stu[i-1].rank[flag];
        }
    }
    for(int i = 0; i < n; i++) {
        exist[stu[i].id] = i + 1;
        stu[i].best = 0;
        you are from = stu [i] .rank [0];
        for(int j = 1; j <= 3; j++) {
            if(stu[i].rank[j] < minn) {
                minn = stu [i] .rank [j];
                stu[i].best = j;
            }
        }
    }
    char c[5] = {'A', 'C', 'M', 'E'};
    for(int i = 0; i < m; i++) {
        scanf("%d", &id);
        int temp = exist[id];
        if(temp) {
            int best = stu[temp-1].best;
            printf("%d %c\n", stu[temp-1].rank[best], c[best]);
        } else {
            printf("N/A\n");
        }
    }
    return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325755305&siteId=291194637