PTA甲级

结构体排序➕二分 | 1012 The Best Rank (2

2019-01-24  本文已影响0人  zilla

写法挺蠢,但是一次AC了也就算了吧2333333
1012 The Best Rank

#include <cstdio>
#include <cstdlib>
const int N=2010;
const char nn[6]="*ACME";
struct Grade{
    int name;
    int sc[4];
}grade[N];
int n,m,gi,tot[N][5];
int cmp(const void* a, const void* b){
    return (*(Grade*)b).sc[gi]-(*(Grade*)a).sc[gi];
}
struct Rank{
    int name;
    int rr;
};
int r_cmp(const void* a, const void* b){
    return (*(Rank*)a).name-(*(Rank*)b).name;
}
Rank c_rank[N],m_rank[N],e_rank[N],a_rank[N];
void rec_rank(Rank rank[]){
    rank[0].name=grade[0].name;
    rank[0].rr=1;
    for (int i = 1; i < n; ++i) {
        rank[i].name=grade[i].name;
        if(grade[i].sc[gi]==grade[i-1].sc[gi])
            rank[i].rr=rank[i-1].rr;
        else rank[i].rr=i+1;
    }
    qsort(rank,n, sizeof(Rank),r_cmp);
}

int binary_search(int no){
    int l=0,r=n-1,m;
    while (l<=r){
        m=(l+r)/2;
        if(tot[m][0]>no)
            r=m-1;
        else if(tot[m][0]<no)
            l=m+1;
        else return m;
    }
    return -1;
}
int main() {
    scanf("%d%d",&n,&m);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d%d%d",&grade[i].name,&grade[i].sc[0],&grade[i].sc[1],&grade[i].sc[2]);
        grade[i].sc[3]=(grade[i].sc[0]+grade[i].sc[1]+grade[i].sc[2])/3;
    }
    gi=0;
    qsort(grade,n, sizeof(Grade),cmp);
    rec_rank(c_rank);
    gi=1;
    qsort(grade,n, sizeof(Grade),cmp);
    rec_rank(m_rank);
    gi=2;
    qsort(grade,n, sizeof(Grade),cmp);
    rec_rank(e_rank);
    gi=3;
    qsort(grade,n, sizeof(Grade),cmp);
    rec_rank(a_rank);
    for (int j = 0; j < n; ++j) {
        tot[j][0]=c_rank[j].name;
        tot[j][1]=a_rank[j].rr;
        tot[j][2]=c_rank[j].rr;
        tot[j][3]=m_rank[j].rr;
        tot[j][4]=e_rank[j].rr;
    }
    int stu;
    for (int k = 0; k < m; ++k) {
        scanf("%d",&stu);
        int res=binary_search(stu);
        if(res==-1){
            puts("N/A");
            continue;
        }
        int b_rank_ind=1,br=tot[res][1];
        for (int i = 2; i <= 4; ++i) {
            if(tot[res][i]<tot[res][b_rank_ind])
                b_rank_ind=i,br=tot[res][i];
        }
        printf("%d %c\n",br,nn[b_rank_ind]);
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读