Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
3
Indexable
Never
//11490 - The Cat Society
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char order[8][20] = {"elder", "nursy", "kitty", "warrior"
                    ,"apprentice", "medicent", "deputy", "leader"};

typedef struct{
    char name[35];
    char pos[20];
    int age;
    int ord;
}Cat;

int minimum(int a, int b){
    if(a > b) return b;
    else return a;
}

int n, m;
int ans[10005];
Cat cat[10005];

int compare(const void *a, const void *b){
    if(cat[*(int*)a].ord > cat[*(int*)b].ord){
        return 1;
    }
    else if(cat[*(int*)a].ord == cat[*(int*)b].ord){
        if(cat[*(int*)a].age < cat[*(int*)b].age && strcmp(cat[*(int*)a].pos, order[4]) != 0){
            return 1;
        }
        else if(cat[*(int*)a].age > cat[*(int*)b].age && strcmp(cat[*(int*)a].pos, order[4]) == 0){
            return 1;
        }
        else if(cat[*(int*)a].age == cat[*(int*)b].age){
            if(strcmp(cat[*(int*)a].name, cat[*(int*)b].name) < 0){
                return 1;
            }
            else return -1;
        }
        else return -1;
    }
    else return -1;
}

int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 0; i < n; i++){
            scanf("%s%s%d", cat[i].name, cat[i].pos, &cat[i].age);
            for(int j = 0; j < 8; j++){
                if(strcmp(order[j], cat[i].pos) == 0){
                    cat[i].ord = j;
                    break;
                }
            }
            ans[i] = i;
        }
        qsort(ans, n, sizeof(int), compare);
        int x = minimum(m, n);
        for(int i = 0; i < x; i++){
            printf("%s\n", cat[ans[i]].name);
        }
        memset(ans, -1, sizeof(ans));
    }
}