一杯茶,一包烟,一个j – 没写查一天。
另外发现花里胡哨写了一堆,性能还没有直接排序来得强…蓝瘦,考试写链表真的想不开…
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
struct person{
int id, virtue, talent;
person* next = NULL;
};
int N, L, H, M = 0;
int sortByVirtue(person* p){ return 100 - p->virtue; }
int sortByGrade(person* p){ return 200 - (p->virtue + p->talent); }
int sortByType(person* p)
{
if(p -> virtue >= H && p -> talent >= H)
return 0;
if(p -> virtue >= H)
return 1;
if(p -> virtue >= p -> talent)
return 2;
return 3;
}
person* sort(person* head, int (*getType)(person*), int typeNumber){
person** info = (person**)malloc(sizeof(person*) * typeNumber);
person** tails = (person**)malloc(sizeof(person*) * typeNumber);
memset(info, 0, sizeof(person*) * typeNumber);
memset(tails, 0, sizeof(person*) * typeNumber);
person* ptr = head;
while(ptr != NULL){
int type = (*getType)(ptr);
if(tails[type] != 0){
tails[type]->next = ptr;
ptr = ptr->next;
tails[type] = tails[type]->next;
tails[type]->next = NULL;
}
else
{
info[type] = ptr;
tails[type] = ptr;
ptr = ptr->next;
tails[type]->next = NULL;
}
}
person* ret = NULL;
head = NULL;
for(int i = 0; i < typeNumber; i++)
{
if(tails[i] != 0){
head == NULL ? head : head -> next = info[i];
ret = ret == NULL ? info[i] : ret;
head = tails[i];
}
}
free(info);
free(tails);
return ret;
}
int main()
{
cin>>N>>L>>H;
person* head = (person*)malloc(sizeof(person)), *ptr = head;
for(int i = 0; i < N; i++){
person* p = (person*)malloc(sizeof(person));
cin>>p->id>>p->virtue>>p->talent;
if(p -> virtue >= L && p -> talent >= L)
{
ptr->next = p;
ptr = p;
M++;
}
}
ptr -> next = NULL;
cout<<M<<endl;
if (M <= 0) return 0;
head = head->next;
head = sort(head, sortByVirtue, 101); //根据virtue排序
head = sort(head, sortByGrade, 201); //根据grade排序
head = sort(head, sortByType, 4); //根据类型排序
person* result[100000]; //id进行插入排序
for (int i = 0; i < M ;i++){
result[i] = head;
head = head->next;
}
for (int i = 0; i < M ; i++){
int j = i - 1;
while(j >= 0 &&
result[j]->virtue == result[j + 1]->virtue &&
result[j]->talent == result[j + 1]->talent &&
result[j]->id > result[j + 1]->id
)
{
person* temp = result[j+1];
result[j + 1] = result[j];
result[j] = temp;
j--;
}
}
for (int i = 0; i < M ;i++)
printf("%d %d %d\n", result[i]->id, result[i]->virtue, result[i]->talent);
}